avoid using vec in printBoard

This commit is contained in:
Sandipsinh Rathod 2024-10-11 16:52:24 -04:00
parent 244bc8d287
commit 76a1ddd26a
No known key found for this signature in database

@ -156,7 +156,7 @@ bool isValidPlayer(char c) {
return c >= 'A' && c <= 'Z' && c != 'X'; return c >= 'A' && c <= 'Z' && c != 'X';
} }
Moves serialize(const string &s) { Moves serialize(char *s) {
Moves result; Moves result;
istringstream iss(s); istringstream iss(s);
iss >> result.x >> result.y; iss >> result.x >> result.y;
@ -227,75 +227,35 @@ void deallocBoard(char **board, int x) {
} }
void printBoard(char **board, int x, int y) { void printBoard(char **board, int x, int y) {
int boardX = x + 2; pt " ";
int boardY = y + 2; for (int col = 0; col < y; ++col) {
myVec<myVec<string> > ans(boardX); if (!(col % 10)) {
pt (col / 10);
for (int i = 0; i < boardX; ++i) { } else {
ans[i] = myVec<string>(boardY); pt " ";
for (int j = 0; j < boardY; ++j) {
ans[i][j] = " ";
} }
} }
pt endl;
for (int i = 0; i < y; ++i) { pt " ";
if (!(i % 10)) { for (int col = 0; col < y; ++col) {
ans[0][i] = toString((i / 10)); pt (col) % 10;
}
ans[1][i] = toString((i % 10));
} }
pt endl;
for (int i = 0; i < x; ++i) { // Print the board with row numbers
if (!(i % 10)) { for (int row = 0; row < x; ++row) {
ans[i + 2][0] = toString((i / 10)); if (row % 10 == 0) {
pt row / 10;
} else {
pt " ";
} }
ans[i + 2][1] = toString((i % 10)); pt row % 10 << " ";
for (int col = 0; col < y; ++col) {
pt board[row][col];
}
pt endl;
} }
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
ans[i + 2][j + 2] = string(1, 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);
for (int j = 0; j < y; ++j) {
length = max(length, (int) ans[i + 2][j + 2].size());
}
maxLength = max(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) {
if (!j) {
pt ' ';
}
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) {
pt ' ';
}
}
pt nl;
}
pt std::endl;
} }
char toLowerCase(char c) { char toLowerCase(char c) {