summaryrefslogtreecommitdiff
path: root/src/game_loop.c
blob: c76f7e6cf4772b1a31d444b0496929b58e278b50 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#define _GNU_SOURCE

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "othello.h"

#define STREQ(a, b) (strcmp(a, b) == 0)

static enum player_color other_player(enum player_color current_player) {
  switch (current_player) {
  case WHITE:
    return BLACK;
  case BLACK:
    return WHITE;
  default:
    return EMPTY;
  }
}

static int player_number_from_color(enum player_color player) {
  switch (player) {
  case WHITE:
    return 1;
  case BLACK:
    return 2;
  default:
    return -1;
  }
}

static void prompt_player(enum player_color current_player) {
  printf("Player %d [h for help] > ", player_number_from_color(current_player));
}

static void print_help() {
  puts("To enter a move, enter the two row and column seperated by spaces.\n"
       "For example `0 0` will select the top left corner.\n\n"
       "To print the board again, enter `p`\n\n"
       "To drop into a guile repl enter `g`.\n\n"
       "In the guile repl you can define an AI to use for the current "
       "player.\n\n"
       "After you define an AI for that player, it will be used for the rest "
       "of the game instead of prompting for input for that user.");
}

static struct move get_move(enum player_color board[8][8],
                            enum player_color current_player) {
  // Initialize move to an invalid move.
  struct move move = {-1, -1};

  do {
    prompt_player(current_player);
    char *input = NULL;
    size_t input_length = 0;

    errno = 0;
    while (getline(&input, &input_length, stdin) <= 0) {
      if (errno != 0) {
        perror("Error getting input");
        exit(EXIT_FAILURE);
      }
    }

    if (STREQ(input, "h\n")) {
      print_help();
    } else if (STREQ(input, "p\n")) {
      print_board(board);
    } else if (STREQ(input, "g\n")) {
      puts("NOT IMPLEMENTED YET!");
    } else {
      sscanf(input, "%d %d", &move.row, &move.col);
    }

    free(input);
  } while (!is_valid_move(board, current_player, move));

  return move;
}

static void apply_move(enum player_color board[8][8],
                       enum player_color current_player, struct move move) {
  if (!is_valid_move(board, current_player, move)) {

    // We should have checked for the validity of the move before we got here.
    fprintf(stderr, "The move %d %d was not valid for player %d\n", move.row,
            move.col, player_number_from_color(current_player) + 1);
    exit(EXIT_FAILURE);
  }

  // Flip in up direction
  int up_end = move.row;
  for (int i = move.row - 1; i > 0; i--) {
    if (board[i][move.col] == EMPTY) {
      break;
    } else if (board[i][move.col] == current_player) {
      up_end = i;
      break;
    }
  }

  // Flip in down direction
  int down_end = move.row;
  for (int i = move.row + 1; i < 8; i++) {
    if (board[i][move.col] == EMPTY) {
      break;
    } else if (board[i][move.col] == current_player) {
      down_end = i;
      break;
    }
  }

  // Flip in left direction
  int left_end = move.col;
  for (int i = move.col - 1; i > 0; i--) {
    if (board[move.row][i] == EMPTY) {
      break;
    } else if (board[move.row][i] == current_player) {
      left_end = i;
      break;
    }
  }

  // Flip in right direction
  int right_end = move.col;
  for (int i = move.col + 1; i < 8; i++) {
    if (board[move.row][i] == EMPTY) {
      break;
    } else if (board[move.row][i] == current_player) {
      right_end = i;
      break;
    }
  }

  for (int i = move.row; i > up_end; i--) {
    board[i][move.col] = current_player;
  }
  for (int i = move.row; i < down_end; i++) {
    board[i][move.col] = current_player;
  }
  for (int i = move.col; i > left_end; i--) {
    board[move.row][i] = current_player;
  }
  for (int i = move.col; i < right_end; i++) {
    board[move.row][i] = current_player;
  }
}

enum player_color game_loop(enum player_color board[8][8]) {
  enum player_color current_player = WHITE;

  while (has_valid_moves(board, current_player)) {
    struct move move = get_move(board, current_player);
    apply_move(board, current_player, move);
    current_player = other_player(current_player);
  }

  int white_score = 0, black_score = 0;
  for (int row = 0; row < 8; row++) {
    for (int col = 0; col < 8; col++) {
      white_score += board[row][col] == WHITE;
      black_score += board[row][col] == BLACK;
    }
  }

  return white_score > black_score   ? WHITE
         : black_score > white_score ? BLACK
                                     : EMPTY;
}