summaryrefslogtreecommitdiff
path: root/cmd/main.c
blob: 5863889d56df63ed8a763a578800e929d842b00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This file is a part of othello-ai-guile-c
 *
 * Copyright (C) 2021  Robby Zambito
 *
 * othello-ai-guile-c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * othello-ai-guile-c is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE

#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"
#include "othello_board.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'},
      {"player-two", required_argument, NULL, '2'},
      {"scm-final-board", no_argument, NULL, 's'},
      {0, 0, 0, 0}};

  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 two strategy\n"
      " -s --scm-final-board   print the final board as a Scheme object\n";

  char *player_one_strategy_path = NULL;
  char *player_two_strategy_path = NULL;
  bool should_print_as_scm = false;

  int c;
  while (1) {
    int option_index = 0;
    c = getopt_long(argc, argv, "h1:2:s", long_options, &option_index);
    if (c == -1) {
      break;
    }
    errno = 0;
    switch (c) {
    case 'h':
      printf("%s", usage);
      exit(EXIT_SUCCESS);
    case '1':
      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;
      }
      break;
    case '2':
      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;
      }
      break;
    case 's':
      should_print_as_scm = true;
      break;
    }
  }

  // Couldn't open one or more of the player strategies, bail out.
  if (exit_status == EXIT_FAILURE) {
    exit(EXIT_FAILURE);
  }

  int white_score, black_score;
  enum player_color winner =
      game_loop(player_one_strategy_path, player_two_strategy_path,
                &white_score, &black_score);
  free(player_one_strategy_path);
  free(player_two_strategy_path);

  if (should_print_as_scm) {
    // Make sure guile has been initialized.
    // It probably has, since it is unlikely that the user would want to
    // print the final board as a Scheme object if they have no Scheme
    // players... but it is possible.
    scm_init_guile();

    scm_c_eval_string("(use-modules (ice-9 pretty-print))"
                      "(pretty-print (get-board))");
    printf("%d\n", white_score);
    printf("%d\n", black_score);
  } else {
    switch (winner) {
    case WHITE:
      puts("Congratulations for winning player 1!");
      break;
    case BLACK:
      puts("Congratulations for winning player 2!");
      break;
    default:
      puts("The game ended with no winner.");
    }

    puts("Here was the final board:");
    print_board(get_board());

    printf("Final white score: %d\n", white_score);
    printf("Final black score: %d\n", black_score);
  }

  return EXIT_SUCCESS;
}