-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
299 lines (248 loc) · 6.71 KB
/
Board.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "Board.h"
#include "ChessPiece.h"
using namespace Chess::ChessComponents::PlayField;
Board::Board( const int width, const int height )
: m_width ( width ),
m_height ( height )
{
initPiecesMap();
}
Board::~Board()
{
cout << "~Board()\n";
m_movesCountMap.clear();
m_piecesMap.clear();
for ( auto iter = m_whitePieces.begin(); iter != m_whitePieces.end(); ++ iter )
{
delete *iter;
}
for ( auto iter = m_blackPieces.begin(); iter != m_blackPieces.end(); ++ iter )
{
delete *iter;
}
}
Board::Board( const Board& other )
{
copy ( other );
}
void Board::seWidth( const int width )
{
m_width = width;
}
int Board::width() const
{
return m_width;
}
void Board::setHeight( const int height )
{
m_height = height;
}
int Board::height() const
{
return m_height;
}
void Board::copy( const Board& other )
{
m_whitePieces = other.m_whitePieces;
m_blackPieces = other.m_blackPieces;
m_piecesMap = other.m_piecesMap;
m_width = other.m_width;
m_height = other.m_height;
}
void Board::display()
{
int width = 0;
for ( Position pos( 0, 0 ); pos != Position ( m_width, m_height ); pos = nextPosition( pos ) )
{
if ( isValidPosition( pos ) )
{
m_piecesMap[ pos ] == 0 ? cout << "# " : cout << m_piecesMap [ pos ]->m_sigil << " ";
width++;
if ( width == m_width )
{
width = 0;
cout << endl;
}
}
else
{
break;
}
}
}
bool Board::isValidPosition( const Position& position ) const
{
return position.x() >= 0
&& position.x() < m_width
&& position.y() >= 0
&& position.y() < m_height;
}
bool Board::isPiece( const Position& position ) const
{
unordered_map< Position, ChessPiece* >::const_iterator iter = m_piecesMap.find( position );
if ( iter == m_piecesMap.end() )
{
return false;
}
return iter->second != 0;
}
bool Board::isObstacle( const Position& position, const Colour& colour ) const
{
unordered_map< Position, ChessPiece* >::const_iterator iter = m_piecesMap.find( position );
return isPiece( position ) && iter->second->colour() == colour;
}
void Board::addPiece( ChessPiece* piece )
{
if ( isValidPosition( piece->position() ) )
{
m_piecesMap[ piece->position() ] = piece;
piece->colour() == white ? m_whitePieces.push_back( piece ) : m_blackPieces.push_back( piece );
m_movesCountMap[ piece ] = 0;
}
// add it to map
// add it to colours vectors.
}
bool Board::movePiece( const Position& from, const Position& to )
{
if ( m_piecesMap[ from ] == 0
|| !this->isValidPosition( from )
|| !this->isValidPosition( to ) )
{
return false;
}
m_piecesMap[ from ]->setPosition( to );
m_piecesMap[ to ] = m_piecesMap[ from ];
m_piecesMap[ from ] = 0;
return true;
}
void Board::setWhitePieces( vector< ChessPiece* > pieces )
{
m_whitePieces = pieces;
}
vector< ChessPiece* > Board::whitePieces() const
{
return m_whitePieces;
}
void Board::setBlackPieces( vector< ChessPiece* > pieces )
{
m_blackPieces = pieces;
}
vector< ChessPiece* > Board::blackPieces() const
{
return m_blackPieces;
}
bool Board::placePieces( vector < ChessPiece* > pieces,
unsigned int index )
{
if ( index == pieces.size() )
{
return true;
}
vector< ChessPiece* >tmpPieces( index );
std::copy( pieces.begin(), pieces.begin() + index, tmpPieces.begin() ); //is this really the better way, obi-wan?
for ( int i = 0; i < this->width(); i++ )
{
for ( int j = 0; j < this->height(); j++ )
{
if ( m_piecesMap[ Position( i, j ) ] == 0 )
{
pieces[ index ]->setPosition( Position( i, j ) );
const bool threat = this->isThreat( pieces[ index ] );
const bool los = this->inLoS( Position( i, j ), tmpPieces );
if ( index == 0 || !( los
|| threat ) )
{
addPiece( pieces[ index ] );
if ( placePieces( pieces, ++index ) )
{
return true;
}
this->clearPosition( Position( i, j ) );
--index;
}
}
}
}
return false;
}
bool Board::isThreat( ChessPiece* piece )
{
vector < Position > tmp = piece->allowedMovements();
for ( unsigned int i = 0; i < tmp.size(); ++i )
{
if ( m_piecesMap[ tmp[ i ] ] != 0 )
{
// cout << "I see something at " << piece->getPosition() << endl;
return true;
}
}
return false;
}
bool Board::inLoS( const Position& position, vector < ChessPiece* > aVector ) const
{
if ( !aVector.empty() )
{
for ( unsigned int i = 0; i < aVector.size(); i++ )
{
vector < Position > helper = aVector[ i ]->allowedMovements();
for ( unsigned int j = 0; j < helper.size(); j++ )
{
if ( position == helper[ j ] )
{
// cout << "Something sees me. I am at " << position << endl;
return true;
}
}
}
}
return false;
}
Position Board::nextPosition( const Position& position )
{
if ( position.x() == m_width && position.y() == m_height )
{
return Position ( position.x() - 1, position.y() - 1 );
}
if ( position.x() == m_width - 1 && position.y() < m_height - 1 )
{
return Position( 0, position.y() + 1 );
}
return Position( position.x() + 1, position.y() );
}
void Board::clearPosition( const Position& position )
{
//cout << "Bye-bye, cruel world! Pick my body from " << position << endl;
m_piecesMap[ position ] = 0;
}
void Board::initPiecesMap()
{
for ( int i = 0; i < m_width; i++ )
{
for ( int j = 0; j < m_height; j++ )
{
m_piecesMap [ Position( i, j ) ] = 0;
}
}
}
ChessPiece* Board::pieceAt( const Position& position ) const
{
unordered_map< Position, ChessPiece* >::const_iterator iter = m_piecesMap.find( position );
if ( iter == m_piecesMap.end() )
{
return 0;
}
return iter->second;
}
void Board::setPassedMoves( ChessPiece* piece, int anInteger )
{
m_movesCountMap[ piece ] = anInteger;
}
int Board::passedMoves( ChessPiece* piece ) const
{
unordered_map< ChessPiece*, int >::const_iterator iter = m_movesCountMap.find( piece );
if ( iter == m_movesCountMap.end() )
{
return -1;
}
return iter->second;
}