-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueen.cpp
More file actions
116 lines (101 loc) · 3.57 KB
/
queen.cpp
File metadata and controls
116 lines (101 loc) · 3.57 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
#include "queen.h"
Queen::Queen(QString _pos, enum Color _color, QString _name):Piece(_pos, _color,_name,'Q')
{
}
/**
* @brief Queen::isMoveLegal Queen movement is simply Rook+Bishop movement
* @param squareName QString
* @param pieceAtNewLocation Piece*
* @param arr Piece** representing the board
* @return bool
*/
bool Queen::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 Queen::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);
bool straightMovement = (rowDifference == 0 || colDifference == 0);
bool diagonallyMovement = (rowAbs == colAbs);
return straightMovement || diagonallyMovement;
}
bool Queen::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);
bool straightMovement = (rowDifference == 0 || colDifference == 0);
bool diagonallyMovement = (rowAbs == colAbs);
if(straightMovement){
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;
}
}
}else if(diagonallyMovement){
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;
}