-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomputer.c
141 lines (115 loc) · 3.82 KB
/
computer.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
#include "headers/global.h"
#include "headers/move.h"
void selectRandomPawn(Content pawnsLayout[GAME_WIDTH][GAME_HEIGHT], SDL_Rect *posPawnToMove){
int nbPawns = 0, numPawn = 0, comptePion = 0, x = 0, y = 0;
nbPawns = countPawns(pawnsLayout, RED);
numPawn = (rand() % nbPawns) + 1;
//We go through the board
for(x=0; x<10; x++){
for(y=0; y<10; y++){
if(pawnsLayout[x][y] == RED_PAWN) {
comptePion++; //we count the number of pawns encountered
}
if(comptePion == numPawn && pawnsLayout[x][y] == RED_PAWN){ //if it is the number of the pawn randomly picked
//this is the one we move
posPawnToMove->x = x;
posPawnToMove->y = y;
}
}
}
}
bool eatPawnForComputer(Content pawnsLayout[GAME_WIDTH][GAME_HEIGHT], Content surroundingCells[5], SDL_Rect *pawnPos){
int i, moveToDo = 5;
// We look at the computer's pawn's surroundings and store the content
if(pawnPos->x > 0){ //left cell
surroundingCells[0] = pawnsLayout[pawnPos->x - 1][pawnPos->y];
}
if(pawnPos->x > 0 && pawnPos->y < 9) { //left front cell
surroundingCells[1] = pawnsLayout[pawnPos->x - 1][pawnPos->y + 1];
}
if(pawnPos->y < 9) { //front cell
surroundingCells[2] = pawnsLayout[pawnPos->x][pawnPos->y + 1];
}
if(pawnPos->x < 9 && pawnPos->y < 9) { //front right cell
surroundingCells[3] = pawnsLayout[pawnPos->x + 1][pawnPos->y + 1];
}
if(pawnPos->x < 9){ //right cell
surroundingCells[4] = pawnsLayout[pawnPos->x + 1][pawnPos->y];
}
for(i=0; i<5; i++){
if(surroundingCells[i] == BLACK_CITY){
moveToDo = i;
break;
}
if(surroundingCells[i] == BLACK_PAWN && moveToDo == 5) {
moveToDo = i;
}
}
// If an enemy is surrounding the computer's pawn, we kill it
switch(moveToDo){
case 0 :
pawnsLayout[pawnPos->x - 1][pawnPos->y] = RED_PAWN;
pawnsLayout[pawnPos->x][pawnPos->y] = EMPTY;
return TRUE;
break;
case 1 :
pawnsLayout[pawnPos->x - 1][pawnPos->y + 1] = RED_PAWN;
pawnsLayout[pawnPos->x][pawnPos->y] = EMPTY;
return TRUE;
break;
case 2 :
pawnsLayout[pawnPos->x][pawnPos->y + 1] = RED_PAWN;
pawnsLayout[pawnPos->x][pawnPos->y] = EMPTY;
return TRUE;
break;
case 3 :
pawnsLayout[pawnPos->x + 1][pawnPos->y + 1] = RED_PAWN;
pawnsLayout[pawnPos->x][pawnPos->y] = EMPTY;
return TRUE;
break;
case 4 :
pawnsLayout[pawnPos->x + 1][pawnPos->y] = RED_PAWN;
pawnsLayout[pawnPos->x][pawnPos->y] = EMPTY;
return TRUE;
break;
}
return FALSE;
}
bool moveComputerPawn(Content pawnsLayout[GAME_WIDTH][GAME_HEIGHT], Content surroundingCells[6], SDL_Rect *pawnPos, SDL_Rect *cityPos){
// We move towards the enemy city if possible
if(cityPos->x < pawnPos->x && surroundingCells[1] == EMPTY){
pawnsLayout[pawnPos->x - 1][pawnPos->y + 1] = RED_PAWN;
} else if(cityPos->x > pawnPos->x && surroundingCells[3] == EMPTY){
pawnsLayout[pawnPos->x + 1][pawnPos->y + 1] = RED_PAWN;
} else if(cityPos->x == pawnPos->x && surroundingCells[2]==EMPTY){
pawnsLayout[pawnPos->x][pawnPos->y + 1] = RED_PAWN;
} else return FALSE;
pawnsLayout[pawnPos->x][pawnPos->y] = EMPTY;
return TRUE;
}
void computerPawnAction(Content pawnsLayout[GAME_WIDTH][GAME_HEIGHT]){
int x, y;
bool couldEat = FALSE, correctMove = FALSE;
Content surroundingCells[5];
SDL_Rect posPawnToMove, enemyCity;
for(y=9; y>=0; y--){
for(x=0; x<10; x++){
if(pawnsLayout[x][y] == RED_PAWN){
posPawnToMove.x = x;
posPawnToMove.y = y;
if(eatPawnForComputer(pawnsLayout, surroundingCells, &posPawnToMove)){
return;
}
}
if(pawnsLayout[x][y] == BLACK_CITY){
enemyCity.x = x;
enemyCity.y = y;
}
}
}
// Here, no pawn could kill, so we move a random one
do{
selectRandomPawn(pawnsLayout, &posPawnToMove);
correctMove = moveComputerPawn(pawnsLayout, surroundingCells, &posPawnToMove, &enemyCity);
} while(!correctMove);
}