From 58324035132009869c406d486605be98725c83f1 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Wed, 5 Jan 2022 22:38:24 -0500 Subject: Open strategy files and pass them to the game loop Also fixed a bug where we could have gone off the upper bounds of the board. --- cmd/main.c | 32 ++++++++++++++++++++++++-------- include/othello.h | 4 +++- src/game_loop.c | 29 ++++++++++++++++++++++++++--- src/valid_moves.c | 2 +- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/cmd/main.c b/cmd/main.c index 41aecde..c85704a 100644 --- a/cmd/main.c +++ b/cmd/main.c @@ -19,6 +19,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -37,10 +38,10 @@ int main(int argc, char **argv) { const char *usage = " -h --help show help output\n" " -1 --player-one specify a file to use for player one strategy\n" - " -2 --player-two specify a file to use for player one strategy\n"; + " -2 --player-two specify a file to use for player two strategy\n"; - char *player_one_strategy_path = NULL; - char *player_two_strategy_path = NULL; + FILE *player_one_strategy = NULL; + FILE *player_two_strategy = NULL; int c; while (1) { @@ -49,22 +50,37 @@ int main(int argc, char **argv) { if (c == -1) { break; } + errno = 0; switch (c) { case 'h': printf("%s", usage); exit(EXIT_SUCCESS); case '1': - free(player_one_strategy_path); - player_one_strategy_path = strdup(optarg); + /*char *player_one_strategy_path = strdup(optarg);*/ + if (player_one_strategy != NULL) { + fclose(player_one_strategy); + } + player_one_strategy = fopen(optarg, "r"); + if (errno != 0) { + perror("Could not open player 1 strategy"); + } + /*free(player_one_strategy_path);*/ break; case '2': - free(player_two_strategy_path); - player_two_strategy_path = strdup(optarg); + if (player_two_strategy != NULL) { + fclose(player_two_strategy); + } + player_two_strategy = fopen(optarg, "r"); + if (errno != 0) { + perror("Could not open player 2 strategy"); + } + /*char *player_two_strategy_path = strdup(optarg);*/ + /*free(player_two_strategy_path);*/ break; } } - enum player_color winner = game_loop(); + enum player_color winner = game_loop(player_one_strategy, player_two_strategy); switch (winner) { case WHITE: diff --git a/include/othello.h b/include/othello.h index 3c3027c..56745bb 100644 --- a/include/othello.h +++ b/include/othello.h @@ -21,6 +21,7 @@ #pragma once #include +#include enum player_color { EMPTY = '*', @@ -41,7 +42,8 @@ extern struct move (*player_two_get_move)(void); void initialize_board(void); // Plays a game to completion, starting with board. -enum player_color game_loop(void); +enum player_color game_loop(FILE *player_one_strategy, + FILE *player_two_strategy); // Prints the current state of the board, including coordinates in the margins. void print_board(void); diff --git a/src/game_loop.c b/src/game_loop.c index bdb37f5..22c624e 100644 --- a/src/game_loop.c +++ b/src/game_loop.c @@ -39,9 +39,30 @@ enum player_color get_current_player(void) { return current_player; } struct move (*player_one_get_move)(); struct move (*player_two_get_move)(); -enum player_color game_loop() { +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; +} + +enum player_color game_loop(FILE *player_one_strategy, + FILE *player_two_strategy) { initialize_board(); - using_history(); + + if (player_one_strategy == NULL || player_two_strategy == NULL) { + using_history(); + } current_player = WHITE; #define other_player (current_player == WHITE ? BLACK : WHITE) @@ -55,7 +76,9 @@ enum player_color game_loop() { #undef other_player - rl_clear_history(); + if (player_one_strategy == NULL || player_two_strategy == NULL) { + rl_clear_history(); + } return get_winner(); } diff --git a/src/valid_moves.c b/src/valid_moves.c index bd53833..e8e72f6 100644 --- a/src/valid_moves.c +++ b/src/valid_moves.c @@ -31,7 +31,7 @@ bool is_valid_move(const enum player_color current_player, } // The move must be below the upper bounds of the board. - if (move.row >= 8 || move.col >= 8) { + if (move.row > 8 || move.col > 8) { return false; } -- cgit v1.2.3