From 6b8d4bea6175961ef907e7251d54725f38500eb7 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Thu, 10 Oct 2024 11:51:38 -0400 Subject: [PATCH] initial working code --- main.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index a26b5e4..f1e1790 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include #define pt cout << +#define pter cerr << #define nl '\n' #define in cin >> @@ -17,7 +18,7 @@ unsigned long allocatedMem = 0; void *alloc(size_t size) { void *ptr = malloc(size); if (ptr == nullptr) { - cerr << "Memory allocation failed" << nl; + pter "Memory allocation failed" << nl; return nullptr; } allocatedMem += 1; @@ -52,6 +53,8 @@ bool isValidPlayer(char c) { } Moves serialize(const string &s) { + // TODO: add check for impossible board sizes + Moves result; istringstream iss(s); iss >> result.x >> result.y; @@ -72,6 +75,14 @@ Moves serialize(const string &s) { return result; } +char **allocBoard(int x, int y) { + char **board = static_cast(alloc(x * sizeof(char *))); + for (int i = 0; i < x; ++i) { + board[i] = static_cast(alloc(y * sizeof(char))); + } + return board; +} + void initBoard(char **board, int x, int y) { for (int i = 0; i < x; ++i) { for (int j = 0; j < y; j++) { @@ -81,10 +92,7 @@ void initBoard(char **board, int x, int y) { } char **allocAndInitBoard(int x, int y) { - char **board = static_cast(alloc(sizeof(char *) * x)); - for (int i = 0; i < x; ++i) { - board[i] = static_cast(alloc(sizeof(char) * y)); - } + char **board = allocBoard(x, y); initBoard(board, x, y); return board; } @@ -131,6 +139,85 @@ bool isValidMove(int x, int y) { return !(x & 1 != y & 1 && !(x & 1)); } +void addPoint( + map &points, + char **board, + int x, + int y, + char player +) { + if (board[x][y] == ' ') { + points[player]++; + board[x][y] = player; + } +} + +void solveVertical( + int moveX, + int moveY, + char **board, + map &points, + int x, + int y, + char player +) { + // TODO: fix ambiguity: + // - + // | + // - + // in the condition above, we do not know what to do at the end of the board + + // need to check for x-1, x+1, y-1, y+1 + + // check for any box below current move + if (moveX < x - 3 && 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 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); + } + } +} + +void solveHorizontal( + int moveX, + int moveY, + char **board, + map &points, + 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); + } + } +} + void solve(Moves moves, char **board) { map points; @@ -153,9 +240,17 @@ void solve(Moves moves, char **board) { // TODO: if (moveX % 2 == 0 && moveY % 2 == 1) { - + solveVertical(moveX, moveY, board, points, moves.x, moves.y, player); } else if (moveX % 2 == 1 && moveY % 2 == 0) { + solveHorizontal(moveX, moveY, board, points, moves.y, player); + } else { + pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl; + board[moveX][moveY] = 'X'; + printBoard(board, moves.x, moves.y); + printScores(points); + pt "Exiting.." << nl; + return; } } @@ -216,7 +311,7 @@ int init(char *fileName) { // --- IO START --- FILE *inp = fopen(fileName, "r"); if (inp == nullptr) { - cerr << "File not found" << nl; + pter "File not found" << nl; return 1; } @@ -244,7 +339,7 @@ int init(char *fileName) { fclose(inp); if (!assert_alloc()) { - cerr << "Memory leak detected" << nl; + pter "Memory leak detected" << nl; return 1; } // --- CLEANUP END --- @@ -253,14 +348,14 @@ int init(char *fileName) { int main(int argc, char **argv) { if (argc < 2) { - cerr << "No input file provided" << nl; + pter "No input file provided" << nl; return 1; } try { return init(argv[1]); } catch (const invalid_argument &e) { - cerr << e.what() << nl; + pter e.what() << nl; return 1; } }