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 pter cerr <<
#define nl '\n'
#define nl "\n"
#define in cin >>
using namespace std;
@ -117,37 +117,64 @@ char toLowerCase(char c) {
return c | 32;
}
int normalizePlayer(char c) {
int normalize(char c) {
return toLowerCase(c) - 'a';
}
void printScores(map<char, int> &points) {
// TODO: change this to custom sort before submission
vector<pair<char, int> > ranking(points.begin(), points.end());
sort(ranking.begin(), ranking.end(), [](const pair<char, int> &a, const pair<char, int> &b) {
return a.second < b.second;
});
void swap(pair<char, int> *a, int i, int j) {
pair<char, int> temp = a[i];
a[i] = a[j];
a[j] = temp;
}
// 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) {
// TODO: simplify this
return !(x & 1 != y & 1 && !(x & 1));
}
void addPoint(
map<char, int> &points,
int *points,
char **board,
int x,
int y,
char player
) {
if (board[x][y] == ' ') {
points[player]++;
if (points[normalize(player)] == -1) {
points[normalize(player)] = 1;
} else {
points[normalize(player)]++;
}
board[x][y] = player;
}
}
@ -156,7 +183,7 @@ void solveVertical(
int moveX,
int moveY,
char **board,
map<char, int> &points,
int *points,
int x,
int y,
char player
@ -190,7 +217,7 @@ void solveHorizontal(
int moveX,
int moveY,
char **board,
map<char, int> &points,
int *points,
int y,
char player
) {
@ -219,7 +246,10 @@ void solveHorizontal(
}
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) {
int moveX = move.moveX;
@ -254,8 +284,9 @@ void solve(Moves moves, char **board) {
}
}
printScores(points);
printBoard(board, moves.x, moves.y);
printScores(points);
dealloc(points);
}
/*