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 --------
|
||||
// Struct to store a move
|
||||
struct Move {
|
||||
char player;
|
||||
int moveX;
|
||||
int moveY;
|
||||
};
|
||||
|
||||
// Struct to store/serialize the moves
|
||||
struct Moves {
|
||||
int x;
|
||||
int y;
|
||||
myVec<Move> moves;
|
||||
};
|
||||
|
||||
// Check if the player is valid
|
||||
bool isValidPlayer(char c) {
|
||||
return c >= 'A' && c <= 'Z' && c != 'X';
|
||||
}
|
||||
|
||||
// Serialize the input string
|
||||
Moves serialize(char *s) {
|
||||
Moves result;
|
||||
istringstream iss(s);
|
||||
@ -208,6 +212,7 @@ Moves serialize(char *s) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Allocate the board
|
||||
char **allocBoard(int x, int y) {
|
||||
char **board = static_cast<char **>(alloc(x * sizeof(char *)));
|
||||
for (int i = 0; i < x; ++i) {
|
||||
@ -216,6 +221,7 @@ char **allocBoard(int x, int y) {
|
||||
return board;
|
||||
}
|
||||
|
||||
// Initialize the board by space or dot
|
||||
void initBoard(char **board, int x, int y) {
|
||||
for (int i = 0; i < x; ++i) {
|
||||
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 **board = allocBoard(x, y);
|
||||
initBoard(board, x, y);
|
||||
return board;
|
||||
}
|
||||
|
||||
// Deallocate the board
|
||||
void deallocBoard(char **board, int x) {
|
||||
for (int i = 0; i < x; ++i) {
|
||||
dealloc(board[i]);
|
||||
@ -237,6 +245,7 @@ void deallocBoard(char **board, int x) {
|
||||
dealloc(board);
|
||||
}
|
||||
|
||||
// Print the board
|
||||
void printBoard(char **board, int x, int y) {
|
||||
pt " ";
|
||||
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) {
|
||||
return c | 32;
|
||||
}
|
||||
|
||||
// Normalize the character to [0, 25)
|
||||
int normalize(char c) {
|
||||
return toLowerCase(c) - 'a';
|
||||
}
|
||||
|
||||
// Print the scores of the players
|
||||
void printScores(int *points, char blacklist) {
|
||||
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
|
||||
26 * sizeof(myPair<char, myPair<int, bool> >)));
|
||||
@ -324,10 +336,13 @@ void printScores(int *points, char blacklist) {
|
||||
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) {
|
||||
return (x & 1) != (y & 1) && board[x][y] == ' ';
|
||||
}
|
||||
|
||||
// Add a point to the player and update the board
|
||||
void addPoint(
|
||||
int *points,
|
||||
char **board,
|
||||
@ -345,6 +360,7 @@ void addPoint(
|
||||
}
|
||||
}
|
||||
|
||||
// check for any box on the top or bottom of the current move
|
||||
void solveVertical(
|
||||
int moveX,
|
||||
int moveY,
|
||||
@ -452,7 +468,8 @@ void solve(Moves moves, char **board) {
|
||||
dealloc(points);
|
||||
}
|
||||
|
||||
int init(char *fileName) {
|
||||
// Run the game
|
||||
int run(char *fileName) {
|
||||
// --- IO START ---
|
||||
FILE *inp = fopen(fileName, "r");
|
||||
if (!inp) {
|
||||
@ -498,8 +515,9 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// collect errors at single place
|
||||
try {
|
||||
return init(argv[1]);
|
||||
return run(argv[1]);
|
||||
} catch (const invalid_argument &e) {
|
||||
pter e.what() << nl;
|
||||
return 1;
|
||||
|
Loading…
Reference in New Issue
Block a user