-
Notifications
You must be signed in to change notification settings - Fork 2
/
reversi.c
179 lines (164 loc) · 4.87 KB
/
reversi.c
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
172
173
174
175
176
177
178
179
// Game "Reversi" for Flipper Zero
// Copyright 2023 Dmitry Matyukhin
#include "reversi.h"
// Psst! Most of this file was written with Copilot
// Check if the move is legal by checking if it results in any opponent pieces being captured
bool is_legal_move(int8_t board[BOARD_SIZE][BOARD_SIZE], int row, int col,
int player) {
if (board[row][col] != 0)
return false;
int opponent = -player;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0)
continue;
int r = row + i, c = col + j;
if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE &&
board[r][c] == opponent) {
int k = 2;
while (true) {
r += i;
c += j;
if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE)
break;
if (board[r][c] == player)
return true;
if (board[r][c] == 0)
break;
k++;
}
}
}
}
return false;
}
// Check if the game is over by checking if there are no more moves left for
// either player
bool is_game_over(int8_t board[BOARD_SIZE][BOARD_SIZE]) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (is_legal_move(board, i, j, BLACK) ||
is_legal_move(board, i, j, WHITE)) {
return false;
}
}
}
return true;
}
bool has_legal_moves(int8_t board[BOARD_SIZE][BOARD_SIZE],
int8_t player_color) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (is_legal_move(board, i, j, player_color)) {
return true;
}
}
}
return false;
}
// Calculate the heuristic value of the current board. This function can
// be replaced with a more complex evaluation function that takes into
// account factors such as mobility, piece square tables, etc.
int heuristic(int8_t board[BOARD_SIZE][BOARD_SIZE]) {
int white = 0, black = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 1)
white++;
if (board[i][j] == -1)
black++;
}
}
return white - black;
}
// Make a move on the board and capture any opponent pieces
void make_move(GameState *state, int x, int y, int player) {
state->board[x][y] = player;
int opponent = -player;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0)
continue;
int r = x + i, c = y + j;
if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE &&
state->board[r][c] == opponent) {
int k = 2;
while (true) {
r += i;
c += j;
if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE)
break;
if (state->board[r][c] == player) {
r -= i;
c -= j;
while (r != x || c != y) {
state->board[r][c] = player;
r -= i;
c -= j;
}
break;
}
if (state->board[r][c] == 0)
break;
k++;
}
}
}
}
state->is_game_over = is_game_over(state->board);
}
void init_game(GameState *state) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
state->board[i][j] = 0;
}
}
// Place the initial pieces
int mid = BOARD_SIZE / 2;
state->board[mid - 1][mid - 1] = WHITE;
state->board[mid][mid] = WHITE;
state->board[mid - 1][mid] = BLACK;
state->board[mid][mid - 1] = BLACK;
state->cursor_x = mid - 1;
state->cursor_y = mid + 1;
// Set up turn order
state->human_color = WHITE;
state->current_player = WHITE;
state->is_game_over = false;
}
void human_move(GameState *game_state) {
if (game_state->current_player != game_state->human_color) {
return;
}
if (is_legal_move(game_state->board, game_state->cursor_x,
game_state->cursor_y, game_state->current_player)) {
make_move(game_state, game_state->cursor_x, game_state->cursor_y,
game_state->current_player);
game_state->current_player = -game_state->current_player;
}
}
void computer_move(GameState *game_state) {
if (game_state->current_player == game_state->human_color) {
return;
}
int best_row = -1, best_col = -1, best_score = -1000000;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (!is_legal_move(game_state->board, i, j, game_state->current_player)) {
continue;
}
int score = heuristic(game_state->board);
if (score > best_score) {
best_score = score;
best_row = i;
best_col = j;
}
}
}
if (best_row != -1) {
make_move(game_state, best_row, best_col, game_state->current_player);
}
if (has_legal_moves(game_state->board, game_state->human_color)) {
game_state->current_player = -game_state->current_player;
}
}