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 /cmd/main.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 'cmd/main.c')
-rw-r--r-- | cmd/main.c | 43 |
1 files changed, 23 insertions, 20 deletions
@@ -18,17 +18,20 @@ #define _GNU_SOURCE -#include <getopt.h> #include <errno.h> +#include <getopt.h> #include <libguile.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "othello.h" int main(int argc, char **argv) { + int exit_status = EXIT_SUCCESS; + static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"player-one", required_argument, NULL, '1'}, @@ -40,8 +43,8 @@ int main(int argc, char **argv) { " -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 two strategy\n"; - FILE *player_one_strategy = NULL; - FILE *player_two_strategy = NULL; + char *player_one_strategy_path = NULL; + char *player_two_strategy_path = NULL; int c; while (1) { @@ -56,31 +59,31 @@ int main(int argc, char **argv) { printf("%s", usage); exit(EXIT_SUCCESS); case '1': - /*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"); + player_one_strategy_path = strdup(optarg); + if (access(player_one_strategy_path, F_OK) == -1) { + fprintf(stderr, "Could not access player 1 strategy\n"); + exit_status = EXIT_FAILURE; } - /*free(player_one_strategy_path);*/ break; case '2': - if (player_two_strategy != NULL) { - fclose(player_two_strategy); + player_two_strategy_path = strdup(optarg); + if (access(player_two_strategy_path, F_OK) == -1) { + fprintf(stderr, "Could not access player 2 strategy\n"); + exit_status = EXIT_FAILURE; } - 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(player_one_strategy, player_two_strategy); + // Couldn't open one or more of the player strategies, bail out. + if (exit_status == EXIT_FAILURE) { + exit(EXIT_FAILURE); + } + + enum player_color winner = + game_loop(player_one_strategy_path, player_two_strategy_path); + free(player_one_strategy_path); + free(player_two_strategy_path); switch (winner) { case WHITE: |