diff options
author | Robby Zambito <contact@robbyzambito.me> | 2022-01-17 17:58:28 -0500 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2022-01-17 17:58:28 -0500 |
commit | 59627145631f254191ce1b9de561c8ba0ddc889e (patch) | |
tree | fe6c45083da7ce34bb93f67fe22bf6877c47b918 /src/move.c | |
parent | 6a9badff9bfbbdd7f54f018d3117c1778ecaa74e (diff) |
Successfully read a move from scheme
Also was able to print the current board from scheme, so we should be
able to do basic analysis at least. Should add more primitives for use
from scheme.
Diffstat (limited to 'src/move.c')
-rw-r--r-- | src/move.c | 47 |
1 files changed, 36 insertions, 11 deletions
@@ -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; } - - |