-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOLLINSS-reversi2.c
More file actions
358 lines (337 loc) · 12.8 KB
/
COLLINSS-reversi2.c
File metadata and controls
358 lines (337 loc) · 12.8 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include "reversi.h"
/*
* Get_Human_Move()
*
* Requests a valid move from the user for given <player>'s turn,
* either PLAYER1 or PLAYER2
* Sets the x,y coordinates for the valid move by filling the <x> & <y>
* pointers
* Returns the score for the chosen move, i.e. the number of squares converted
* from the opponent's colour to the player's colour, plus one for the
* move itself.
*
* Remember that the user types values in the range 1...MAX_BOARD_SIZE, but
* the program should subtract 1 so that the x/y coordinates are in the
* range 0...MAX_BOARD_SIZE-1. C likes numbers to start from 0!
*/
int Get_Human_Move(int player, int * x, int * y) {
/* the function returns -1 because that indicates to the calling function
* that the program should terminate, which is the behaviour we want when
* the function isn't implemented. When you write the function, replace
* the return value with the score for the chosen move.
*/
int error = 1, test, score = 1, col, row, enemy = 0;
char c;
/* checks which player's turn it is and sets the opposition accordingly, "enemy" */
if (player == PLAYER1)
enemy = PLAYER2;
if (player == PLAYER2)
enemy = PLAYER1;
/* loops until a correct move is entered */
while (error == 1) {
fprintf(stderr, "Enter your valid move player %i, in an x y format or 'q' to terminate or 'p' to pass \n", player);
test = fscanf(stdin, "%d %d", x, y);
if (test == 2) {
if (( * x) <= MAX_BOARD_SIZE && ( * x) >= 0 && ( * y) <= MAX_BOARD_SIZE && ( * y) >= 0) {
( * y) --; /* (*x) does not need to be decremented due to the format of my initialisation */
col = * x;
row = * y;
/* checks to see if entered move is a free space and if surrounded by "enemy" tiles */
if (board[col][row] == EMPTY_SPACE && (board[col - 1][row - 1] == enemy || board[col - 1][row] == enemy || board[col - 1][row + 1] == enemy || board[col][row + 1] == enemy || board[col][row - 1] == enemy || board[col + 1][row + 1] == enemy || board[col + 1][row] == enemy || board[col + 1][row - 1] == enemy))
error = 0;
score = Get_Score(player, col, row);
if (score == 0)
error = 1;
}
} else if (test != 2) {
test = fscanf(stdin, "%c", & c);
if (c == 'p') {
return 0;
}
if (c == 'q')
return -1;
}
}
return score;
}
/*
* Get_Score()
*
* Returns the score for the chosen move <x>, <y> by the given <player>,
* i.e. the number of squares converted from the opponent's colour to the
* player's colour, plus one for the move itself.
*
* Returns 0 if no squares are converted
*/
int Get_Score(int player, int x, int y) {
/* fill with your code, and replace return value as appropriate */
int score = 0, go, i, j, enemy, tempscore = 0;
/* checks which player's turn it is and sets the opposition accordingly, "enemy" */
if (player == PLAYER1)
enemy = 2;
if (player == PLAYER2)
enemy = 1;
/* each direction (all 8) is looped through and the total points are added up */
if (board[(x) - 1][(y) - 1] == enemy) {
for (i = (x) - 1, j = (y) - 1, go = 1; i >= 0 && j >= 0 && go == 1; i--, j--) {
if (board[i][j] == enemy)
tempscore++; /*temporary score holder incase the line is not terminated with player's own tile */
if (board[i][j] == player) {
go = 0;
score += tempscore; /* if line is terminated and points are aggregated it is added to the running total: "score" */
}
if (board[i][j] == EMPTY_SPACE) /* line is not terminated and so contents of "tempscore" are lost */
go = 0; /* "go" is an exit variable that can be used to exit for loop prematurely */
}
tempscore = 0;
}
if (board[x - 1][y] == enemy) {
for (i = (x) - 1, j = (y), go = 1; i >= 0 && go == 1; i--) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x - 1][y + 1] == enemy) {
for (i = (x) - 1, j = (y) + 1, go = 1; i >= 0 && j <= MAX_BOARD_SIZE && go == 1; i--, j++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x][y + 1] == enemy) {
for (i = (x), j = (y) + 1, go = 1; j <= MAX_BOARD_SIZE && go == 1; j++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x][y - 1] == enemy) {
for (i = (x), j = (y) - 1, go = 1; j >= 0 && go == 1; j--) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x + 1][y + 1] == enemy) {
for (i = (x) + 1, j = (y) + 1, go = 1; i <= MAX_BOARD_SIZE && j <= MAX_BOARD_SIZE && go == 1; i++, j++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x + 1][y] == enemy) {
for (i = (x) + 1, j = (y), go = 1; i <= MAX_BOARD_SIZE && go == 1; i++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x + 1][y - 1] == enemy) {
for (i = (x) + 1, j = (y) - 1, go = 1; i <= MAX_BOARD_SIZE && j >= 0 && go == 1; i++, j--) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (score != 0)
score++;
return score;
}
/*
* Move()
*
* Updates the board with given <player>'s move at <x>,<y>
*
* Returns the score on a successful move, i.e. the number of squares converted
* from the opponent's colour to the player's colour, plus one for the
* move itself.
*
* Returns 0 on an invalid move
*/
int Move(int player, int x, int y) {
/* fill with your code, and replace return value as appropriate */
int score = 0, go, i, j, s, enemy, tempscore = 0;
board[x][y] = player;/* sets the intial position of player to an occupied state */
/* checks which player's turn it is and sets the opposition accordingly, "enemy" */
if (player == PLAYER1)
enemy = 2;
if (player == PLAYER2)
enemy = 1;
/* each direction is looped through as in Get_Score but the value of the tiles are set accordingly also*/
if (board[(x) - 1][(y) - 1] == enemy) {
for (i = (x) - 1, j = (y) - 1, go = 1; i >= 0 && j >= 0 && go == 1; i--, j--) {
if (board[i][j] == enemy)
tempscore++; /*temporary score holder incase the line is not terminated with player's own tile */
if (board[i][j] == player) {
go = 0;
score += tempscore; /* if line is terminated and points are aggregated it is added to the running total: "score" */
for (s = tempscore; s > 0; s--) /* sets the relevant tiles to player's tiles */
board[i + s][j + s] = player;
}
if (board[i][j] == EMPTY_SPACE) /* line is not terminated and so tempscore is lost and loop exits */
go = 0; /* "go" is an exit variable that can be used to exit for loop prematurely */
}
tempscore = 0; /* reset to zero for next direction */
}
if (board[x - 1][y] == enemy) {
for (i = (x) - 1, j = (y), go = 1; i >= 0 && go == 1; i--) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i + s][j] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x - 1][y + 1] == enemy) {
for (i = (x) - 1, j = (y) + 1, go = 1; i >= 0 && j <= MAX_BOARD_SIZE && go == 1; i--, j++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i + s][j - s] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x][y + 1] == enemy) {
for (i = (x), j = (y) + 1, go = 1; j <= MAX_BOARD_SIZE && go == 1; j++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i][j - s] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x][y - 1] == enemy) {
for (i = (x), j = (y) - 1, go = 1; j >= 0 && go == 1; j--) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i][j + s] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x + 1][y + 1] == enemy) {
for (i = (x) + 1, j = (y) + 1, go = 1; i <= MAX_BOARD_SIZE && j <= MAX_BOARD_SIZE && go == 1; i++, j++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i - s][j - s] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x + 1][y] == enemy) {
for (i = (x) + 1, j = (y), go = 1; i <= MAX_BOARD_SIZE && go == 1; i++) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i - s][j] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (board[x + 1][y - 1] == enemy) {
for (i = (x) + 1, j = (y) - 1, go = 1; i <= MAX_BOARD_SIZE && j >= 0 && go == 1; i++, j--) {
if (board[i][j] == enemy)
tempscore++;
if (board[i][j] == player) {
go = 0;
score += tempscore;
for (s = tempscore; s > 0; s--)
board[i - s][j + s] = player;
}
if (board[i][j] == EMPTY_SPACE)
go = 0;
}
tempscore = 0;
}
if (score != 0)
score++; /*increments for the move itslef as well */
/* sets the new overall scores for each player to be printed */
if (player == PLAYER1) {
player1_score += score;
player2_score -= (score - 1);
}
if (player == PLAYER2) {
player2_score += score;
player1_score -= (score - 1);
}
return score;
}