summaryrefslogtreecommitdiff
path: root/src/valid_moves.c
blob: 5913fb85ed852069d55d5fe9dfc7feeb94265092 (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
/* 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 <stdbool.h>

#include "othello.h"
#include "othello_move.h"

bool is_valid_move(enum player_color **board,
                   const enum player_color current_player,
                   const struct move move) {
  if (current_player == EMPTY) {
    return false;
  }

  // Make a copy of the input board. This is done because we lean on the logic
  // that exists in apply_move to check if a move is valid. We don't want to
  // necessarily mutate the board yet though.
  enum player_color **b = copy_board(board);
  // Apply the move to the copy of the board.
  bool res = apply_move(b, current_player, move);

  free_board(b);

  return res;
}

bool has_valid_moves(enum player_color **board,
                     const enum player_color current_player) {
  bool result = false;
  struct move move;
  for (move.row = 0; move.row < 8 && !result; move.row++) {
    for (move.col = 0; move.col < 8 && !result; move.col++) {
      result = is_valid_move(board, current_player, move);
    }
  }

  return result;
}