-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrook.cpp
More file actions
97 lines (85 loc) · 2.78 KB
/
rook.cpp
File metadata and controls
97 lines (85 loc) · 2.78 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
#include "rook.h"
Rook::Rook(QString _pos, enum Color _color, QString _name):Piece(_pos, _color,_name,'R')
{
}
/**
* @brief Rook::isMoveLegal Rooks move in straight lines, like bishop only need to
* check if the movement is blocked by another piece.
* @param squareName QString
* @param pieceAtNewLocation Piece*
* @param arr Piece** array representing the board
* @return bool
*/
bool Rook::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 {
hasMoved = true;
return true;
}
} else {
hasMoved = true;
return true;
}
}
}
return false;
}
bool Rook::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);
int colAbs = abs(colDifference);
int squaresMoved = std::max(rowAbs, colAbs);
for(int i=1;i<squaresMoved;i++){
QString tempPosition;
//If row has not changed, increment column to see if path is blocked
if(rowDifference == 0){
if(colDifference > 0){
tempPosition.append(currentColumn+i);
} else{
tempPosition.append(currentColumn-i);
}
tempPosition.append(currentRow);
}
//If col has not changed, increment row to see if path is blocked
if(colDifference == 0){
tempPosition.append(currentColumn);
if(rowDifference > 0){
tempPosition.append(currentRow+i);
} else {
tempPosition.append(currentRow-i);
}
}
int tempIndex = convertMoveToIndex(tempPosition);
if(arr[tempIndex]){
return true;
}
}
return false;
}
bool Rook::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;
//If one of these values is 0, the movement is straight
return (rowDifference == 0 || colDifference == 0);
}
bool Rook::getHasMoved() const
{
return hasMoved;
}