forked from qyx22122/TheGameOfWar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.h
184 lines (160 loc) · 3.6 KB
/
board.h
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
181
182
183
#define BOARD_SIZE 16
#define BOARD_LENGHT BOARD_SIZE*BOARD_SIZE
typedef struct Board {
bool green[BOARD_LENGHT];
bool blue[BOARD_LENGHT];
} Board;
typedef enum PlayerColor {
GREEN = 0,
BLUE = 1
} PlayerColor;
typedef enum MoveType {
NONE = -1,
ADVANCE = 0,
MOVE = 1
} MoveType;
typedef struct Move {
MoveType type;
int position;
} Move;
bool validMove(Board* b, Move* move, int turn) {
if(move->type == ADVANCE)
return true;
// Check if index is in bounds
if(move->position < 0 || move->position >= BOARD_LENGHT) {
return false;
}
if(turn == 0) {
return !b->blue[move->position];
}
else {
return !b->green[move->position];
}
}
void initBoard(Board* b) {
for(int i = 0; i < BOARD_LENGHT; i++) b->blue[i] = b->green[i] = 0;
char SP[] = "000000000000000001100110011001100110011001100110";
for(int i = 0; i < strlen(SP); i++) b->blue[i] = SP[i] - '0';
for(int i = BOARD_LENGHT-1; i > BOARD_LENGHT-1-strlen(SP); i--) b->green[i] = SP[BOARD_LENGHT-1-i] - '0';
}
void printBoard(Board* b) {
for(int i = 0; i < BOARD_LENGHT; i++){
printf("%c", (b->green[i] ? 'G' : (b->blue[i] ? 'B' : ' ')));
if(i%BOARD_SIZE == BOARD_SIZE-1)
printf("\n");
}
printf("----------------\n");
}
int count(Board* b, int i) {
int count = 0;
int x = i % BOARD_SIZE;
int y = i / BOARD_SIZE;
// Left
if(x > 0) {
count += b->green[x-1 + y * BOARD_SIZE];
count -= b->blue[x-1 + y * BOARD_SIZE];
}
// Right
if(x < BOARD_SIZE-1) {
count += b->green[x+1 + y * BOARD_SIZE];
count -= b->blue[x+1 + y * BOARD_SIZE];
}
// Top
if(y > 0) {
count += b->green[x + (y-1) * BOARD_SIZE];
count -= b->blue[x + (y-1) * BOARD_SIZE];
}
// Bottom
if(y < BOARD_SIZE-1) {
count += b->green[x + (y+1) * BOARD_SIZE];
count -= b->blue[x + (y+1) * BOARD_SIZE];
}
// Top Left
if(x > 0 && y > 0) {
count += b->green[x-1 + (y-1) * BOARD_SIZE];
count -= b->blue[x-1 + (y-1) * BOARD_SIZE];
}
// Top Right
if(x < BOARD_SIZE-1 && y > 0) {
count += b->green[x+1 + (y-1) * BOARD_SIZE];
count -= b->blue[x+1 + (y-1) * BOARD_SIZE];
}
// Bottom Left
if(x > 0 && y < BOARD_SIZE-1) {
count += b->green[x-1 + (y+1) * BOARD_SIZE];
count -= b->blue[x-1 + (y+1) * BOARD_SIZE];
}
// Bottom Right
if(x < BOARD_SIZE-1 && y < BOARD_SIZE-1) {
count += b->green[x+1 + (y+1) * BOARD_SIZE];
count -= b->blue[x+1 + (y+1) * BOARD_SIZE];
}
return count;
}
void updateBoard(Board* b){
Board tmp;
for(int i = 0; i < BOARD_LENGHT; i++) tmp.blue[i] = tmp.green[i] = false;
int c;
for(int i = 0; i < BOARD_LENGHT; i++){
c = count(b,i);
if(b->green[i]) {
if(c < 0) {
tmp.green[i] = false;
tmp.blue[i] = true;
}
if(c == 2 || c == 3) {
tmp.green[i] = true;
}
}
else {
if(c == 3)
tmp.green[i] = true;
}
c *= -1;
if(b->blue[i]) {
if(c < 0) {
tmp.blue[i] = false;
tmp.green[i] = true;
}
if(c == 2 || c == 3) {
tmp.blue[i] = true;
}
}
else {
if(c == 3)
tmp.blue[i] = true;
}
}
for(int i = 0; i < BOARD_LENGHT; i++){
b->green[i] = tmp.green[i];
b->blue[i] = tmp.blue[i];
}
}
void updateBoardMove(Board* b, Move* move, int turn) {
if(!validMove(b, move, turn)) {
printf("Cannot update board - Move isn't valid.\n");
return;
}
if(move->type == ADVANCE) {
updateBoard(b);
return;
}
if(turn == 0) {
b->green[move->position] = !b->green[move->position];
}
else {
b->blue[move->position] = !b->blue[move->position];
}
}
int checkWinCondition(Board* b) {
int bC, gC;
bC = gC = 0;
for(int i = 0; i < BOARD_LENGHT; i++) {
bC += b->blue[i];
gC += b->green[i];
}
if(bC && gC) return 2;
if(!bC && !gC) return -1;
if(!bC) return 0;
if(!gC) return 1;
}