From 3008c1c2cb2ed105baadc4aa725609871098a23d Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Fri, 11 Oct 2024 12:01:47 -0400 Subject: [PATCH] fix `printBoard` --- main.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 90 insertions(+), 25 deletions(-) diff --git a/main.cpp b/main.cpp index 8419513..ef44b74 100644 --- a/main.cpp +++ b/main.cpp @@ -11,6 +11,9 @@ using namespace std; +// TODO: drop pair +// remove global def of std + // -------- START RAII HELPERS-------- unsigned long allocatedMem = 0; @@ -62,6 +65,12 @@ public: length = 0; } + myVec(size_t size) { + capacity = size; + length = 0; + data = static_cast(malloc(size * sizeof(T))); + } + void push_back(const T &val) { resize(); data[length++] = val; @@ -203,12 +212,68 @@ void deallocBoard(char **board, int x) { } void printBoard(char **board, int x, int y) { + int boardX = x + 2; + int boardY = y + 2; + myVec> ans(boardX); + + for (int i = 0; i < boardX; ++i) { + for (int j = 0; j < boardY; ++j) { + ans[i].push_back(' '); + } + } + + for (int i = 0; i < y; ++i) { + if (!(i % 10)) { + ans[0][i] = (i / 10) + '0'; + } + ans[1][i] = (i % 10) + '0'; + } + + for (int i = 0; i < x; ++i) { + if (!(i % 10)) { + ans[i + 2][0] = (i / 10) + '0'; + } + ans[i + 2][1] = (i % 10) + '0'; + } + for (int i = 0; i < x; ++i) { for (int j = 0; j < y; ++j) { - pt board[i][j]; + ans[i + 2][j + 2] = board[i][j]; + } + } + + // Calculate the maximum length of the row numbers plus two spaces + int maxLength = 0; + for (int i = 0; i < x; ++i) { + int length = 2 + (i >= 10 ? 2 : 1); + if (length > maxLength) { + maxLength = length; + } + } + + // Print the first two rows with the calculated spaces + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < maxLength; ++j) { + pt ' '; + } + for (int j = 0; j < y; ++j) { + pt ans[i][j]; } pt nl; } + + // Print the rest of the board + for (int i = 2; i < boardX; ++i) { + for (int j = 0; j < boardY; ++j) { + if (j == 2) { + pt ' '; + } + pt ans[i][j]; + if (j == boardY - 1) { + pt nl; + } + } + } pt endl; } @@ -238,7 +303,7 @@ void bubbleSort(pair > *a) { void printScores(int *points, char blacklist) { pair > *arr = static_cast > *>(alloc( - 26 * sizeof(pair >))); + 26 * sizeof(pair >))); for (int i = 0; i < 26; i++) { pair > p; @@ -267,13 +332,13 @@ void printScores(int *points, char blacklist) { bubbleSort(arr); - for (int i = 0; i < 26; i++) { + for (int i = 25; i > -1; i--) { if (arr[i].second.first != -1) { pt arr[i].first << ": " << arr[i].second.first << " boxes" << ( - (i == 25 || arr[i].second.second) && arr[i].first != blacklist + (i == 25 || arr[i].second.second) && arr[i].first != blacklist ? arr[i].second.second - ? " (tie)\n" - : " (win)\n" + ? " (tie)\n" + : " (win)\n" : nl); } } @@ -287,11 +352,11 @@ bool isValidMove(int x, int y, char **board) { } void addPoint( - int *points, - char **board, - int x, - int y, - char player + int *points, + char **board, + int x, + int y, + char player ) { if (board[x][y] == ' ') { if (points[normalize(player)] == -1) { @@ -304,13 +369,13 @@ void addPoint( } void solveVertical( - int moveX, - int moveY, - char **board, - int *points, - int x, - int y, - char player + int moveX, + int moveY, + char **board, + int *points, + int x, + int y, + char player ) { // TODO: fix ambiguity: // - @@ -338,12 +403,12 @@ void solveVertical( } void solveHorizontal( - int moveX, - int moveY, - char **board, - int *points, - int y, - char player + int moveX, + int moveY, + char **board, + int *points, + int y, + char player ) { if (moveY) { if (moveY == y - 1) { @@ -363,7 +428,7 @@ void solveHorizontal( } } else { if (board[moveX][moveY + 2] != ' ' && (board[moveX + 1][moveY + 1] != ' ' && board[moveX - 1][moveY + 1] != - ' ')) { + ' ')) { addPoint(points, board, moveX, moveY + 1, player); } }