-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.cpp
More file actions
214 lines (184 loc) · 6.01 KB
/
Copy pathChessBoard.cpp
File metadata and controls
214 lines (184 loc) · 6.01 KB
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
#include "ChessBoard.h"
#include <cctype>
#include <stdio.h>
#include <iostream>
#include <vector>
#include "include/EmptyPiece.h"
#include "PieceException.h"
#include "MoveException.h"
#include "PathException.h"
#include "CaptureException.h"
ChessBoard::ChessBoard()
{
//ctor
m_board = IntializeBoard();//cpp
}
ChessBoard::ChessBoard(std::vector<ChessPiece*> putOnboard){
//ChessBoard();
m_board = IntializeBoard();
std::vector<ChessPiece*>::iterator iterator = putOnboard.begin();
while (iterator != putOnboard.end()){
position_t coordinates = (*iterator)->GetPosition();
m_board[coordinates.y][coordinates.x] = *iterator;
iterator++;
}
}
ChessBoard::~ChessBoard()
{
//dtor
delete m_board;
}
ChessPiece*** ChessBoard::IntializeBoard(){
ChessPiece*** board = new ChessPiece**[boardHeight];
for(int h = 0; h < boardHeight; h++){
board[h] = new ChessPiece*[boardWidth];
}
for (int h = 0; h < boardHeight; h++){
for(int w = 0; w < boardWidth;w++){
board[h][w] = new EmptyPiece(h,w);
}
}
return board;
}
//TODO: Should be passing in complext data
bool ChessBoard::MovePiece(int xs, int ys, int xnew, int ynew) {
ChessPiece* pieceMoving = m_board[ys][xs];
bool pieceMoved = false;
if (IsValidAction(pieceMoving, xnew, ynew)){
ChessPiece* newSquare = m_board[ynew][xnew];
m_board[ynew][xnew] = m_board[ys][xs];
//Piece Moved to new square
m_board[ynew][xnew]->SetPosition(xnew,ynew);
if(newSquare->GetSymbol() != '_' ){
newSquare->SetIsAvailable(false);
m_board[ys][xs] = new EmptyPiece(ys, xs);
} else{
newSquare->SetPosition(xs, ys);
m_board[ys][xs] = newSquare;
}
pieceMoved = true;
}
return pieceMoved;
}
bool ChessBoard::IsValidAction(ChessPiece* piece, int x, int y){
bool ret = true;
try {
if (!IsMoveAvailable(piece, position_t(x,y))){
throw MoveException();
}
//If both pieces on same side, move is invalid
if(AreSameColor(m_board[y][x]->GetSymbol(),piece->GetSymbol())){
throw PieceException();
}
if (!IsPathClear(piece,x,y)) {
throw PathException();
}
if(!CaptureAllowable(piece,x, y)){
throw CaptureException();
}
} catch (ChessException e) {
std::cout << e.what() << std::endl;
ret = false;
}
return ret;
}
ChessPiece*** ChessBoard::GetBoard(){
return m_board;
}
void ChessBoard::DisplayBoard(){
printf(" ");
for (int i = 0; i < boardHeight; i++){
printf("%d", i);
}
printf("\n");
for (int i = 0; i < boardHeight;i++){
printf("%d", i);
for(int j = 0; j < boardWidth; j++){
printf("%c", m_board[i][j]->GetSymbol());
}
printf("\n");
}
}
char ChessBoard::getCell(int x, int y){
return m_board[x][y]->GetSymbol();
}
bool ChessBoard::AreSameColor(char p1, char p2){
if (p1 == '_' || p2 == '_'){
return false; // one of the squares is empty
}
bool bothBlack = std::isupper(p1) && std::isupper(p2);
bool bothWhite = !std::isupper(p1) && !std::isupper(p2);
return bothBlack || bothWhite;
}
//Requirement: The x,y coordinates have been established to be a valid move for the piece
//TODO: Add a check so this no longer is the case
bool ChessBoard::IsPathClear(ChessPiece* piece, int x, int y){
//Knights don't have a limit
if (piece->GetSymbol() == 'k' ||piece->GetSymbol() == 'K' ){
return true;
}
//We weren't moving
if(piece->GetPosition().x == x && piece->GetPosition().y == y){
return false;
}
//If the difference is just 1 square, is fine
if (IsAdjacentSquare(piece->GetPosition(), position_t(x,y))){
return true;
}
//At this point we've established move is at least 2 squares away in some direction
bool isPathClear = true;
int modifier = 1;
position_t piecePos(piece->GetPosition());
int xmodified = piecePos.x;
int ymodified = piecePos.y;
while(xmodified != x || ymodified != y){
xmodified = piecePos.x;
ymodified = piecePos.y;
if (piecePos.x == x && piecePos.y != y){
ymodified = piecePos.y + (y - piecePos.y > 0 ? modifier : -1 * modifier);
} else if (piecePos.x != x && piecePos.y ==y){
xmodified = piecePos.x + (x - piecePos.x > 0 ? modifier : -1 * modifier);
} else {
xmodified = piecePos.x + (x - piecePos.x > 0 ? modifier : -1 * modifier);
ymodified = piecePos.y + (y - piecePos.y > 0 ? modifier : -1 * modifier);
}
if(m_board[ymodified][xmodified]->GetSymbol() !='_'){
isPathClear = false;
}
modifier++;
}
return isPathClear;
}
//Required Is already established x,y is one of the valid moves && not on same side
bool ChessBoard::CaptureAllowable(ChessPiece* piece,int x,int y){
/*
If piece is pawn && move is diagnollay, then valid only if square is not
*/
//Return true, because no special rules needed
if (std::toupper(piece->GetSymbol()) != 'P')
return true;
//Is a diagonal move so is valid for pawn
if (piece->GetPosition().x - x != 0 && m_board[y][x]->GetSymbol() != '_' ){
return true;
} else if (piece->GetPosition().x - x == 0 && m_board[y][x]->GetSymbol() == '_'){
return true; //Mean's pawn is moving forward
}
//Else is an invalid capture
return false;
}
bool ChessBoard::IsAdjacentSquare(position_t p1, position_t p2){
bool adjacentX = p2.x - p1.x <=1 && p2.x - p1.x >= -1;
bool adjacentY = p2.y - p1.y <=1 && p2.y - p1.y >= -1;
return adjacentX && adjacentY;
}
bool ChessBoard::IsMoveAvailable(ChessPiece* piece, position_t newpos){
bool ret = false;
std::vector<position_t> validMoves(piece->GetValidMoves());
for (std::vector<position_t>::iterator i = validMoves.begin(); i != validMoves.end(); i++ ){
if((*i) == newpos){
ret = true;
break;
}
}
return ret;
}