-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbishop.cpp
87 lines (70 loc) · 2.28 KB
/
bishop.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
#include "bishop.h"
Bishop::Bishop(QString _pos, enum Color _color, QString _name):Piece(_pos, _color,_name,'B')
{
}
/**
* @brief Bishop::isMoveLegal Bishop can move in any direction, but only diagonally.
* Important to check if any piece is blocking the move.
* @param squareName QString
* @param pieceAtNewLocation QString
* @param arr Piece** array representing the board
* @return bool
*/
bool Bishop::isMoveLegal(QString squareName, Piece* pieceAtNewLocation, Piece** arr){
if(isMovementLegal(squareName)){
if(isMovementBlocked(squareName, arr)){
return false;
} else {
if(pieceAtNewLocation){
if(pieceAtNewLocation->getColor() == color){
return false;
} else {
return true;
}
} else {
return true;
}
}
}
return false;
}
bool Bishop::isMovementBlocked(QString to, Piece** arr)
{
char currentColumn = currentPosition.at(0).toLatin1();
char currentRow = currentPosition.at(1).toLatin1();
char newColumn = to.at(0).toLatin1();
char newRow = to.at(1).toLatin1();
int rowDifference = newRow-currentRow;
int colDifference = newColumn-currentColumn;
int rowAbs = abs(rowDifference);
for(int i=1;i<rowAbs;i++){
QString tempPosition;
if(colDifference > 0){
tempPosition.append(currentColumn+i);
} else{
tempPosition.append(currentColumn-i);
}
if(rowDifference > 0){
tempPosition.append(currentRow+i);
} else {
tempPosition.append(currentRow-i);
}
int tempIndex = convertMoveToIndex(tempPosition);
if(arr[tempIndex]){
return true;
}
}
return false;
}
bool Bishop::isMovementLegal(QString to){
char currentColumn = currentPosition.at(0).toLatin1();
char currentRow = currentPosition.at(1).toLatin1();
char newColumn = to.at(0).toLatin1();
char newRow = to.at(1).toLatin1();
int rowDifference = newRow-currentRow;
int colDifference = newColumn-currentColumn;
int rowAbs = abs(rowDifference);
int colAbs = abs(colDifference);
//If these two values are the same, the move is diagonally
return rowAbs == colAbs;
}