summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2021-12-29 22:39:07 -0500
committerRobby Zambito <contact@robbyzambito.me>2021-12-29 22:49:59 -0500
commitb9113f203b831e8d4f7cfeeff4701b07da0ea44e (patch)
treef0bb9cb7d900a388d0db6e1e2671583c12f5360f
parentab8cf4fab8a83b95f66ed8c0d76ec2200a23c847 (diff)
Do not pass board around
Instead use the get_board() function to get a reference to the board.
-rw-r--r--cmd/main.c6
-rw-r--r--include/othello.h12
-rw-r--r--src/game_loop.c44
-rw-r--r--src/initialize_board.c18
-rw-r--r--src/print_board.c4
-rw-r--r--src/valid_moves.c9
6 files changed, 47 insertions, 46 deletions
diff --git a/cmd/main.c b/cmd/main.c
index 8b3e521..e751856 100644
--- a/cmd/main.c
+++ b/cmd/main.c
@@ -28,10 +28,8 @@
int main(int argc, char **argv) {
(void)argc;
(void)argv;
- enum player_color board[8][8];
- initialize_board(board);
- enum player_color winner = game_loop(board);
+ enum player_color winner = game_loop();
switch (winner) {
case WHITE:
@@ -45,7 +43,7 @@ int main(int argc, char **argv) {
}
puts("Here was the final board:");
- print_board(board);
+ print_board();
return EXIT_SUCCESS;
}
diff --git a/include/othello.h b/include/othello.h
index e6c7796..acf09a0 100644
--- a/include/othello.h
+++ b/include/othello.h
@@ -40,24 +40,22 @@ extern struct move (*player_two_get_move)(enum player_color board[8][8],
enum player_color current_player);
// Set a board to a new game state.
-void initialize_board(enum player_color board[8][8]);
+void initialize_board(enum player_color ***board);
// True if move is valid for current_player.
// Otherwise false.
-bool is_valid_move(enum player_color board[8][8],
- const enum player_color current_player,
+bool is_valid_move(const enum player_color current_player,
const struct move move);
// True if current_player has any valid moves.
// Otherwise false.
-bool has_valid_moves(enum player_color board[8][8],
- const enum player_color current_player);
+bool has_valid_moves(const enum player_color current_player);
// Plays a game to completion, starting with board.
-enum player_color game_loop(enum player_color board[8][8]);
+enum player_color game_loop(void);
// Prints the current state of the board, including coordinates in the margins.
-void print_board(enum player_color board[8][8]);
+void print_board(void);
// Returns the color of the player whose turn it is.
enum player_color get_current_player(void);
diff --git a/src/game_loop.c b/src/game_loop.c
index 4674e44..c11f473 100644
--- a/src/game_loop.c
+++ b/src/game_loop.c
@@ -31,10 +31,18 @@
#define STREQ(a, b) (strcmp(a, b) == 0)
-struct move (*player_one_get_move)(enum player_color board[8][8],
- enum player_color current_player);
-struct move (*player_two_get_move)(enum player_color board[8][8],
- enum player_color current_player);
+static enum player_color current_player;
+
+enum player_color get_current_player(void) { return current_player; }
+
+static enum player_color **board;
+
+const enum player_color **get_board(void) {
+ return (const enum player_color **)board;
+}
+
+struct move (*player_one_get_move)();
+struct move (*player_two_get_move)();
static enum player_color other_player(enum player_color current_player) {
switch (current_player) {
@@ -76,8 +84,7 @@ static void inner_main(void *closure, int argc, char **argv) {
scm_shell(argc, argv);
}
-static struct move prompt_get_move(enum player_color board[8][8],
- enum player_color current_player) {
+static struct move prompt_get_move(enum player_color current_player) {
// Initialize move to an invalid move.
struct move move = {-1, -1};
@@ -90,7 +97,7 @@ static struct move prompt_get_move(enum player_color board[8][8],
if (STREQ(input, "h")) {
print_help();
} else if (STREQ(input, "p")) {
- print_board(board);
+ print_board();
} else if (STREQ(input, "g")) {
int argc = 1;
char **argv = calloc(1, sizeof(char *));
@@ -108,14 +115,13 @@ static struct move prompt_get_move(enum player_color board[8][8],
}
free(input);
- } while (!is_valid_move(board, current_player, move));
+ } while (!is_valid_move(current_player, move));
return move;
}
/* Returns true if the move was valid */
-static bool apply_move(enum player_color board[8][8],
- enum player_color current_player, struct move move) {
+static bool apply_move(enum player_color current_player, struct move move) {
// Flip in up direction
bool flipped_up = false;
@@ -168,24 +174,16 @@ static bool apply_move(enum player_color board[8][8],
return flipped_up || flipped_down || flipped_left || flipped_right;
}
-static enum player_color current_player;
-
-enum player_color get_current_player(void) { return current_player; }
-
-static enum player_color board[8][8];
-
-const enum player_color **get_board(void) {
- return (const enum player_color **)board;
-}
-enum player_color game_loop(enum player_color init_board[8][8]) {
+enum player_color game_loop() {
+ initialize_board(&board);
using_history();
current_player = WHITE;
- while (has_valid_moves(board, current_player)) {
- struct move move = prompt_get_move(board, current_player);
- apply_move(board, current_player, move);
+ while (has_valid_moves(current_player)) {
+ struct move move = prompt_get_move(current_player);
+ apply_move(current_player, move);
current_player = other_player(current_player);
}
diff --git a/src/initialize_board.c b/src/initialize_board.c
index 2e037c3..18b1468 100644
--- a/src/initialize_board.c
+++ b/src/initialize_board.c
@@ -18,20 +18,26 @@
#define _GNU_SOURCE
+#include <stdlib.h>
+
#include "othello.h"
-void initialize_board(enum player_color board[8][8]) {
+void initialize_board(enum player_color ***board) {
+ *board = malloc(8 * sizeof(enum player_color *));
+ for (int i = 0; i < 8; i++) {
+ (*board)[i] = malloc(8 * sizeof(enum player_color));
+ }
// Set all the positions to empty
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
- board[i][j] = EMPTY;
+ (*board)[i][j] = EMPTY;
}
}
// Fill the starting positions
- board[3][3] = WHITE;
- board[3][4] = BLACK;
- board[4][3] = BLACK;
- board[4][4] = WHITE;
+ (*board)[3][3] = WHITE;
+ (*board)[3][4] = BLACK;
+ (*board)[4][3] = BLACK;
+ (*board)[4][4] = WHITE;
}
diff --git a/src/print_board.c b/src/print_board.c
index 4256e7e..98280fd 100644
--- a/src/print_board.c
+++ b/src/print_board.c
@@ -22,7 +22,9 @@
#include "othello.h"
-void print_board(enum player_color board[8][8]) {
+void print_board() {
+ const enum player_color **board = get_board();
+
puts(" 0 1 2 3 4 5 6 7");
for (int row = 0; row < 8; row++) {
diff --git a/src/valid_moves.c b/src/valid_moves.c
index d37de3c..bd53833 100644
--- a/src/valid_moves.c
+++ b/src/valid_moves.c
@@ -22,9 +22,9 @@
#include "othello.h"
-bool is_valid_move(enum player_color board[8][8],
- const enum player_color current_player,
+bool is_valid_move(const enum player_color current_player,
const struct move move) {
+ const enum player_color **board = get_board();
// The move must be a positive position.
if (move.row < 0 || move.col < 0) {
return false;
@@ -81,13 +81,12 @@ bool is_valid_move(enum player_color board[8][8],
return is_valid_up || is_valid_down || is_valid_left || is_valid_right;
}
-bool has_valid_moves(enum player_color board[8][8],
- const enum player_color current_player) {
+bool has_valid_moves(const enum player_color current_player) {
bool result = false;
struct move move;
for (move.row = 0; move.row < 8 && !result; move.row++) {
for (move.col = 0; move.col < 8 && !result; move.col++) {
- result = is_valid_move(board, current_player, move);
+ result = is_valid_move(current_player, move);
}
}