summaryrefslogtreecommitdiff
path: root/src/valid_moves.c
blob: 445f6544675a04b62407d896806239a474984f5d (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
#include <stdbool.h>

#include "othello.h"

bool is_valid_move(const enum player_color board[8][8],
                   const enum player_color current_player,
                   const struct move move) {
  // The move must be a positive position
  if (move.row < 0 || move.col < 0) {
    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;

  if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) {
    // Check if valid up
    bool would_flip_oppenent = false;
    bool has_other_end = false;
    for (int i = move.row - 1; i > 0 && !has_other_end; i--) {
      if (board[i][move.col] == current_player) {
        has_other_end = true;
      } else if (board[i][move.col] == other_color) {
        would_flip_oppenent = true;
      } else {
        // We have reached an empty tile
        break;
      }
    }
    is_valid_up = has_other_end && would_flip_oppenent;
  }

  if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) {
    // Check if valid down
    bool would_flip_oppenent = false;
    bool has_other_end = false;
    for (int i = move.row + 1; i < 8 && !has_other_end; i++) {
      if (board[i][move.col] == current_player) {
        has_other_end = true;
      } else if (board[i][move.col] == other_color) {
        would_flip_oppenent = true;
      } else {
        // We have reached an empty tile
        break;
      }
    }
    is_valid_down = has_other_end && would_flip_oppenent;
  }

  if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) {
    // Check if valid left
    bool would_flip_oppenent = false;
    bool has_other_end = false;
    for (int i = move.col - 1; i > 0 && !has_other_end; i--) {
      if (board[move.row][i] == current_player) {
        has_other_end = true;
      } else if (board[move.row][i] == other_color) {
        would_flip_oppenent = true;
      } else {
        // We have reached an empty tile
        break;
      }
    }
    is_valid_left = has_other_end && would_flip_oppenent;
  }

  if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) {
    // Check if valid right
    bool would_flip_oppenent = false;
    bool has_other_end = false;
    for (int i = move.col + 1; i < 8 && !has_other_end; i++) {
      if (board[move.row][i] == current_player) {
        has_other_end = true;
      } else if (board[move.row][i] == other_color) {
        would_flip_oppenent = true;
      } else {
        // We have reached an empty tile
        break;
      }
    }
    is_valid_right = has_other_end && would_flip_oppenent;
  }

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

bool has_valid_moves(const enum player_color board[8][8],
                     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;
}