-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
287 lines (266 loc) · 8.6 KB
/
Board.cpp
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
#include "Board.h"
#include "Pawn.h"
#include "Rook.h"
#include "Knight.h"
#include "Bishop.h"
#include "Queen.h"
#include "King.h"
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
Board::Board()
{
Figure blank;
for (int row=0; row<8; row++)
{
for (int col=0; col<12; col++)
{
board[row][col] = blank;
}
}
}
void Board::set_field(int row, int col, Figure toSet)
{
assert(row>=0 && row<=8 && col>=0 && col<=12);
board[row][col] = toSet;
}
Figure* Board::get_field(int row, int col)
{
return &board[row][col];
}
void Board::print_board()const
{
for (int row=7; row>=0; row--)
{
cout << endl << endl << endl << endl << endl << endl;
cout << " " << row + 1 << " ";
for (int col=0; col<8; col++)
{
board[row][col].print();
cout << " ";
if(board[row][col]=="Free")
cout << " . ";
}
}
cout << endl << endl << endl;
cout << " A B C D E F G H";
cout << endl << endl << endl << endl << endl;
}
void Board::print_taken()const
{
bool flag = true;
cout << "Taken figures: " << endl;
for (int col=8; col<12 && flag; col++)
{
for (int row=7; row>=0 && flag; row--)
{
board[row][col].print();
if (!(board[row][col]=="Free"))
cout << endl;
else
flag = false;
}
}
cout << endl;
}
void Board::start()
{
Pawn white_pawn(1), black_pawn(0);
Rook white_rook(1), black_rook(0);
Knight white_knight(1), black_knight(0);
Bishop white_bishop(1), black_bishop(0);
Queen white_queen(1), black_queen(0);
King white_king(1), black_king(0);
white_king.unmoved = true;
black_king.unmoved = true;
for (int col=0; col<8; col++)
{
board[6][col] = black_pawn;
}
for (int col=0; col<8; col++)
{
board[1][col] = white_pawn;
}
board[7][1] = black_rook ; board[7][6] = black_rook ;
board[0][1] = white_rook ; board[0][6] = white_rook ;
board[7][2] = black_knight; board[7][5] = black_knight;
board[0][2] = white_knight; board[0][5] = white_knight;
board[7][0] = black_bishop; board[7][7] = black_bishop;
board[0][0] = white_bishop; board[0][7] = white_bishop;
board[7][3] = black_queen ;
board[0][3] = white_queen ;
board[7][4] = black_king ;
board[0][4] = white_king ;
board[7][4].unmoved = true;
board[0][4].unmoved = true;
board[7][7].unmoved = true;
board[0][7].unmoved = true;
board[7][0].unmoved = true;
board[0][0].unmoved = true;
}
bool Board::isFree(int row, int col)
{
return *get_field(row,col)=="Free";
}
bool Board::move(int row, int col, int newRow, int newCol)
{
Figure toMove = *get_field(row, col);
if (row<0 || row>7 || col<0 || col>7 || newRow<0 || newRow>7 || newCol<0 || newCol>7 || toMove=="Free" || (row==newRow && col==newCol))
{
cout << "Illegal move. No action taken." << endl;
return false;
}
else
{
if (toMove=="King ") {King king(toMove.getColor().isWhite()); return king.move(row,col,newRow,newCol,*this);}
if (toMove=="Queen ") {Queen queen(toMove.getColor().isWhite()); return queen.move(row,col,newRow,newCol,*this);}
if (toMove=="Bishop") {Bishop bishop(toMove.getColor().isWhite()); return bishop.move(row,col,newRow,newCol,*this);}
if (toMove=="Knight") {Knight knight(toMove.getColor().isWhite()); return knight.move(row,col,newRow,newCol,*this);}
if (toMove=="Rook ") {Rook rook(toMove.getColor().isWhite()); return rook.move(row,col,newRow,newCol,*this);}
if (toMove=="Pawn ") {Pawn pawn(toMove.getColor().isWhite()); return pawn.move(row,col,newRow,newCol,*this);}
}
}
void Board::announce(int row, int col, int newRow, int newCol)
{
switch (col)
{
case 0: cout << "A";break;
case 1: cout << "B";break;
case 2: cout << "C";break;
case 3: cout << "D";break;
case 4: cout << "E";break;
case 5: cout << "F";break;
case 6: cout << "G";break;
case 7: cout << "H";break;
}
cout << row+1 << " to ";
switch (newCol)
{
case 0: cout << "A";break;
case 1: cout << "B";break;
case 2: cout << "C";break;
case 3: cout << "D";break;
case 4: cout << "E";break;
case 5: cout << "F";break;
case 6: cout << "G";break;
case 7: cout << "H";break;
}
cout << newRow+1 << endl;
}
void Board::take(int row, int col)
{
bool flag = false;
for (int j=8; j<=12; j++)
{
if (flag) break;
for (int i=7; i>=0; i--)
{
if (this->isFree(i,j))
{
board[i][j] = board[row][col];
flag = true;
break;
}
}
}
board[row][col].setName("Free");
}
void Board::game()
{
cout << "Game started." << endl << "Enter moves like this: 'A2 A3' or 'a2 a3'. " << endl << "Enter 'A0 A0' to end game. " << endl;
start();
print_board();
char x1, x2;
int row, col, newRow, newCol;
while (!ended())
{
cin >> x1 >> row >> x2 >> newRow;
if ((x1 == 'a' || x1 == 'A') && (x2 == 'a' || x2 == 'A') && row == 0 && newRow == 0)
break;
row--;
newRow--;
switch(x1)
{
case 'a': col = 0;break;
case 'b': col = 1;break;
case 'c': col = 2;break;
case 'd': col = 3;break;
case 'e': col = 4;break;
case 'f': col = 5;break;
case 'g': col = 6;break;
case 'h': col = 7;break;
case 'A': col = 0;break;
case 'B': col = 1;break;
case 'C': col = 2;break;
case 'D': col = 3;break;
case 'E': col = 4;break;
case 'F': col = 5;break;
case 'G': col = 6;break;
case 'H': col = 7;break;
}
switch(x2)
{
case 'a': newCol = 0;break;
case 'b': newCol = 1;break;
case 'c': newCol = 2;break;
case 'd': newCol = 3;break;
case 'e': newCol = 4;break;
case 'f': newCol = 5;break;
case 'g': newCol = 6;break;
case 'h': newCol = 7;break;
case 'A': newCol = 0;break;
case 'B': newCol = 1;break;
case 'C': newCol = 2;break;
case 'D': newCol = 3;break;
case 'E': newCol = 4;break;
case 'F': newCol = 5;break;
case 'G': newCol = 6;break;
case 'H': newCol = 7;break;
}
if (move(row,col,newRow,newCol))
{
print_board();
print_taken();
}
}
cout << "Game ended. " << endl;
}
bool Board::ended()
{
for (int row=7; row>=0; row--)
{
for (int col=8; col<12; col++)
{
if (board[row][col] == "King ")
{
if (board[row][col].getColor().isWhite())
cout << "Black wins. " << endl;
if (!board[row][col].getColor().isWhite())
cout << "White wins. " << endl;
return true;
}
}
}
return false;
}
bool Board::isAttacked(int newRow, int newCol, bool white)
{
bool flag = false;
for (int i=0; i<8 && !flag; i++)
{
for (int j=0; j<8 && !flag; j++)
{
if (board[i][j].getColor().isWhite() == white)
{
if (board[i][j]=="King ") {King king(board[i][j].getColor().isWhite()); flag = king.canAttack(i,j,newRow,newCol,*this);}
if (board[i][j]=="Queen ") {Queen queen(board[i][j].getColor().isWhite()); flag = queen.canAttack(i,j,newRow,newCol,*this);}
if (board[i][j]=="Bishop") {Bishop bishop(board[i][j].getColor().isWhite()); flag = bishop.canAttack(i,j,newRow,newCol,*this);}
if (board[i][j]=="Knight") {Knight knight(board[i][j].getColor().isWhite()); flag = knight.canAttack(i,j,newRow,newCol,*this);}
if (board[i][j]=="Rook ") {Rook rook(board[i][j].getColor().isWhite()); flag = rook.canAttack(i,j,newRow,newCol,*this);}
if (board[i][j]=="Pawn ") {Pawn pawn(board[i][j].getColor().isWhite()); flag = pawn.canAttack(i,j,newRow,newCol,*this);}
}
}
}
return flag;
}