initial working code
This commit is contained in:
parent
d9806254ec
commit
6b8d4bea61
115
main.cpp
115
main.cpp
@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
|
||||
#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<char **>(alloc(x * sizeof(char *)));
|
||||
for (int i = 0; i < x; ++i) {
|
||||
board[i] = static_cast<char *>(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<char **>(alloc(sizeof(char *) * x));
|
||||
for (int i = 0; i < x; ++i) {
|
||||
board[i] = static_cast<char *>(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<char, int> &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<char, int> &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<char, int> &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<char, int> 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user