add comments
This commit is contained in:
parent
4f65ec59e8
commit
ed1d837f1b
22
main.cpp
22
main.cpp
@ -144,22 +144,26 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// -------- END SIMPLE VEC IMPLEMENTATION --------
|
// -------- END SIMPLE VEC IMPLEMENTATION --------
|
||||||
|
// Struct to store a move
|
||||||
struct Move {
|
struct Move {
|
||||||
char player;
|
char player;
|
||||||
int moveX;
|
int moveX;
|
||||||
int moveY;
|
int moveY;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Struct to store/serialize the moves
|
||||||
struct Moves {
|
struct Moves {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
myVec<Move> moves;
|
myVec<Move> moves;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Check if the player is valid
|
||||||
bool isValidPlayer(char c) {
|
bool isValidPlayer(char c) {
|
||||||
return c >= 'A' && c <= 'Z' && c != 'X';
|
return c >= 'A' && c <= 'Z' && c != 'X';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serialize the input string
|
||||||
Moves serialize(char *s) {
|
Moves serialize(char *s) {
|
||||||
Moves result;
|
Moves result;
|
||||||
istringstream iss(s);
|
istringstream iss(s);
|
||||||
@ -208,6 +212,7 @@ Moves serialize(char *s) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allocate the board
|
||||||
char **allocBoard(int x, int y) {
|
char **allocBoard(int x, int y) {
|
||||||
char **board = static_cast<char **>(alloc(x * sizeof(char *)));
|
char **board = static_cast<char **>(alloc(x * sizeof(char *)));
|
||||||
for (int i = 0; i < x; ++i) {
|
for (int i = 0; i < x; ++i) {
|
||||||
@ -216,6 +221,7 @@ char **allocBoard(int x, int y) {
|
|||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the board by space or dot
|
||||||
void initBoard(char **board, int x, int y) {
|
void initBoard(char **board, int x, int y) {
|
||||||
for (int i = 0; i < x; ++i) {
|
for (int i = 0; i < x; ++i) {
|
||||||
for (int j = 0; j < y; j++) {
|
for (int j = 0; j < y; j++) {
|
||||||
@ -224,12 +230,14 @@ void initBoard(char **board, int x, int y) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allocate and initialize the board
|
||||||
char **allocAndInitBoard(int x, int y) {
|
char **allocAndInitBoard(int x, int y) {
|
||||||
char **board = allocBoard(x, y);
|
char **board = allocBoard(x, y);
|
||||||
initBoard(board, x, y);
|
initBoard(board, x, y);
|
||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deallocate the board
|
||||||
void deallocBoard(char **board, int x) {
|
void deallocBoard(char **board, int x) {
|
||||||
for (int i = 0; i < x; ++i) {
|
for (int i = 0; i < x; ++i) {
|
||||||
dealloc(board[i]);
|
dealloc(board[i]);
|
||||||
@ -237,6 +245,7 @@ void deallocBoard(char **board, int x) {
|
|||||||
dealloc(board);
|
dealloc(board);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print the board
|
||||||
void printBoard(char **board, int x, int y) {
|
void printBoard(char **board, int x, int y) {
|
||||||
pt " ";
|
pt " ";
|
||||||
for (int col = 0; col < y; ++col) {
|
for (int col = 0; col < y; ++col) {
|
||||||
@ -269,14 +278,17 @@ void printBoard(char **board, int x, int y) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert the character to lower case
|
||||||
char toLowerCase(char c) {
|
char toLowerCase(char c) {
|
||||||
return c | 32;
|
return c | 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize the character to [0, 25)
|
||||||
int normalize(char c) {
|
int normalize(char c) {
|
||||||
return toLowerCase(c) - 'a';
|
return toLowerCase(c) - 'a';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print the scores of the players
|
||||||
void printScores(int *points, char blacklist) {
|
void printScores(int *points, char blacklist) {
|
||||||
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
|
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
|
||||||
26 * sizeof(myPair<char, myPair<int, bool> >)));
|
26 * sizeof(myPair<char, myPair<int, bool> >)));
|
||||||
@ -324,10 +336,13 @@ void printScores(int *points, char blacklist) {
|
|||||||
pt std::endl;
|
pt std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the move is valid
|
||||||
|
// A move is valid if x%2 != y%2 and the cell is empty
|
||||||
bool isValidMove(int x, int y, char **board) {
|
bool isValidMove(int x, int y, char **board) {
|
||||||
return (x & 1) != (y & 1) && board[x][y] == ' ';
|
return (x & 1) != (y & 1) && board[x][y] == ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add a point to the player and update the board
|
||||||
void addPoint(
|
void addPoint(
|
||||||
int *points,
|
int *points,
|
||||||
char **board,
|
char **board,
|
||||||
@ -345,6 +360,7 @@ void addPoint(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check for any box on the top or bottom of the current move
|
||||||
void solveVertical(
|
void solveVertical(
|
||||||
int moveX,
|
int moveX,
|
||||||
int moveY,
|
int moveY,
|
||||||
@ -452,7 +468,8 @@ void solve(Moves moves, char **board) {
|
|||||||
dealloc(points);
|
dealloc(points);
|
||||||
}
|
}
|
||||||
|
|
||||||
int init(char *fileName) {
|
// Run the game
|
||||||
|
int run(char *fileName) {
|
||||||
// --- IO START ---
|
// --- IO START ---
|
||||||
FILE *inp = fopen(fileName, "r");
|
FILE *inp = fopen(fileName, "r");
|
||||||
if (!inp) {
|
if (!inp) {
|
||||||
@ -498,8 +515,9 @@ int main(int argc, char **argv) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// collect errors at single place
|
||||||
try {
|
try {
|
||||||
return init(argv[1]);
|
return run(argv[1]);
|
||||||
} catch (const invalid_argument &e) {
|
} catch (const invalid_argument &e) {
|
||||||
pter e.what() << nl;
|
pter e.what() << nl;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user