-
Notifications
You must be signed in to change notification settings - Fork 4
/
piece.h
530 lines (412 loc) · 16.4 KB
/
piece.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#ifndef PIECE_H
#define PIECE_H
#include "datastructures.h"
void DrawPiece(PtrBoard, PtrCell, int, int, int);
int NoOfPieces(int *, int *, PtrBoard);
/*
* @description : Used to draw the piece, This can be called anywhere you need to draw a piece.
*
* @param - board - Address of the global struct Board variable. We use it to 'fill' in the array entries, as we build the piece
* @param - cell - Address of the cell to which this piece belongs
* @param - pieceNo - Index of this piece in the Pieces array in Board struct
* @param - Color used to draw this piece
*
*/
void DrawPiece(PtrBoard board, PtrCell cell, int pieceNo, int color, int isKing = FALSE)
{
//store a reference to the cell in piece array
board->Pieces[pieceNo].Cell = cell;
//Is this a red or blue piece ?
int type = color == RED ? RED : BLUE ;
// configure the properties of associated cell
cell->IsOccupied = TRUE; //this cell now has a piece belonging to it.
cell->OccupiedBy = type;
board->Pieces[pieceNo].IsKing = isKing; //we also set this param in MovePiece function
board->Pieces[pieceNo].State = OnBoard;
board->Pieces[pieceNo].Type = type;
board->Pieces[pieceNo].Index = pieceNo;
//store a reference to this piece in cell
cell->Piece = &board->Pieces[pieceNo];
//set draw color of circle / piece
setcolor(color);
//center of circle; this is also used as seed point
int circleX = (cell->Left + cell->Right) / 2;
int circleY = (cell->Top + cell->Bottom) / 2;
//draws the circle for this piece
circle( circleX ,circleY, RADIUS );
setfillstyle(SOLID_FILL, color);
floodfill( circleX, circleY, color );
//if the cell contains a kinged piece
if (cell->Piece->IsKing)
{
//draws the circle for king indication
setcolor(WHITE);
circle( circleX ,circleY, RADIUS / 4);
setfillstyle(SOLID_FILL, GREEN);
floodfill( circleX, circleY, WHITE );
}
// reset draw color to draw the next rectangle / cell
setcolor(BORDER_COLOR);
}
/* @description - Used to draw the (sort of) score board, indicating how many pieces are 'OnBoard' of a particular type
* @param -
* @param -
* @param -
*
* @return -
*/
void DrawIndicator(PtrBoard board)
{
char * buffer = (char *)malloc( 3 );
int bluePieces = 0, redPieces = 0;
NoOfPieces(&redPieces, &bluePieces, board);
int x = 650, y = 500;
outtextxy(VERTICAL_HUDLINE + 70, y - 50, "Number of pieces left");
// red circle
setcolor(RED);
circle(VERTICAL_HUDLINE + 130, y + 60, 25);
setfillstyle(SOLID_FILL, RED); //fills the color red in it
floodfill(VERTICAL_HUDLINE + 130, y + 60, RED);
// red pieces
if (redPieces < 10) outtextxy( VERTICAL_HUDLINE + 124, y + 100, itoa(redPieces, buffer, 10) );
else outtextxy( VERTICAL_HUDLINE + 117, y + 100, itoa(redPieces, buffer, 10) );
// blue circle
setcolor(BLUE);
circle(VERTICAL_HUDLINE + 270, y + 60, 25);
setfillstyle(SOLID_FILL, BLUE); //fills the color blue in it
floodfill(VERTICAL_HUDLINE + 271, y + 60, BLUE);
// blue pieces
if (bluePieces < 10) outtextxy(VERTICAL_HUDLINE + 264, y + 100, itoa(bluePieces, buffer, 10) );
else outtextxy(VERTICAL_HUDLINE + 257, y + 100, itoa(bluePieces, buffer, 10) );
setcolor(CYAN);
}
/* @description -
*
* @param -
* @param -
* @param -
*
* @return -
*/
void MovePiece(PtrMove move, int turn, PtrBoard board, int forAI = FALSE)
{
//change previous cell data
move->CurrentCell->IsOccupied = FALSE;
//change target cell data
move->TargetCell->Piece = move->CurrentCell->Piece;
move->TargetCell->IsOccupied = TRUE;
move->TargetCell->OccupiedBy = turn;
move->CurrentCell->Piece = NULL;
move->CurrentCell->OccupiedBy = NONE;
//re draw prev cell
DrawCell( move->CurrentCell, move->CurrentCell->Row, move->CurrentCell->Column );
//redraw target cells in normal white color
if ( move->TargetCell != NULL) DrawCell( move->TargetCell, move->TargetCell->Row, move->TargetCell->Column );
if (!forAI)
{
for(int t = 0; t < 3; t++)
{
if ( move->OtherTargetCells[t] != NULL )
DrawCell( move->OtherTargetCells[t], move->OtherTargetCells[t]->Row, move->OtherTargetCells[t]->Column );
}
}
int isKing = FALSE;
//determine if the piece is kinged or not, piece is kinged if it reaches the opposite end of the board
if ( turn == RED && move->TargetCell->Row == ROW - 1 ) { move->TargetCell->Piece->IsKing = TRUE; isKing = TRUE; }
if ( turn == BLUE && move->TargetCell->Row == 0 ) { move->TargetCell->Piece->IsKing = TRUE; isKing = TRUE; }
//draw piece on target /destination cell
DrawPiece( board, move->TargetCell, move->TargetCell->Piece->Index, turn, move->TargetCell->Piece->IsKing );
// If the move is a jump, then the JumpedCell needs to be updated as well
if (move->isJump)
{
DrawCell(move->JumpedCell, move->JumpedCell->Row, move->JumpedCell->Column);
move->JumpedCell->Piece->State = Removed;
move->JumpedCell->Piece = NULL;
move->JumpedCell->IsOccupied = FALSE;
move->JumpedCell->OccupiedBy = NONE;
}
}
/* @description -
*
* @param -
* @param -
* @param -
*
* @return -
*/
void PlayAITurn(PtrBoard board, int AIColor)
{
// Get the Opponent's Color to predict their moves
int opponentColor = (AIColor == RED) ? BLUE : RED;
// PieceType AIPiece = (turn == BLUE) ? Blue : Red;
// PieceType opponentPiece = (turn == BLUE) ? Red : Blue;
// Array to hold all the pieces the AI or user can control
Piece AIPieces[ PIECES_COUNT/2 ];
Piece opponentPieces[ PIECES_COUNT/2 ];
// Holds the number of pieces for use in for loops
int numberOfAIPieces = 0;
int numberOfOpponentPieces = 0;
// Goes through all the pieces on the board
for (int i = 0; i < (PIECES_COUNT); i++)
{
// Checks to see whom the piece belongs to and whether it is on the
// the board, that is not removed or "jumped over".
if (board->Pieces[i].State == OnBoard)
{
if (board->Pieces[i].Type == AIColor)
AIPieces[ numberOfAIPieces++ ] = board->Pieces[i];
if (board->Pieces[i].Type == opponentColor)
opponentPieces[ numberOfOpponentPieces++ ] = board->Pieces[i];
}
} // End of pieces gathering for-loop
//========================================================================//
Move possibleMoves[numberOfAIPieces * 4];
int numberOfPossibleMoves = 0;
// A buffer to temporarily store the moves so they can be evaluated.
PtrMove buffer[4];
for(int i = 0; i < 4; i++)
buffer[i] = (PtrMove) calloc( 1, sizeof(Move) ); //initializes address to NULL values
for (int i = 0; i < numberOfAIPieces; i++)
{
// Enters when the function does return targets
if (IdentifyAndHighlightTargets(AIPieces[i].Cell, buffer, AIColor, board, TRUE))
{
// If a target exists, it stores all the data in the PossibleMoves
// array
for (int j = 0; j < 4; j++)
{
if ( (*buffer[j]).TargetCell != NULL )
{
possibleMoves[numberOfPossibleMoves] = *buffer[j];
possibleMoves[numberOfPossibleMoves].movePriority = 0;
numberOfPossibleMoves++;
}
}
}
} // End of PossibleMoves calculating for-loop
//========================================================================//
Move opponentMoves[numberOfOpponentPieces * 4];
int numberOfOpponentMoves = 0;
// Resetting the buffer
for(int i = 0; i < 4; i++)
buffer[i] = (PtrMove) calloc( 1, sizeof(Move) ); //initializes address to NULL values
for (int i = 0; i < numberOfOpponentPieces; i++)
{
// Enters when the function does return targets
if (IdentifyAndHighlightTargets(opponentPieces[i].Cell, buffer, opponentColor, board, TRUE))
{
// If a target exists, it stores all the data in the PossibleMoves array
for (int j = 0; j < 4; j++)
{
if ( (*buffer[j]).TargetCell != NULL )
{
opponentMoves[numberOfOpponentMoves++] = *buffer[j];
}
}
}
} // End of OpponentMoves calculating for-loop
//========================================================================//
Move goodMoves[numberOfPossibleMoves];
int numberOfGoodMoves = 0;
// Opponent based moves
for(int i = 0; i < numberOfOpponentMoves; i++)
{
if (opponentMoves[i].isJump)
{
for(int j = 0; j < numberOfPossibleMoves; j++)
{
if (possibleMoves[j].TargetCell == opponentMoves[i].TargetCell &&
opponentMoves[i].TargetCell->IsOccupied == FALSE)
{
goodMoves[numberOfGoodMoves] = possibleMoves[j];
goodMoves[numberOfGoodMoves].movePriority = HIGH_PRIORITY;
numberOfGoodMoves++;
}
}
}
}
// A bool for whether the move is safe or not
int safe = TRUE;
// Jump moves
for(int i = 0; i < numberOfPossibleMoves; i++)
{
if (possibleMoves[i].isJump)
{
safe = TRUE;
for(int j = 0; j < numberOfOpponentMoves; j++)
{
if ( (possibleMoves[i].TargetCell == opponentMoves[j].TargetCell && // Tests to see if the target cell can be jumped over by an opponent's piece
possibleMoves[i].JumpedCell != opponentMoves[j].CurrentCell) &&// and whether that cell is the jumped piece's target cell or not
!(possibleMoves[i].TargetCell->Column == 7 || // All of this checks whether the move finishes on the edge of the board
possibleMoves[i].TargetCell->Column == 0 || // since those are safe spots and should not make the safe bool false
possibleMoves[i].TargetCell->Row == 7 ||
possibleMoves[i].TargetCell->Row == 0 ) )
{
safe = FALSE;
break;
}
}
if (safe)
{
goodMoves[numberOfGoodMoves] = possibleMoves[i];
goodMoves[numberOfGoodMoves].movePriority = MODERATE_PRIORITY;
numberOfGoodMoves++;
}
}
}
// Position based moves
for (int i = 0; i < numberOfPossibleMoves; i++)
{
if (possibleMoves[i].TargetCell->Column == 7 ||
possibleMoves[i].TargetCell->Column == 0 ||
possibleMoves[i].TargetCell->Row == 7 ||
possibleMoves[i].TargetCell->Row == 0)
{
goodMoves[numberOfGoodMoves] = possibleMoves[i];
goodMoves[numberOfGoodMoves].movePriority = LOW_PRIORITY;
numberOfGoodMoves++;
}
}
//========================================================================//
Move highPriorityMoves[numberOfGoodMoves];
Move moderatePriorityMoves[numberOfGoodMoves];
Move lowPriorityMoves[numberOfGoodMoves];
int numberOfHighPriorityMoves = 0;
int numberOfModeratePriorityMoves = 0;
int numberOfLowPriorityMoves = 0;
for(int i = 0; i < numberOfGoodMoves; i++)
{
if(goodMoves[i].movePriority == HIGH_PRIORITY)
highPriorityMoves[numberOfHighPriorityMoves++] = goodMoves[i];
if(goodMoves[i].movePriority == MODERATE_PRIORITY)
moderatePriorityMoves[numberOfModeratePriorityMoves++] = goodMoves[i];
if(goodMoves[i].movePriority == LOW_PRIORITY)
lowPriorityMoves[numberOfLowPriorityMoves++] = goodMoves[i];
}
//========================================================================//
// Delay
delay(1500);
int moveIndex;
if(numberOfHighPriorityMoves != 0)
{
// Randomly select a move
moveIndex = rand() % numberOfHighPriorityMoves;
// Moves the piece
MovePiece(&highPriorityMoves[moveIndex], AIColor, board, TRUE);
}
else if(numberOfModeratePriorityMoves != 0)
{
// Randomly select a move
moveIndex = rand() % numberOfModeratePriorityMoves;
// Moves the piece
MovePiece(&moderatePriorityMoves[moveIndex], AIColor, board, TRUE);
}
else if(numberOfLowPriorityMoves != 0)
{
// Randomly select a move
moveIndex = rand() % numberOfLowPriorityMoves;
// Moves the piece
MovePiece(&lowPriorityMoves[moveIndex], AIColor, board, TRUE);
}
else if(numberOfGoodMoves != 0)
{
// Randomly select a move
moveIndex = rand() % numberOfGoodMoves;
// Moves the piece
MovePiece(&goodMoves[moveIndex], AIColor, board, TRUE);
}
else
{
// Randomly select a move
moveIndex = rand() % numberOfPossibleMoves;
// Moves the piece
MovePiece(&possibleMoves[moveIndex], AIColor, board, TRUE);
}
}
int NoOfPieces(int * redPieces, int * bluePieces, PtrBoard board)
{
for ( int i = 0; i < PIECES_COUNT; i++)
{
if ( board->Pieces[i].Type == BLUE && board->Pieces[i].State == OnBoard )
(*bluePieces)++;
if ( board->Pieces[i].Type == RED && board->Pieces[i].State == OnBoard )
(*redPieces)++;
}
}
int PiecesLeft(PtrBoard board)
{
int bluePieces = 0, redPieces = 0;
NoOfPieces(&redPieces, &bluePieces, board);
return bluePieces && redPieces;
}
int PlayerMoveNotPossible(int turn, PtrBoard board)
{
Piece playerPieces[ PIECES_COUNT/2 ];
// Holds the number of pieces for use in for loops
int numberOfPlayerPieces = 0;
// Goes through all the pieces on the board
for (int i = 0; i < (PIECES_COUNT); i++)
{
// Checks to see whom the piece belongs to and whether it is on the
// the board, that is not removed or "jumped over".
if (board->Pieces[i].State == OnBoard)
{
if (board->Pieces[i].Type == turn)
playerPieces[ numberOfPlayerPieces++ ] = board->Pieces[i];
}
} // End of pieces gathering for-loop
int numberOfPossibleMoves = 0;
// A buffer to temporarily store the moves so they can be evaluated.
PtrMove buffer[4];
for(int i = 0; i < 4; i++)
buffer[i] = (PtrMove) calloc( 1, sizeof(Move) ); //initializes address to NULL values
for (int i = 0; i < numberOfPlayerPieces; i++)
{
// Enters when the function does return targets
if (IdentifyAndHighlightTargets(playerPieces[i].Cell, buffer, turn, board, TRUE))
{
// If a target exists, it stores all the data in the PossibleMoves
// array
for (int j = 0; j < 4; j++)
{
if ( (*buffer[j]).TargetCell != NULL )
{
numberOfPossibleMoves++;
}
}
}
} // End of PossibleMoves calculating for-loop
if (numberOfPossibleMoves == 0) return TRUE;
else return FALSE;
}
int PiecesLeftForPlayer(int turn, PtrBoard board)
{
int bluePieces = 0, redPieces = 0;
NoOfPieces(&redPieces, &bluePieces, board);
if (turn == RED) return redPieces;
else return bluePieces;
}
int GameOver(int *winner,int turn, PtrBoard board)
{
if(PlayerMoveNotPossible(turn, board))
{
if(PiecesLeftForPlayer(turn, board) != 0)
{
// If there are pieces left, then that means no pieces can move so
// we draw the game
gameState = Draw;
*winner = NULL;
return TRUE;
}
else
{
// there is a winner so we set things accordingly
gameState = Win;
*winner = (turn == BLUE) ? RED : BLUE;
return TRUE;
}
}
// the game is not over yet
return FALSE;
}
#endif /* PIECE_H */