fix errors

This commit is contained in:
Sandipsinh Rathod 2024-11-18 17:04:23 -05:00
parent d8e83f20a0
commit aad83f47dc
No known key found for this signature in database

@ -304,42 +304,36 @@ int normalize(char c) {
// Print the scores of the players
void printScores(int *points, char blacklist) {
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
26 * sizeof(myPair<char, myPair<int, bool> >)));
myPair<char, int> *arr = static_cast<myPair<char, int> *>(alloc(26 * sizeof(myPair<char, int>)));
for (int i = 0; i < 26; i++) {
myPair<char, myPair<int, bool> > p;
myPair<int, bool> p1;
p1.first = points[i];
p1.second = false;
p.first = static_cast<char>(i + 'A');
p.second = p1;
arr[i] = p;
arr[i] = myPair<char, int>(static_cast<char>(i + 'A'), points[i]);
}
// Find the maximum score and count the number of players with the maximum score
int maxScore = -1;
int maxCount = 0;
for (int i = 0; i < 26; i++) {
if (arr[i].second.first > maxScore) {
maxScore = arr[i].second.first;
for (int i = 0; i < 26; ++i) {
if (arr[i].first != blacklist && arr[i].second > maxScore) {
maxScore = arr[i].second;
maxCount = 1;
} else if (arr[i].second.first == maxScore) {
} else if (arr[i].first != blacklist && arr[i].second == maxScore) {
maxCount++;
}
}
for (int i = 0; i < 26; i++) {
if (arr[i].second.first != -1) {
pt "Player " << arr[i].first << " has " << arr[i].second.first << (arr[i].second.first < 2
? " box"
: " boxes");
if (arr[i].second.first == maxScore) {
if (maxCount > 1) {
pt " (tie)";
} else if (arr[i].first != blacklist) {
if (arr[i].second != -1) {
pt "Player " << arr[i].first << " has " << arr[i].second
<< (arr[i].second < 2 ? " box" : " boxes");
// Handle win/tie logic
if (arr[i].second == maxScore && arr[i].first != blacklist) {
if (maxCount == 1) {
pt " (win)";
} else {
pt " (tie)";
}
}
pt nl;
@ -347,7 +341,6 @@ void printScores(int *points, char blacklist) {
}
dealloc(arr);
pt std::endl;
}
// Check if the move is valid
@ -384,20 +377,20 @@ void solveVertical(
int y,
char player
) {
// check for any box below current move
if (moveX < x - 2 && board[moveX + 2][moveY] != ' ' && board[moveX + 1][moveY - 1] != ' ') {
int rightEdge = moveY == y - 1 ? 1 : board[moveX + 1][moveY + 1] != ' ';
if (rightEdge) {
addPoint(points, board, moveX + 1, moveY, player);
}
// Check for a box below the current vertical move
if (moveX < x - 2 && // Ensure the box below is within bounds
board[moveX + 2][moveY] != ' ' && // Bottom horizontal line
board[moveX + 1][moveY - 1] != ' ' && // Left vertical line
board[moveX + 1][moveY + 1] != ' ') { // Right vertical line
addPoint(points, board, moveX + 1, moveY, player); // Add point for box below
}
// check for any box above current move
if (moveX > 1 && board[moveX - 2][moveY] != ' ' && board[moveX - 1][moveY - 1] != ' ') {
int rightEdge = moveY == y - 1 ? 1 : board[moveX - 1][moveY + 1] != ' ';
if (rightEdge) {
addPoint(points, board, moveX - 1, moveY, player);
}
// Check for a box above the current vertical move
if (moveX > 1 && // Ensure the box above is within bounds
board[moveX - 2][moveY] != ' ' && // Top horizontal line
board[moveX - 1][moveY - 1] != ' ' && // Left vertical line
board[moveX - 1][moveY + 1] != ' ') { // Right vertical line
addPoint(points, board, moveX - 1, moveY, player); // Add point for box above
}
}
@ -410,27 +403,20 @@ void solveHorizontal(
int y,
char player
) {
if (moveY) {
if (moveY == y - 1) {
if (board[moveX][moveY - 2] != ' ' && (
board[moveX + 1][moveY - 1] != ' ' && board[moveX - 1][moveY - 1] != ' ')) {
addPoint(points, board, moveX, moveY - 1, player);
}
} else {
if (board[moveX][moveY - 2] != ' ' || board[moveX][moveY + 2] != ' ') {
if (board[moveX + 1][moveY - 1] != ' ' && board[moveX - 1][moveY - 1] != ' ') {
addPoint(points, board, moveX, moveY - 1, player);
}
if (board[moveX + 1][moveY + 1] != ' ' && board[moveX - 1][moveY + 1] != ' ') {
addPoint(points, board, moveX, moveY + 1, player);
}
}
}
} else {
if (board[moveX][moveY + 2] != ' ' && (board[moveX + 1][moveY + 1] != ' ' && board[moveX - 1][moveY + 1] !=
' ')) {
addPoint(points, board, moveX, moveY + 1, player);
}
// Check for a box to the left of the current horizontal move
if (moveY > 1 && // Ensure the box to the left is within bounds
board[moveX][moveY - 2] != ' ' && // Left horizontal line
board[moveX - 1][moveY - 1] != ' ' && // Top vertical line
board[moveX + 1][moveY - 1] != ' ') { // Bottom vertical line
addPoint(points, board, moveX, moveY - 1, player); // Add point for box to the left
}
// Check for a box to the right of the current horizontal move
if (moveY < y - 2 && // Ensure the box to the right is within bounds
board[moveX][moveY + 2] != ' ' && // Right horizontal line
board[moveX - 1][moveY + 1] != ' ' && // Top vertical line
board[moveX + 1][moveY + 1] != ' ') { // Bottom vertical line
addPoint(points, board, moveX, moveY + 1, player); // Add point for box to the right
}
}