switch to pointer to store players

This commit is contained in:
Sandipsinh Rathod 2024-10-10 12:13:46 -04:00
parent 6b8d4bea61
commit caf1b6d08e

@ -6,7 +6,7 @@
#define pt cout << #define pt cout <<
#define pter cerr << #define pter cerr <<
#define nl '\n' #define nl "\n"
#define in cin >> #define in cin >>
using namespace std; using namespace std;
@ -117,21 +117,44 @@ char toLowerCase(char c) {
return c | 32; return c | 32;
} }
int normalizePlayer(char c) { int normalize(char c) {
return toLowerCase(c) - 'a'; return toLowerCase(c) - 'a';
} }
void printScores(map<char, int> &points) { void swap(pair<char, int> *a, int i, int j) {
// TODO: change this to custom sort before submission pair<char, int> temp = a[i];
vector<pair<char, int> > ranking(points.begin(), points.end()); a[i] = a[j];
sort(ranking.begin(), ranking.end(), [](const pair<char, int> &a, const pair<char, int> &b) { a[j] = temp;
return a.second < b.second;
});
// Print the ranks
for (const auto &rank: ranking) {
pt rank.first << ": " << rank.second << " boxes" << nl;
} }
void bubbleSort(pair<char, int> *a) {
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25 - i; j++) {
if (a[j].second > a[j + 1].second)
swap(a, j, j + 1);
}
}
}
void printScores(int *points) {
pair<char, int> *arr = static_cast<pair<char, int> *>(alloc(26 * sizeof(pair<char, int>)));
for (int i = 0; i < 26; i++) {
arr[i] = {static_cast<char>(i + 'a'), points[i]};
}
bubbleSort(arr);
int prev = -1;
for (int i = 0; i < 26; i++) {
if (arr[i].second != -1) {
pt arr[i].first << ": " << arr[i].second << " boxes" << (i == 25 || prev == arr[i].second ? " (win) \n" : nl);
prev = arr[i].second;
}
}
dealloc(arr);
} }
bool isValidMove(int x, int y) { bool isValidMove(int x, int y) {
@ -140,14 +163,18 @@ bool isValidMove(int x, int y) {
} }
void addPoint( void addPoint(
map<char, int> &points, int *points,
char **board, char **board,
int x, int x,
int y, int y,
char player char player
) { ) {
if (board[x][y] == ' ') { if (board[x][y] == ' ') {
points[player]++; if (points[normalize(player)] == -1) {
points[normalize(player)] = 1;
} else {
points[normalize(player)]++;
}
board[x][y] = player; board[x][y] = player;
} }
} }
@ -156,7 +183,7 @@ void solveVertical(
int moveX, int moveX,
int moveY, int moveY,
char **board, char **board,
map<char, int> &points, int *points,
int x, int x,
int y, int y,
char player char player
@ -190,7 +217,7 @@ void solveHorizontal(
int moveX, int moveX,
int moveY, int moveY,
char **board, char **board,
map<char, int> &points, int *points,
int y, int y,
char player char player
) { ) {
@ -219,7 +246,10 @@ void solveHorizontal(
} }
void solve(Moves moves, char **board) { void solve(Moves moves, char **board) {
map<char, int> points; int *points = static_cast<int *>(alloc(26 * (sizeof(int))));
for (int i = 0; i < 26; i++) {
points[i] = -1;
}
for (Move &move: moves.moves) { for (Move &move: moves.moves) {
int moveX = move.moveX; int moveX = move.moveX;
@ -254,8 +284,9 @@ void solve(Moves moves, char **board) {
} }
} }
printScores(points);
printBoard(board, moves.x, moves.y); printBoard(board, moves.x, moves.y);
printScores(points);
dealloc(points);
} }
/* /*