Repository navigation
Expand file tree
/
Copy pathpieces.cpp
More file actions
75 lines (50 loc) · 2.24 KB
/
Copy pathpieces.cpp
File metadata and controls
75 lines (50 loc) · 2.24 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
#include <iostream>
#include "pieces.h"
Piece::Piece(char piecesymbol, PieceColor piececolor){
pieceSymbol = piecesymbol;
pieceColor = piececolor;
}
bool Pawn::isValidMove(int startX, int startY, int endX, int endY, Piece * targetPiece){
if (pieceColor == PieceColor::WHITE){
bool doubleMove = (startX == 1 && endX == 3 && startY == endY && targetPiece == nullptr);
bool normalMove = (startY == endY && (endX == startX +1) && targetPiece == nullptr);
bool captureMove = (endX == startX +1 && ((endY == startY +1)||(endY == startY -1))&& targetPiece != nullptr);
return doubleMove || normalMove || captureMove;
}
if (pieceColor == PieceColor:: BLACK){
bool doubleMove = (startX == 6 && endX == 4 && startY == endY && targetPiece == nullptr);
bool normalMove = (startY == endY && (endX == startX -1) && targetPiece == nullptr);
bool captureMove = (endX == startX -1 && ((endY == startY +1)||(endY == startY -1))&& targetPiece != nullptr);
return doubleMove || normalMove || captureMove;
}
return false;
}
bool Rook::isValidMove(int startX, int startY, int endX, int endY, Piece * targetPiece){
return ((startY == endY)|| (startX == endX));
}
bool Knight::isValidMove(int startX, int startY, int endX, int endY, Piece * targetPiece){
// horizontal
if ((endX == startX -2)||(endX == startX +2)){
return ((endY == startY -1)|| (endY == startY +1));
}
//vertical
if ((endY == startY-2)||(endY == startY +2)){
return ((endX == startX -1)|| (endX== startX +1));
}
return false;
}
bool Bishop::isValidMove(int startX, int startY, int endX, int endY, Piece * targetPiece){
return (abs(endX-startX) == abs(endY - startY));
}
bool Queen::isValidMove(int startX, int startY, int endX, int endY, Piece * targetPiece){
return ((startY == endY) || (startX == endX)) || (abs(endX - startX) == abs(endY - startY));
}
bool King::isValidMove(int startX, int startY, int endX, int endY, Piece * targetPiece){
if (abs(endX - startX) <= 1 && abs(endY - startY) <= 1){
return true;
}
if (abs(endX - startX) == 0 && abs(endY - startY) == 2 && !pieceMoved()) {
return true;
}
return false;
}