-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwinfunc.c
More file actions
181 lines (160 loc) · 4.92 KB
/
winfunc.c
File metadata and controls
181 lines (160 loc) · 4.92 KB
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
180
#include "board.h"
#include "stdio.h"
int BOARD_SIZE = SIZE;
bool checkWinDebug(GameState *state) {
int player = state->current_stone;
player = 3 - player;
//printf("Checking win condition for Player %d\n", player);
// Check rows
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col <= BOARD_SIZE - 5; col++) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row][col + i] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
// Check columns
for (int col = 0; col < BOARD_SIZE; col++) {
for (int row = 0; row <= BOARD_SIZE - 5; row++) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row + i][col] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
// Check diagonals (top-left to bottom-right)
for (int row = 0; row <= BOARD_SIZE - 5; row++) {
for (int col = 0; col <= BOARD_SIZE - 5; col++) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row + i][col + i] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
// Check diagonals (top-right to bottom-left)
for (int row = 0; row <= BOARD_SIZE - 5; row++) {
for (int col = BOARD_SIZE - 1; col >= 4; col--) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row + i][col - i] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
return false;
}
bool checkWin(GameState *state) {
int player = state->current_stone;
// Check rows
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col <= BOARD_SIZE - 5; col++) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row][col + i] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
// Check columns
for (int col = 0; col < BOARD_SIZE; col++) {
for (int row = 0; row <= BOARD_SIZE - 5; row++) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row + i][col] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
// Check diagonals (top-left to bottom-right)
for (int row = 0; row <= BOARD_SIZE - 5; row++) {
for (int col = 0; col <= BOARD_SIZE - 5; col++) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row + i][col + i] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
// Check diagonals (top-right to bottom-left)
for (int row = 0; row <= BOARD_SIZE - 5; row++) {
for (int col = BOARD_SIZE - 1; col >= 4; col--) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (state->board[row + i][col - i] == player)
count++;
else
break;
}
if (count == 5)
return true;
}
}
return false;
}
bool checkDraw(GameState *state){
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (state->board[row][col] == 0){
return false;
}
}
}
return true;
}
int get_winner(GameState* state) {
return checkWin(state) ? state->current_stone : -1;
}
int is_game_over(GameState* state) {
return checkWin(state) || checkDraw(state);
}
int get_valid_move_count(GameState* state) {
return get_valid_moves(state).count;
}
GameState make_move(GameState* state, int action) {
ValidMoves valid_moves = get_valid_moves(state);
int row = valid_moves.rows[action];
int col = valid_moves.cols[action];
add_stone(state, col + 'a', row + 1);
state->current_stone = (state->current_stone == BLACK) ? WHITE : BLACK;
return *state;
}
GameState make_move_copy(GameState state, int action) {
ValidMoves valid_moves = get_valid_moves(&state);
int row = valid_moves.rows[action];
int col = valid_moves.cols[action];
add_stone(&state, col + 'a', row + 1);
state.current_stone = (state.current_stone == BLACK) ? WHITE : BLACK;
return state;
}