diff options
author | Robby Zambito <contact@robbyzambito.me> | 2022-01-05 22:38:24 -0500 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2022-01-05 22:38:24 -0500 |
commit | 58324035132009869c406d486605be98725c83f1 (patch) | |
tree | 1ceeca62c7c690caa797440b6a25d72833049bb6 /cmd/main.c | |
parent | 6a35a3cffd976f6fa9d96759e2f2bb0f784b92ac (diff) |
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.
Diffstat (limited to 'cmd/main.c')
-rw-r--r-- | cmd/main.c | 32 |
1 files changed, 24 insertions, 8 deletions
@@ -19,6 +19,7 @@ #define _GNU_SOURCE #include <getopt.h> +#include <errno.h> #include <libguile.h> #include <stdbool.h> #include <stdio.h> @@ -37,10 +38,10 @@ int main(int argc, char **argv) { const char *usage = " -h --help show help output\n" " -1 --player-one <file> specify a file to use for player one strategy\n" - " -2 --player-two <file> specify a file to use for player one strategy\n"; + " -2 --player-two <file> 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: |