-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel3.cc
234 lines (205 loc) · 6.94 KB
/
level3.cc
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
#include "level3.h"
#include "ai.h"
#include "grid.h"
#include "piece.h"
#include <stdlib.h>
#include <vector>
#include "queen.h"
using namespace std;
Level3::Level3(Grid* g, bool colour): AI(g, colour) {} // ctor
void Level3::randomMove() {
// random move just like Level1
//choose a random piece
Piece* p = getRandPiece();
int size = p->legalMoves.size();
//random legal position to move to
Cell* c = p->legalMoves[genRandIndexInRange(0, size)];
//get all necessary coordinates
int pieceX = p->getX();
int pieceY = p->getY();
int cellX = c->getX();
int cellY = c->getY();
//move the piece
try {
g->move(pieceX, pieceY, cellX, cellY);
if (p->getType() == 'P' and cellY == 0) {
p->setTakenOut(g->moveCount() -1);
Piece *newPiece = new Queen('Q');
g->place(newPiece, cellX, cellY);
g->updateRecord("Q");
g->reAttachAll();
} else if (p->getType() == 'p' and cellY == 7) {
p->setTakenOut(g->moveCount() -1);
Piece *newPiece = new Queen('q');
g->place(newPiece, cellX, cellY);
g->updateRecord("q");
g->reAttachAll();
}
}
catch (const char* msg) {
randomMove();
}
}
void Level3::makeMove() {
int colourInt1, colourInt2 = 0; // AI's opponent's colour
if (colour == 0) { // AI's colour
colourInt1 = 1;
colourInt2 = 2;
} else {
colourInt1 = 2;
colourInt2 = 1;
}
bool found = false; // did we find anythying to move
bool breakOutOfMainLoop = false;
bool breakOutOfForLoop = false;
vector <Piece*> *opponentPieces = nullptr;
bool savedMyPiece = false;
// select the elements of the opponent's Pieces one-by-one and for each, check if any of its legalMoves has any myPiece within attack vicinity. If I find something, move myPiece to safety.
unsigned int k = 0;
if (colourInt2 == 1) {
opponentPieces = &(g->blackPieces);
} else if (colourInt2 == 2) {
opponentPieces = &(g->whitePieces);
}
while (k < opponentPieces->size()) {
if (breakOutOfMainLoop == true) {
break;
}
Piece *opponentPiece = (*opponentPieces)[k];
for (unsigned int j = 0; j < (opponentPiece->attackRange).size(); ++j) {
if ((j == opponentPiece->attackRange.size()-1) && ((opponentPiece->attackRange[j])->getOccupiedState() != colourInt2)) {
if (k == opponentPieces->size()-1) {
breakOutOfMainLoop = true;
}
break;
}
if (opponentPiece->attackRange[j]->getOccupiedState() == colourInt2) { // AI under risk from opponent
Cell *c = opponentPiece->attackRange[j]; // Cell on which my threatened piece is
Piece *myPieceOnCell = c->returnPiece(); // My (AI's) piece under threat
//AI piece under threat
found = true;
// If my threatened piece can attack and capture the predator (opponent's piece that threatens me)
for (unsigned int d=0; d < (myPieceOnCell->attackRange).size(); ++d) {
if (((myPieceOnCell->attackRange[d])->getX() == opponentPiece->getX()) &&
((myPieceOnCell->attackRange[d])->getY() == opponentPiece->getY())) {
//trying to take on predator
g->move(c->getX(), c->getY(), opponentPiece->getX(), opponentPiece->getY());
//successfully destroyed predator
if (myPieceOnCell->getType() == 'P' and opponentPiece->getY() == 0) {
myPieceOnCell->setTakenOut(g->moveCount() -1);
Piece *newPiece = new Queen('Q');
g->place(newPiece, myPieceOnCell->getX(), myPieceOnCell->getY());
g->updateRecord("Q");
g->reAttachAll();
} else if (myPieceOnCell->getType() == 'p' and opponentPiece->getY() == 7) {
myPieceOnCell->setTakenOut(g->moveCount() -1);
Piece *newPiece = new Queen('q');
g->place(newPiece, myPieceOnCell->getX(), myPieceOnCell->getY());
g->updateRecord("q");
g->reAttachAll();
}
breakOutOfForLoop = true;
breakOutOfMainLoop = true;
savedMyPiece = true;
break;
}
}
if (breakOutOfForLoop == true) {
break;
}
randomMove();
savedMyPiece = true;
try {
int stopEnough = 0;
bool broken = 0;
for (unsigned int h = 0; h < (opponentPiece->attackRange).size(); ++h) {
if ((myPieceOnCell->getX() == ((opponentPiece->attackRange[h])->getX())) && (myPieceOnCell->getY() ==
((opponentPiece->attackRange[h])->getY()))) {
// random move wasn't good enough; still under threat; undoing move
g->undo();
// again trying another randomMove()
randomMove();
breakOutOfMainLoop = true;
h = 0;
++stopEnough;
if (stopEnough >= 30) {
break;
broken = 1;
}
}
}
if (broken == 1) {
breakOutOfMainLoop = true;
break;
}
}
catch (...) {
breakOutOfMainLoop = true;
break;
}
}
}
++k;
}
if (found == 0) {
randomMove();
savedMyPiece = true;
}
breakOutOfMainLoop = false;
found = 0;
if (savedMyPiece == false) {
//select the elements of the myPieces vector one-by-one and for each, check if any of its legalMoves has an opponent piece that can be captured.
unsigned int i = 0;
while (i < myPieces->size()) {
if (breakOutOfMainLoop == true) {
break;
}
Piece *p = (*myPieces)[i]; // myPieces is set in the AI superclass
for (unsigned int j = 0; j < (p->legalMoves).size(); ++j) {
if ((j == p->legalMoves.size()-1) && ((p->legalMoves[j])->getOccupiedState() != colourInt1)) {
if (i == myPieces->size()-1) {
breakOutOfMainLoop = true;
}
break;
}
if (p->legalMoves[j]->getOccupiedState() == colourInt1) {
Cell *c = p->legalMoves[j];
int pieceX = p->getX();
int pieceY = p->getY();
int cellX = c->getX();
int cellY = c->getY();
found = true;
try {
g->move(pieceX, pieceY, cellX, cellY);
if (p->getType() == 'P' and cellY == 0) { // for replacing the pawn at the end
p->setTakenOut(g->moveCount() -1);
Piece *newPiece = new Queen('Q');
g->place(newPiece, cellX, cellY);
g->updateRecord("Q");
g->reAttachAll();
} else if (p->getType() == 'p' and cellY == 7) {
p->setTakenOut(g->moveCount() -1);
Piece *newPiece = new Queen('q');
g->place(newPiece, cellX, cellY);
g->updateRecord("q");
g->reAttachAll();
}
breakOutOfMainLoop = true; // break out of outer loop
break; // break out of inner loop
}
catch (...) {
if (j == p->legalMoves.size()-1) {
break; // get out of this inner loop
} else {
continue;
}
}
}
}
++i;
}
if (found == 0) {
randomMove();
}
}
}