blob: bcbbd464c666639ecf7760eb64ee0c3f0210a71d (
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
|
#include <stdbool.h>
#include "othello.h"
bool is_valid_move(const player_color board[8][8],
const 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 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 player_color board[8][8],
const 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;
}
|