fix printBoard

This commit is contained in:
Sandipsinh Rathod 2024-10-11 12:01:47 -04:00
parent 6f8eb0f0ac
commit 3008c1c2cb
No known key found for this signature in database

115
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<T *>(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<myVec<char>> 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<char, pair<int, bool> > *a) {
void printScores(int *points, char blacklist) {
pair<char, pair<int, bool> > *arr = static_cast<pair<char, pair<int, bool> > *>(alloc(
26 * sizeof(pair<char, pair<int, bool> >)));
26 * sizeof(pair<char, pair<int, bool> >)));
for (int i = 0; i < 26; i++) {
pair<char, pair<int, bool> > 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);
}
}