-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
363 lines (329 loc) · 8.33 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
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
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#ifndef _WIN32
#include "Terminal.h"
#endif // !_WIN32
#include "Board.h"
#include "CreatePiece.h"
#include "Piece.h"
#include <algorithm>
#include <string>
using std::cout;
using std::endl;
using std::map;
using std::vector;
using std::pair;
using std::find;
using std::make_pair;
using std::string;
// DO NOT MODIFY THIS FUNCTION!!!! //
Board::Board( void ){}
// Deep copy of the board when initializing
Board::Board( const Board& b ) {
_occ.clear();
for(map<pair<char, char>, Piece*>::const_iterator it = b.occ().cbegin(); it != b.occ().cend(); ++it) {
Piece *p = create_piece(it->second->to_ascii());
_occ[it->first] = p;
}
}
// Board Destructor
Board::~Board() {
for(map<pair<char, char>, Piece*>::const_iterator it = _occ.cbegin(); it != _occ.cend(); ++it) {
delete (it->second);
}
}
// Returns const pointer of a Piece based on location on a board
const Piece* Board::operator()( pair< char , char > position ) const
{
if(_occ.find(position) != _occ.end()) {
// This position is occupied by a piece
Piece* my_pointer = (_occ.find(position))->second;
return my_pointer;
}
return NULL;
}
// Returns non-const pointer of a Piece based on location on a board
Piece* Board::get_piece_pointer ( pair< char, char > position ) const {
if(_occ.find(position) != _occ.end()) {
// This position is occupied by a piece
Piece* my_pointer = (_occ.find(position))->second;
return my_pointer;
}
return NULL;
}
// Add a piece to a specified location onto the board
bool Board::add_piece( pair< char , char > position , char piece_designator )
{
if(_occ.find(position) != _occ.end()) {
// Piece already exists
return false;
}
if(position.first < 'A' || position.first > 'H') {
// Wrong Letter
return false;
}
if(position.second < '1' || position.second > '8') {
// Wrong Number
return false;
}
// Ensures piece is one of the following characters
vector<char> pieces = {'k', 'K', 'q', 'Q', 'b', 'B', 'N', 'n', 'R',
'r', 'p', 'P', 'M', 'm'};
vector<char>::iterator it;
it = find(pieces.begin(), pieces.end(), piece_designator);
if(it == pieces.end()) {
return false;
}
// Create Piece
_occ[ position ] = create_piece( piece_designator );
return true;
}
// Checks path is clear, does not check if the
// Returns true if path is clear
// Returns true if not moving in a line
bool Board::path_is_clear(std::pair<char, char> start, std::pair<char, char> end) const {
int h_move = end.first - start.first;
int v_move = end.second - start.second;
// For counting reasons
int h_abs = std::abs(h_move) - 1;
int v_abs = std::abs(v_move) - 1;
pair<char, char> cur_pt = start;
// Checks for non-line shape
if(h_move != 0 && v_move != 0 && h_move != v_move) {
return true;
}
string dir;
// Checks vertical movement
if(h_move == 0) {
if(v_move > 0) {
for(int i = 0; i < v_abs; i++) {
cur_pt.second += 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
}
else {
for(int i = 0; i < v_abs; i++) {
cur_pt.second -= 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
}
return true;
}
// Checks horizontal movement
if(v_move == 0) {
if(h_move > 0) {
for(int i = 0; i < h_abs; i++) {
cur_pt.first += 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
} else {
for(int i = 0; i < h_abs; i++) {
cur_pt.first -= 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
}
return true;
}
if(h_move > 0) {
if(v_move > 0) {
dir = "ne";
} else {
dir = "se";
}
} else {
if(v_move > 0) {
dir = "nw";
} else {
dir = "sw";
}
}
// Checks for diagonal movement
if(dir == "ne") {
for(int i = 0; i < h_abs; i++) {
cur_pt.first += 1;
cur_pt.second += 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
return true;
}
else if(dir == "nw") {
for(int i = 0; i < h_abs; i++) {
cur_pt.first -= 1;
cur_pt.second += 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
return true;
}
else if(dir == "se") {
for(int i = 0; i < h_abs; i++) {
cur_pt.first += 1;
cur_pt.second -= 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
return true;
}
else if(dir == "sw") {
for(int i = 0; i < h_abs; i++) {
cur_pt.first -= 1;
cur_pt.second -= 1;
if(_occ.find(cur_pt) != _occ.end()) {
return false;
}
}
return true;
}
return false;
}
// Checks end position piece to see if it is empty or if it is the same color as the piece
// Adds piece to end location if true
// Returns false if same piece
bool Board::check_end_location(pair<char, char> start, pair<char, char> end) const {
// Movement for pawn
if(((_occ.find(start)->second->to_ascii()) == 'P') || ((_occ.find(start)->second->to_ascii()) == 'p')) {
if(abs(start.first - end.first) == 1 && abs(start.second - end.second) == 1) {
// Pawn tries to move diagonally but there is no piece there
if(_occ.find(end) == _occ.end()) {
return false;
}
else if(_occ.find(start)->second->is_white() == _occ.find(end)->second->is_white()) {
return false;
}
// Pawn tries to move diagnoally and there is a piece there
else {
return true;
}
}
else if(abs(start.second - end.second) == 1 || abs(start.second - end.second) == 2) {
// Pawn tries to move vertically but piece is there
if(_occ.find(end) != _occ.end()) {
return false;
}
// Pawn tries to move vertically and space is empty
else {
return true;
}
}
}
// End spot is empty, piece is able to move to spot
if(_occ.find(end) == _occ.end()) {
return true;
}
// Piece tries capturing piece of same color
if(_occ.find(start)->second->is_white() == _occ.find(end)->second->is_white()) {
return false;
}
// Piece tries capturing piece of different color
else {
return true;
}
return false;
}
// Moves the piece from the start position to the end position
void Board::execute_move(pair<char, char> start, pair<char, char> end) {
// delete end piece if it exists:
if(_occ[end] != NULL) {
delete _occ[end];
}
// Scenario for Pawn
if((_occ[start]->to_ascii() == 'P') || (_occ[start]->to_ascii() == 'p')) {
// Pawn Promotion to a Queen
if(end.second == '8') {
delete _occ[start];
_occ.erase(start);
_occ.erase(end);
add_piece(end, 'Q');
}
else if(end.second == '1') {
delete _occ[start];
_occ.erase(start);
_occ.erase(end);
add_piece(end, 'q');
}
else {
_occ[end] = _occ[start];
_occ.erase(start);
}
}
else {
// Assign start pointer as end location value
_occ[end] = _occ[start];
// Remove original location
_occ.erase(start);
}
}
// Clears the map so that a new map can be inserted
void Board::clear_board() {
// Code that should delete the pointers
// associated with the previous board
for(map<pair<char, char>, Piece*>::const_iterator it = _occ.cbegin(); it != _occ.cend(); ++it) {
delete it->second;
}
_occ.clear();
}
// Loops through map to see if there's either 'K' or 'k'
// If there's one each, return true, else return false
bool Board::has_valid_kings( void ) const
{
int black_king = 0;
int white_king = 0;
for(map<pair<char, char>, Piece*>::const_iterator it = _occ.cbegin();
it != _occ.cend(); ++it) {
if((it->second)->to_ascii() == 'K') {
white_king = white_king + 1;
}
else if((it->second)->to_ascii() == 'k') {
black_king = black_king + 1;
}
}
if(black_king == 1 && white_king == 1) {
return true;
}
else {
return false;
}
}
void Board::display( void ) const
{
// Display the board
cout << *this << endl;
}
// Deep copy of the board when assigning
Board& Board::operator=(const Board& b) {
_occ.clear();
for(map<pair<char, char>, Piece*>::const_iterator it = b.occ().cbegin(); it != b.occ().cend(); ++it) {
Piece *p = create_piece(it->second->to_ascii());
_occ[it->first] = p;
}
return *this;
}
// DO NOT MODIFY THIS FUNCTION!!!! //
std::ostream& operator << ( std::ostream& os , const Board& board )
{
for( char r='8' ; r>='1' ; r-- )
{
for( char c='A' ; c<='H' ; c++ )
{
const Piece* piece = board( pair< char , char >( c , r ) );
if( piece ) os << piece->to_ascii();
else os << '-';
}
os << std::endl;
}
return os;
}