From e3a728e10bb15f1443895f51a790d80ec0ca089d Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Fri, 11 Oct 2024 13:31:02 -0400 Subject: [PATCH] fix `printBoards` --- main.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/main.cpp b/main.cpp index d028589..d36e003 100644 --- a/main.cpp +++ b/main.cpp @@ -213,31 +213,32 @@ 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); + myVec > ans(boardX); for (int i = 0; i < boardX; ++i) { + ans[i] = myVec(boardY); for (int j = 0; j < boardY; ++j) { - ans[i].push_back(' '); + ans[i][j] = " "; } } for (int i = 0; i < y; ++i) { if (!(i % 10)) { - ans[0][i] = (i / 10) + '0'; + ans[0][i] = toString((i / 10)); } - ans[1][i] = (i % 10) + '0'; + ans[1][i] = toString((i % 10)); } for (int i = 0; i < x; ++i) { if (!(i % 10)) { - ans[i + 2][0] = (i / 10) + '0'; + ans[i + 2][0] = toString((i / 10)); } - ans[i + 2][1] = (i % 10) + '0'; + ans[i + 2][1] = toString((i % 10)); } for (int i = 0; i < x; ++i) { for (int j = 0; j < y; ++j) { - ans[i + 2][j + 2] = board[i][j]; + ans[i + 2][j + 2] = string(1, board[i][j]); } } @@ -245,9 +246,12 @@ void printBoard(char **board, int x, int y) { int maxLength = 0; for (int i = 0; i < x; ++i) { int length = 2 + (i >= 10 ? 2 : 1); - if (length > maxLength) { - maxLength = length; - } + for(int j = 0; j < y; ++j) { + if (ans[i + 2][j + 2].size() > length) { + length = ans[i + 2][j + 2].size(); + } + } + maxLength = max(maxLength, length); } // Print the first two rows with the calculated spaces @@ -264,14 +268,12 @@ void printBoard(char **board, int x, int y) { // Print the rest of the board for (int i = 2; i < boardX; ++i) { for (int j = 0; j < boardY; ++j) { - if (j == 2) { + if (j==2) { pt ' '; } pt ans[i][j]; - if (j == boardY - 1) { - pt nl; - } } + pt nl; } pt endl; }