-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillingStones.cpp
More file actions
167 lines (146 loc) · 5.58 KB
/
killingStones.cpp
File metadata and controls
167 lines (146 loc) · 5.58 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
#include "graGo.h"
bool moveKills(
int x,
int y,
struct game_state_values curr_state,
struct board_opt curr_board,
int surrounded[BOARD_SIZE * BOARD_SIZE / 2][2],
int possible_kills[4][3]) {
// sprawdŸ czy kamieñ jest w tablicy zbicia i zaznacz obecnosc
for (int i = 0; i < 4; i++) {
if (x == possible_kills[i][0] && y == possible_kills[i][1]) {
possible_kills[i][2] = 1;
break;
}
}
// sprawdz kierunki w ktorych moga znajdowac sie inne kamienie
int x_lewo = ((x - 1 >= 0) ? (x - 1) : -1);
int x_prawo = ((x + 1 < curr_board.size) ? (x + 1) : -1);
int y_gora = ((y - 1 >= 0) ? (y - 1) : -1);
int y_dol = ((y + 1 < curr_board.size) ? (y + 1) : -1);
// sprawdz czy kamien jest otoczony, jesli nie zwroc false
if (x_lewo != -1)
if (curr_state.board[y][x_lewo] == EMPTY)
return false;
if (x_prawo != -1)
if (curr_state.board[y][x_prawo] == EMPTY)
return false;
if (y_gora != -1)
if (curr_state.board[y_gora][x] == EMPTY)
return false;
if (y_dol != -1)
if (curr_state.board[y_dol][x] == EMPTY)
return false;
// dodaj do otoczonych
int index_otoczone;
for (index_otoczone = 0; index_otoczone < (BOARD_SIZE*BOARD_SIZE/2) && surrounded[index_otoczone][0] != -1; index_otoczone++)
;
surrounded[index_otoczone][0] = x;
surrounded[index_otoczone][1] = y;
// znajdz kolejny potencjalny element struktury
if (y_gora != -1)
if (curr_state.board[y_gora][x] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
bool kamien_byl_sprawdzany = false;
for (int i = 0; i < (BOARD_SIZE*BOARD_SIZE/2) && surrounded[i][0] != -1; i++) {
if (surrounded[i][0] == x && surrounded[i][1] == y_gora) {
kamien_byl_sprawdzany = true;
break;
}
}
if (!kamien_byl_sprawdzany) {
if (!moveKills(x, y_gora, curr_state, curr_board, surrounded, possible_kills))
return false;
}
}
if (x_prawo != -1)
if (curr_state.board[y][x_prawo] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
bool kamien_byl_sprawdzany = false;
for (int i = 0; i < (BOARD_SIZE*BOARD_SIZE/2) && surrounded[i][0] != -1; i++) {
if (surrounded[i][0] == x_prawo && surrounded[i][1] == y) {
kamien_byl_sprawdzany = true;
break;
}
}
if (!kamien_byl_sprawdzany) {
if (!moveKills(x_prawo, y, curr_state, curr_board, surrounded, possible_kills))
return false;
}
}
if (y_dol != -1)
if (curr_state.board[y_dol][x] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
bool kamien_byl_sprawdzany = false;
for (int i = 0; i < (BOARD_SIZE*BOARD_SIZE/2) && surrounded[i][0] != -1; i++) {
if (surrounded[i][0] == x && surrounded[i][1] == y_dol) {
kamien_byl_sprawdzany = true;
break;
}
}
if (!kamien_byl_sprawdzany) {
if (!moveKills(x, y_dol, curr_state, curr_board, surrounded, possible_kills))
return false;
}
}
if (x_lewo != -1)
if (curr_state.board[y][x_lewo] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
bool kamien_byl_sprawdzany = false;
for (int i = 0; i < (BOARD_SIZE*BOARD_SIZE/2) && surrounded[i][0] != -1; i++) {
if (surrounded[i][0] == x_lewo && surrounded[i][1] == y) {
kamien_byl_sprawdzany = true;
break;
}
}
if (!kamien_byl_sprawdzany) {
if (!moveKills(x_lewo, y, curr_state, curr_board, surrounded, possible_kills))
return false;
}
}
return true;
}
bool moveCanKill(
int x,
int y,
struct game_state_values curr_state,
struct board_opt curr_board,
int possible_kills[4][3]) {
enum axis {
GORA,
DOL,
PRAWO,
LEWO
};
int x_lewo = ((x - 1 >= 0) ? (x - 1) : -1);
int x_prawo = ((x + 1 < curr_board.size) ? (x + 1) : -1);
int y_gora = ((y - 1 >= 0) ? (y - 1) : -1);
int y_dol = ((y + 1 < curr_board.size) ? (y + 1) : -1);
bool znaleziony_kamien = false;
if (x_lewo != -1) {
if (curr_state.board[y][x_lewo] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
znaleziony_kamien = true;
possible_kills[LEWO][0] = x_lewo;
possible_kills[LEWO][1] = y;
}
}
if (x_prawo != -1) {
if (curr_state.board[y][x_prawo] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
znaleziony_kamien = true;
possible_kills[PRAWO][0] = x_prawo;
possible_kills[PRAWO][1] = y;
}
}
if (y_gora != -1) {
if (curr_state.board[y_gora][x] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
znaleziony_kamien = true;
possible_kills[GORA][0] = x;
possible_kills[GORA][1] = y_gora;
}
}
if (y_dol != -1) {
if (curr_state.board[y_dol][x] == (curr_state.turn == PLAYER_ONE ? STONE_P2 : STONE_P1)) {
znaleziony_kamien = true;
possible_kills[DOL][0] = x;
possible_kills[DOL][1] = y_dol;
}
}
if (znaleziony_kamien) return true;
else return false;
}