-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab7Part2.c
443 lines (394 loc) · 13.4 KB
/
Lab7Part2.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*bool strategyCorner(int dimension, char board[26][26], int i, int j);*/
/*bool strategyCorner(int dimension, char board[26][26], int i, int j){
bool correct = false;
if (((i == 0)&&(j == 0))||((i == 0)&&(j == dimension-1))||((i == dimension-1)&&(j == 0))||((i == dimension-1)&&(j == dimension-1)))
correct = true;
return correct;
}*/
/*
* File: Lab7.c
* Author: weiliche
*
* Created on November 9, 2015, 3:28 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*
*
*/
void printBoard(char board[26][26], int n);
bool positionInBounds(int N, char row, char col);
bool checkLegalInDirection(char board[26][26], int N, char row, char col, char colour, int deltaRow, int deltaCol);
void flip(int dimension, char board[26][26], char colour, char row, char col, int deltaRow, int deltaCol);
void filpAllAfterLegal(int dimension, char board[26][26], char colour, char row, char col);
bool checkLegal(int dimension, char board[26][26], char row, char col, char colour);
bool haveMove(int dimension, char board[26][26], char colour);
void finalCompare(int dimension, char board[26][26], char colour, char diffColour);
int score(int dimension, char board[26][26], char colour, int i, int j);
void scoreBoard(int dimension, char board[26][26], int copy[26][26], char colour);
void changeBestMove(int dimension, char board[26][26], int copy[26][26], char colour);
bool strategyCorner(int dimension, char board[26][26], int i, int j);
bool strategyStar(int dimension, char board[26][26], int i, int j);
bool firstCircle (int dimension, char board[26][26], int i, int j);
bool secondCircle (int dimension, char board[26][26], int i, int j);
bool thirdCircle (int dimension, char board[26][26], int i, int j);
//identify names of functions at the start of whole program
void printBoard(char board[26][26], int n){
int i,j;
char firstRow, firstCol;
firstRow = 'a';
firstCol = 'a';
printf(" ");
for(i=0;i<n;i++){
printf("%c",firstRow);
firstRow++;
}
printf("\n"); //first row of print finish
for(i=0;i<n;i++){
printf("%c ", firstCol);
firstCol++;
for (j=0;j<n;j++)
printf("%c",board[i][j]);
printf("\n"); //print according to the array input row by row
}
}
bool positionInBounds(int N, char row, char col){
if ((N>(row-'a'))&&(N>(col-'a')))
return true;
else
return false; //return false when point is out of the dimension
}
bool checkLegalInDirection(char board[26][26], int N, char row, char col, char colour, int deltaRow, int deltaCol)
{
char sameColour, diffColour;
if (colour == 'W')
{
sameColour = 'W';
diffColour = 'B';
}
else if (colour == 'B')
{
sameColour = 'B';
diffColour = 'W'; //this allows the function to apply on both colors
}
int i,j;
i = row - 'a';
j = col - 'a';
int counter=0;
i += deltaRow;
j += deltaCol;
if((i<0)||(j<0)||(i>=N)||(j>=N))
return false; //when the first move gets out of the boundary
else if (board[i][j]==sameColour)
return false; //when the first move is the same color
else if (board[i][j]=='U')
return false; //when the first move is unoccupied
else
{
while((i>=0)&&(j>=0)&&(i<N)&&(j<N))
{
i+=deltaRow;
j+=deltaCol;
if(board[i][j]==sameColour)
return true; //when the end of different colors is the same color
else if(board[i][j]=='U')
return false; //when the end of different colors is unoccupied
else
counter++; //counter does not work in lab 6, it is prepared for lab 7
}
return false; //when gets out of the boundary
}
}
void flip(int dimension, char board[26][26], char colour, char row, char col, int deltaRow, int deltaCol){
char sameColour, diffColour; //the function only flip and not judge whether the move is available or not
if (colour == 'W')
{
sameColour = 'W';
diffColour = 'B';
}
else if (colour == 'B')
{
sameColour = 'B';
diffColour = 'W';
}
int i,j;
i = row - 'a';
j = col - 'a';
board[i][j] = colour; //flip the origin move
i+=deltaRow;
j+=deltaCol;
while (board[i][j]==diffColour){
board[i][j]=sameColour; //flip the following moves one by one
i+=deltaRow;
j+=deltaCol;
}
}
void flipAllAfterLegal(int dimension, char board[26][26], char colour, char row, char col){
int deltaRow, deltaCol;
for(deltaRow=-1;deltaRow<=1;deltaRow++){
for(deltaCol=-1;deltaCol<=1;deltaCol++){
if ((deltaRow!=0)||(deltaCol!=0)){
if (checkLegalInDirection(board, dimension, row, col, colour, deltaRow, deltaCol) == true){
flip(dimension, board, colour, row, col, deltaRow, deltaCol);
}
}
}
}
}
bool checkLegal(int dimension, char board[26][26], char row, char col, char colour){
bool legal = false;
int deltaRow, deltaCol;
if (positionInBounds(dimension, row, col)==false){
return legal;
}
if (board[row-'a'][col-'a']!='U')
return legal;
for(deltaRow=-1;deltaRow<=1;deltaRow++){
for(deltaCol=-1;deltaCol<=1;deltaCol++){
if ((deltaRow!=0)||(deltaCol!=0)){
if (checkLegalInDirection(board, dimension, row, col, colour, deltaRow, deltaCol) == true){
legal = true;
}
}
}
}
return legal;
}
bool haveMove(int dimension, char board[26][26], char colour){
int i,j;
int deltaRow, deltaCol;
char row, col;
bool haveMove = false;
for(i = 0; i<dimension; i++){
for(j = 0; j<dimension; j++){
row = 'a'+i;
col = 'a'+j;
if (checkLegal(dimension, board, row, col, colour)==true){
haveMove = true;
}
}
}
return haveMove;
}
void finalCompare(int dimension, char board[26][26], char colour, char diffColour){
int i, j;
int numColour = 0;
int numDiffColour = 0;
for(i = 0; i<dimension; i++){
for(j = 0; j<dimension; j++){
if(board[i][j]==colour)
numColour++;
else if(board[i][j]==diffColour)
numDiffColour++;
}
}
if (numColour>numDiffColour)
printf("%c player wins.",colour);
else if (numColour<numDiffColour)
printf("%c player wins.",diffColour);
else
printf("Draw!");
}
int score(int dimension, char board[26][26], char colour, int i, int j){
int score=0;
char diffColour;
int numRow, numCol;
char row, col;
bool legal = false;
row = 'a'+i;
col = 'a'+j;
if (colour == 'W')
diffColour ='B';
if (colour == 'B')
diffColour = 'W';
int deltaRow, deltaCol;
for(deltaRow=-1;deltaRow<=1;deltaRow++){
for(deltaCol=-1;deltaCol<=1;deltaCol++){
if((deltaRow!=0)||(deltaCol!=0)){
if (checkLegalInDirection(board, dimension, row, col, colour, deltaRow, deltaCol)==true){
legal = true;
numRow = i;
numCol = j;
numRow += deltaRow;
numCol += deltaCol;
while (board[numRow][numCol] == diffColour){
score ++;
numRow += deltaRow;
numCol += deltaCol;
}
}
}
}
}
if(legal == true){
if (firstCircle(dimension, board, i, j) == true){
if (strategyCorner(dimension, board, i, j) == true)
score+=1000;
else if ((i = 1)||(i = dimension-2)||(j = 1)||(j = dimension-2))
score-=100;
else
score+=100;
}
else if (secondCircle(dimension, board, i, j) == true){
if (strategyStar(dimension, board, i, j) == true)
score-=1000;
else
score+=50;
}
else if (thirdCircle(dimension, board, i,j) == true){
score+=50;
}
else{
}
}
else
score = 0;
return score;
}
void scoreBoard(int dimension, char board[26][26], int copy[26][26], char colour){
int i,j;
for(i=0;i<dimension;i++){
for(j=0;j<dimension;j++){
copy[i][j]= 0 + score(dimension, board, colour, i, j);
}
}
}
void changeBestMove(int dimension, char board[26][26], int copy[26][26], char colour){
scoreBoard(dimension, board, copy, colour);
int bestI, bestJ;
int i, j;
int count = 0;
int highScore = 0;
char row, col;
for(i=0; i<dimension; i++){
for (j=0; j<dimension; j++){
if ((copy[i][j]!= 0) && (count == 0) && (board[i][j] == 'U')){
count++;
bestI = i;
bestJ = j;
highScore = copy[i][j];
}
else if ((copy[i][j]>highScore) && (board[i][j] == 'U') && (copy[i][j]!= 0)){
bestI = i;
bestJ = j;
highScore = copy[i][j];
}
}
}
row = 'a'+bestI;
col = 'a'+bestJ;
flipAllAfterLegal(dimension, board, colour, row, col);
printf("Computer places %c at %c%c.\n", colour, row, col);
printBoard(board, dimension);
}
bool firstCircle (int dimension, char board[26][26], int i, int j){
bool correct = false;
if ((i == 0)||(j == 0)||(i == dimension -1)||(j == dimension-1))
correct = true;
return correct;
}
bool secondCircle (int dimension, char board[26][26], int i, int j){
bool correct = false;
if((i == 1)||(j == 1)||(i == dimension -2)||(j == dimension-2))
correct = true;
return correct;
}
bool thirdCircle (int dimension, char board[26][26], int i, int j){
bool correct = false;
if((i ==2)||(j == 2)||(i == dimension -3)||(j == dimension -3))
correct = true;
return correct;
}
bool strategyCorner(int dimension, char board[26][26], int i, int j){
bool correct = false;
if (((i == 0)&&(j == 0))||((i == 0)&&(j == dimension-1))||((i == dimension-1)&&(j == 0))||((i == dimension-1)&&(j == dimension-1)))
correct = true;
return correct;
}
bool strategyStar(int dimension, char board[26][26], int i, int j){
bool correct = false;
if (((i == 1)&&(j == 1))||((i == 1)&&(j == dimension -2))||((i == dimension-2)&&(j == 1))||((i == dimension -2)&&(j == dimension -2)))
correct = true;
return correct;
}
int main(void){
int dimension;
char board[26][26];
int i,j;
int out = 0;
char row, col;
char colour, diffColour;
int deltaRow, deltaCol;
int copy[26][26];
printf("Enter the board dimension: ");
scanf("%d",&dimension);
for (i=0;i<dimension;i++){
for (j=0;j<dimension;j++)
board[i][j]='U'; //first set all the elements 'U'
}
board[dimension/2-1][dimension/2-1] = 'W';
board[dimension/2-1][dimension/2] = 'B';
board[dimension/2][dimension/2-1] = 'B';
board[dimension/2][dimension/2] = 'W'; //define array in the range of dimension
for(i=0;i<dimension;i++){
for(j=0;j<dimension;j++){
copy[i][j]=0;
}
}
printf("Computer plays (B/W) : ");
scanf(" %c",&colour);
printBoard(board, dimension); //print the board out
if (colour == 'W')
diffColour = 'B';
if (colour == 'B')
diffColour = 'W';
if (colour == 'W'){
printf("Enter move for colour %c (RowCol): ", diffColour);
scanf(" %c %c", &row, &col);
if (checkLegal(dimension, board, row, col, diffColour) == false){
printf("Invalid move.\n%c player wins.",colour);
return EXIT_SUCCESS;
}
else{
board[row-'a'][col-'a']= diffColour;
flipAllAfterLegal(dimension, board, diffColour, row, col);
printBoard(board, dimension);
}
}
while (out<1) {
if (haveMove(dimension, board, colour)==true)
changeBestMove(dimension, board, copy, colour);
else{
if (haveMove(dimension, board, diffColour)==true){
printf("%c player has no valid move.\n",colour);
}
else{
finalCompare(dimension, board, colour, diffColour);
return EXIT_SUCCESS;
}
}
if (haveMove(dimension, board, diffColour)==true){
printf("Enter move for colour %c (RowCol): ", diffColour);
scanf(" %c %c", &row, &col);
if (checkLegal(dimension, board, row, col, diffColour) == false){
printf("Invalid move.\n%c player wins.",colour);
return EXIT_SUCCESS;
}
else{
board[row-'a'][col-'a']= diffColour;
flipAllAfterLegal(dimension, board, diffColour, row, col);
printBoard(board, dimension);
}
}
else{
if (haveMove(dimension, board, colour)==true){
printf("%c player has no valid move.\n", diffColour);
}
else{
finalCompare(dimension, board, colour, diffColour);
return EXIT_SUCCESS;
}
}
}
}