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 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'cmd') 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: -- cgit v1.2.3