-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.cpp
More file actions
219 lines (192 loc) · 6.15 KB
/
Copy pathPiece.cpp
File metadata and controls
219 lines (192 loc) · 6.15 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
215
216
217
218
219
// Bryan Liu (chl312), Dept. of Computing, Imperial College London
/* Implementation for abstract class Piece - info in Piece.hpp
(Constructor, Deconstructor and common methods for extending classes)
*/
#include "pch.h"
#include "Piece.hpp"
Piece::Piece(bool isWhitePlayer)
{
_isWhitePlayer = isWhitePlayer;
}
Piece::~Piece()
{
}
/* Piece.confirmMove():
set isFirstMove false when called as it has moved at least once
*/
void Piece::confirmMove()
{
isFirstMove = false;
}
// Piece.isWhitePlayer() post-cond.: return if this piece belongs to White p.
bool Piece::isWhitePlayer()
{
return (_isWhitePlayer);
}
/* Piece.isKing() post-cond.: return true if the piece is a King
(represented by _isKing - only set to true by King constructor)
*/
bool Piece::isKing()
{
return _isKing;
}
/* Piece.isFriendly()
Pre-cond.: that is a valid reference to a Piece
Post-cond.: return true if this and that Piece belongs to same player
*/
bool Piece::isFriendly(Piece* that)
{
return this->isWhitePlayer() == that->isWhitePlayer();
}
// Piece.playerToString() Post-cond.: return wstring rep. of the piece's player
wstring Piece::playerToString()
{
return _isWhitePlayer ? wstring(_T("White's")) : wstring(_T("Black's"));
}
/* Piece::isSame_ ()
Pre-cond.: sourceFileRank, destFileRank valid file & rank representation
Post-cond.: return true if sourceFileRank and destFileRank is on the same
file/ rank/ diagonal respectively
*/
bool Piece::isSameFile(wstring sourceFileRank, wstring destFileRank)
{
return sourceFileRank.at(ChessInfo::FILE_INDEX) ==
destFileRank.at(ChessInfo::FILE_INDEX);
}
bool Piece::isSameRank(wstring sourceFileRank, wstring destFileRank)
{
return sourceFileRank.at(ChessInfo::RANK_INDEX) ==
destFileRank.at(ChessInfo::RANK_INDEX);
}
bool Piece::isSameDiagonal(wstring sourceFileRank, wstring destFileRank)
{
return abs(sourceFileRank.at(ChessInfo::FILE_INDEX) -
destFileRank.at(ChessInfo::FILE_INDEX)) ==
abs(sourceFileRank.at(ChessInfo::RANK_INDEX) -
destFileRank.at(ChessInfo::RANK_INDEX));
}
/* Piece.noVerticalObstruction()
Pre-cond.: sourceFileRank, destFileRank valid file & rank representation
board a valid, existing reference to map rep. of the board
Post-cond.: return true if there are no pieces on intermediate ranks
on the board
*/
bool Piece::noVerticalObstruction(wstring sourceFileRank, wstring destFileRank, map<wstring, Piece*>* board)
{
TCHAR sourceFile = sourceFileRank.at(ChessInfo::FILE_INDEX);
TCHAR sourceRank = sourceFileRank.at(ChessInfo::RANK_INDEX);
TCHAR destRank = destFileRank.at(ChessInfo::RANK_INDEX);
TCHAR lowRank = (sourceRank < destRank) ? sourceRank : destRank;
TCHAR highRank = (sourceRank < destRank) ? destRank : sourceRank;
for (TCHAR i = lowRank + ChessInfo::EXCLUSIVE_SHIFT; i < highRank; i++)
{
wstring between({ sourceFile, i });
try
{
auto temp = board->at(between);
UNREFERENCED_PARAMETER(temp);
return false;
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
// No piece for this file & rank, no action required
}
}
return true;
}
/* Piece.noVerticalObstruction()
Pre-cond.: sourceFileRank, destFileRank valid file & rank representation
board a valid, existing reference to map rep. of the board
Post-cond.: return true if there are no pieces on intermediate files
on the board
*/
bool Piece::noHorizontalObstruction(wstring sourceFileRank, wstring destFileRank, map<wstring, Piece*>* board)
{
TCHAR sourceFile = sourceFileRank.at(ChessInfo::FILE_INDEX);
TCHAR sourceRank = sourceFileRank.at(ChessInfo::RANK_INDEX);
TCHAR destFile = destFileRank.at(ChessInfo::FILE_INDEX);
TCHAR lowFile = (sourceFile < destFile) ? sourceFile : destFile;
TCHAR highFile = (sourceFile < destFile) ? destFile : sourceFile;
for (TCHAR i = lowFile + ChessInfo::EXCLUSIVE_SHIFT; i < highFile; i++)
{
wstring between({ i, sourceRank });
try
{
auto temp = board->at(between);
UNREFERENCED_PARAMETER(temp);
return false;
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
// No piece for this file & rank, no action required
}
}
return true;
}
/* Piece.noVerticalObstruction()
Pre-cond.: sourceFileRank, destFileRank valid file & rank representation
board a valid, existing reference to map rep. of the board
Post-cond.: return true if there are no pieces on intermediate files
and ranks, which is on the same diagonal, on the board
*/
bool Piece::noDiagonalObstruction(wstring sourceFileRank, wstring destFileRank, map<wstring, Piece*>* board)
{
TCHAR sourceFile = sourceFileRank.at(ChessInfo::FILE_INDEX);
TCHAR destFile = destFileRank.at(ChessInfo::FILE_INDEX);
TCHAR lowFile = (sourceFile < destFile) ? sourceFile : destFile;
TCHAR highFile = (sourceFile < destFile) ? destFile : sourceFile;
TCHAR sourceRank = sourceFileRank.at(ChessInfo::RANK_INDEX);
TCHAR destRank = destFileRank.at(ChessInfo::RANK_INDEX);
TCHAR lowRank = (sourceRank < destRank) ? sourceRank : destRank;
TCHAR highRank = (sourceRank < destRank) ? destRank : sourceRank;
bool isPositiveSlope = (destFile - sourceFile) == (destRank - sourceRank);
wstring between;
for (TCHAR i = ChessInfo::EXCLUSIVE_SHIFT; i < (highFile - lowFile); i++)
{
if (isPositiveSlope)
{
between = { (TCHAR)(lowFile + i), (TCHAR)(lowRank + i) };
}
else {
between = { (TCHAR)(lowFile + i), (TCHAR)(highRank - i) };
}
try
{
auto temp = board->at(between);
UNREFERENCED_PARAMETER(temp);
return false;
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
// No piece for this file & rank, no action required
}
}
return true;
}
/* Piece.destExistFriendlyPiece()
Pre-cond.: destFileRank is a valid file & rank representation
Post-cond.: return true if piece on the dest file & rank is a friendly,
false otherwise (rivalry on dest or empty square)
*/
bool Piece::destExistFriendlyPiece(wstring destFileRank, map<wstring, Piece*>* board) {
try
{
if (isFriendly(board->at(destFileRank)))
{
return true;
}
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
// No piece for this file & rank, expect to return false
}
return false;
}
int Piece::Score()
{
return 1;
}