summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game_loop.c62
-rw-r--r--src/move.c47
2 files changed, 67 insertions, 42 deletions
diff --git a/src/game_loop.c b/src/game_loop.c
index 26cb988..67c9ea0 100644
--- a/src/game_loop.c
+++ b/src/game_loop.c
@@ -40,46 +40,46 @@ struct move (*player_one_get_move)();
struct move (*player_two_get_move)();
static struct move current_player_move(enum player_color current_player,
- FILE *player_one_strategy,
- FILE *player_two_strategy) {
- struct move move = {-1, -1};
-
- if ((current_player == WHITE && player_one_strategy == NULL) ||
- (current_player == BLACK && player_two_strategy == NULL)) {
- move = prompt_get_move(current_player);
- } else if (current_player == WHITE) {
- move = get_scm_move(player_one_strategy);
- } else if (current_player == BLACK) {
- move = get_scm_move(player_two_strategy);
- }
-
- return move;
+ char *player_one_strategy_path,
+ char *player_two_strategy_path) {
+ struct move move = {-1, -1};
+
+ if ((current_player == WHITE && player_one_strategy_path == NULL) ||
+ (current_player == BLACK && player_two_strategy_path == NULL)) {
+ move = prompt_get_move(current_player);
+ } else if (current_player == WHITE) {
+ move = get_scm_move(player_one_strategy_path);
+ } else if (current_player == BLACK) {
+ move = get_scm_move(player_two_strategy_path);
+ }
+
+ return move;
}
-enum player_color game_loop(FILE *player_one_strategy,
- FILE *player_two_strategy) {
- initialize_board();
+enum player_color game_loop(char *player_one_strategy_path,
+ char *player_two_strategy_path) {
+ initialize_board();
- if (player_one_strategy == NULL || player_two_strategy == NULL) {
- using_history();
- }
+ if (player_one_strategy_path == NULL || player_two_strategy_path == NULL) {
+ using_history();
+ }
- current_player = WHITE;
+ current_player = WHITE;
#define other_player (current_player == WHITE ? BLACK : WHITE)
- while (has_valid_moves(current_player)) {
- struct move move = current_player_move(current_player, player_one_strategy,
- player_two_strategy);
- if (apply_move(get_board(), current_player, move)) {
- current_player = other_player;
- }
+ while (has_valid_moves(current_player)) {
+ struct move move = current_player_move(current_player, player_one_strategy_path,
+ player_two_strategy_path);
+ if (apply_move(get_board(), current_player, move)) {
+ current_player = other_player;
}
+ }
#undef other_player
- if (player_one_strategy == NULL || player_two_strategy == NULL) {
- rl_clear_history();
- }
+ if (player_one_strategy_path == NULL || player_two_strategy_path == NULL) {
+ rl_clear_history();
+ }
- return get_winner();
+ return get_winner();
}
diff --git a/src/move.c b/src/move.c
index 968dcfa..60cf58b 100644
--- a/src/move.c
+++ b/src/move.c
@@ -53,11 +53,6 @@ static char *prompt_player(enum player_color current_player) {
}
}
-/*static void inner_main(void *closure, int argc, char **argv) {*/
- /*(void)closure;*/
- /*scm_shell(argc, argv);*/
-/*}*/
-
struct move prompt_get_move(enum player_color current_player) {
// Initialize move to an invalid move.
struct move move = {-1, -1};
@@ -72,7 +67,7 @@ struct move prompt_get_move(enum player_color current_player) {
print_help();
} else if (STREQ(input, "p")) {
print_board();
- /*} else if (STREQ(input, "g")) {*/
+ /*} else if (STREQ(input, "g")) {*/
/*int argc = 1;*/
/*char **argv = calloc(1, sizeof(char *));*/
/*argv[0] = "othello";*/
@@ -95,7 +90,8 @@ struct move prompt_get_move(enum player_color current_player) {
}
/* Returns true if the move was valid */
-bool apply_move(enum player_color **board, enum player_color current_player, struct move move) {
+bool apply_move(enum player_color **board, enum player_color current_player,
+ struct move move) {
// The move must be a positive position.
if (move.row < 0 || move.col < 0) {
@@ -179,11 +175,40 @@ bool apply_move(enum player_color **board, enum player_color current_player, str
return flipped_up || flipped_down || flipped_left || flipped_right;
}
-struct move get_scm_move(FILE *strategy) {
+// Return the current board as a list of lists
+static SCM scm_get_board(void) {
+ enum player_color **board = get_board();
+ // 2D list of empty spaces
+ SCM scm_board =
+ scm_make_list(scm_from_int(8), scm_make_list(scm_from_int(0), NULL));
+ for (int i = 0; i < 8; i++) {
+ for (int j = 7; j >= 0; j--) {
+ if (board[i][j] == WHITE) {
+ scm_list_set_x(scm_board, scm_from_int(i),
+ scm_cons(scm_c_eval_string("'w"),
+ scm_list_ref(scm_board, scm_from_int(i))));
+ } else if (board[i][j] == BLACK) {
+ scm_list_set_x(scm_board, scm_from_int(i),
+ scm_cons(scm_c_eval_string("'b"),
+ scm_list_ref(scm_board, scm_from_int(i))));
+ } else {
+ scm_list_set_x(scm_board, scm_from_int(i),
+ scm_cons(scm_c_eval_string("'e"),
+ scm_list_ref(scm_board, scm_from_int(i))));
+ }
+ }
+ }
+ return scm_board;
+}
+
+struct move get_scm_move(char *strategy_path) {
// Initialize move to an invalid move.
struct move move = {-1, -1};
+ scm_init_guile();
+ scm_c_define_gsubr("get-board", 0, 0, 0, scm_get_board);
+
+ SCM scm_move = scm_c_primitive_load(strategy_path);
+ move.row = scm_to_int(scm_car(scm_move));
+ move.col = scm_to_int(scm_cdr(scm_move));
return move;
- (void)strategy;
}
-
-