summaryrefslogtreecommitdiff
path: root/src/valid_moves.c
blob: 7c3e6761b92e5b47171f82980bfb6f523c80dfeb (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
/* 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"

bool is_valid_move(const enum player_color current_player,
                   const struct move move) {
  return true;
  enum player_color **board = get_board();
  // The move must be a positive position.
  if (move.row < 0 || move.col < 0) {
    return false;
  }

  // The move must be below the upper bounds of the board.
  if (move.row > 8 || move.col > 8) {
    return false;
  }

  // The move must be an empty spot to be valid.
  if (board[move.row][move.col] != EMPTY) {
    return false;
  }

  const enum player_color other_color = current_player == WHITE ? BLACK : WHITE;

  bool is_valid_up = false;
  bool is_valid_down = false;
  bool is_valid_left = false;
  bool is_valid_right = false;

#define check_valid_direction(direction_bool, start, end_cond, step, cur_pos)  \
  /* This if statement ensures that we don't check after one direction is      \
   * already valid.                                                            \
   */                                                                          \
  if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) {   \
    bool would_flip_oppenent = false;                                          \
    bool has_other_end = false;                                                \
    for (int i = start; end_cond && !has_other_end; step) {                    \
      if (cur_pos == current_player) {                                         \
        has_other_end = true;                                                  \
      } else if (cur_pos == other_color) {                                     \
        would_flip_oppenent = true;                                            \
      } else {                                                                 \
        /* We have reached an empty tile*/                                     \
        break;                                                                 \
      }                                                                        \
    }                                                                          \
    direction_bool = has_other_end && would_flip_oppenent;                     \
  }

  check_valid_direction(is_valid_up, move.row - 1, i > 0, i--,
                        board[i][move.col]);
  check_valid_direction(is_valid_down, move.row + 1, i < 8, i++,
                        board[i][move.col]);
  check_valid_direction(is_valid_left, move.col - 1, i > 0, i--,
                        board[move.row][i]);
  check_valid_direction(is_valid_right, move.col + 1, i < 8, i++,
                        board[move.row][i]);

#undef check_valid_direction

  return is_valid_up || is_valid_down || is_valid_left || is_valid_right;
}

bool has_valid_moves(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(current_player, move);
    }
  }

  return result;
}