-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cc
372 lines (332 loc) · 8.26 KB
/
board.cc
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/******************************************************************************
* board.cc
*
* Board class methods' implementation.
*
* Jeff A
* cs670
* pa2
*****************************************************************************/
#include <cstdlib>
#include <cstdio>
#include "board.h"
//============================================================================
// Public Methods //
//============================================================================
/**
* Default constructor
* Generates a random starting board with 8 queens
*/
Board::Board()
{
_cost = -1;
_neighbors = NULLPTR;
initBoard();
}
/**
* Copy constructor
*/
Board::Board(Board const& rhs)
{
_board = new unsigned char[N_CELLS];
for (char i = 0; i < N_CELLS; ++i)
_board[i] = rhs._board[i];
this->_cost = rhs._cost;
if (rhs._neighbors != NULLPTR) // copy other neighbors
{
this->_neighbors = new std::vector<Board>(*(rhs._neighbors));
}
else
{
this->_neighbors = NULLPTR;
}
}
/**
* Destructor
*/
Board::~Board()
{
delete[] _board;
if (_neighbors != NULLPTR)
delete _neighbors;
}
/**
* Assignment operator
*/
Board& Board::operator=(Board const& rhs)
{
if (this != &rhs)
{
for (char i = 0; i < N_CELLS; ++i) // copy board
_board[i] = rhs._board[i];
if (this->_neighbors != NULLPTR) // free these neighbors
{
delete this->_neighbors;
this->_neighbors = NULLPTR;
}
if (rhs._neighbors != NULLPTR) // copy other neighbors
{
this->_neighbors = new std::vector<Board>(*(rhs._neighbors));
}
this->_cost = rhs._cost;
}
return *this;
}
/**
* Get the cost of this board state (constant member)
*/
int Board::cost() const
{
return _cost;
}
/**
* Get a list of this state's successor states
*/
const std::vector<Board>* Board::successors()
{
if (_neighbors == NULLPTR)
{
_neighbors = new std::vector<Board>();
int i, j, p, prevp, r, c; // index i, index j, position p, row r, column c
// generate successor for every legal move for every queen
for (i = 0; i < N_CELLS; ++i)
{
if (_board[i] != NULLPTR)
{
// record row, column, and position
r = row(i);
c = col(i);
p = i;
// move up as far as possible
while (r > 0 && _board[pos(r - 1, c)] != 1)
{
prevp = p;
p = pos(--r, c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move down as far as possible
while (r < N_ROWS - 1 && _board[pos(r + 1, c)] != 1)
{
prevp = p;
p = pos(++r, c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move left as far as possible
while (c > 0 && _board[pos(r, c - 1)] != 1)
{
prevp = p;
p = pos(r, --c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move right as far as possible
while (c < N_COLS - 1 && _board[pos(r, c + 1)] != 1)
{
prevp = p;
p = pos(r, ++c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move up and left as far as possible
while (r > 0 && c > 0 && _board[pos(r - 1, c - 1)] != 1)
{
prevp = p;
p = pos(--r, --c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move up and right as far as possible
while (r > 0 && c < N_COLS - 1 && _board[pos(r - 1, c + 1)] != 1)
{
prevp = p;
p = pos(--r, ++c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move down and left as far as possible
while (r < N_ROWS - 1 && c > 0 && _board[pos(r + 1, c - 1)] != 1)
{
prevp = p;
p = pos(++r, --c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
r = row(i);
c = col(i);
p = i;
// move down and right as far as possible
while (r < N_ROWS-1 && c < N_COLS-1 && _board[pos(r + 1, c + 1)] != 1)
{
prevp = p;
p = pos(++r, ++c);
swap(_board[prevp], _board[p]);
_neighbors->push_back(Board(_board));
}
swap(_board[p], _board[i]);
}
}
}
return _neighbors;
}
/**
* Print the board to stdout (used for debugging)
*/
void Board::print() const
{
printf("\nboard address: %p\n", _board);
for (int i = 0; i < N_ROWS; ++i)
{
for (int j = 0; j < N_COLS; ++j)
{
printf("%-2d", _board[pos(i,j)]);
}
printf("\n");
}
printf("cost: %d\n\n", cost());
}
//============================================================================
// Private Methods //
//============================================================================
/*
* Create a new board with an array
* Restricted to private use only
*/
Board::Board(const unsigned char* board)
{
_board = new unsigned char[N_CELLS];
for (int i = 0; i < N_CELLS; ++i)
_board[i] = board[i];
_cost = evaluate(_board);
_neighbors = NULLPTR;
}
/*
* Initialize the board
*/
void Board::initBoard()
{
int i;
_board = new unsigned char[N_CELLS];
for (i = 0; i < N_QUEENS; ++i)
_board[i] = 1;
for (; i < N_CELLS; ++i)
_board[i] = 0;
shuffle();
_cost = evaluate(_board);
}
/*
* Get the row on a 2D board given a position in a 1D array
*/
int Board::row(const int& pos) const
{
return pos / N_ROWS;
}
/*
* Get the col on a 2D board given a position in a 1D array
*/
int Board::col(const int& pos) const
{
return pos % N_ROWS;
}
/*
* Get the position in the 1D array given row and column in 2D board
*/
int Board::pos(const int& row, const int& col) const
{
return row * N_COLS + col;
}
/*
* Swap element a with element b
*/
void Board::swap(unsigned char& a, unsigned char& b)
{
unsigned char tmp = a;
a = b;
b = tmp;
}
/*
* Shuffle the board (Fisher-Yates Shuffle algorithm)
*/
void Board::shuffle()
{
for (int i = N_CELLS - 1; i > 0; --i)
swap(_board[i], _board[rand() % (i + 1)]);
}
/*
* This is the h function for calculating the h value of board state.
* Calculate the cost of this board state by counting the number of pairs
* of queens endangering each other, directly or indirectly
*/
int Board::evaluate(const unsigned char* b)
{
int currCost = 0;
int i, r, c; // index, row, and column
unsigned char diag1Counts[2*N_QUEENS - 1];
unsigned char diag2Counts[2*N_QUEENS - 1];
unsigned char rowCounts[N_QUEENS];
unsigned char colCounts[N_QUEENS];
// zero out row and cols counts
for (i = 0; i < N_QUEENS; ++i)
{
rowCounts[i] = 0;
colCounts[i] = 0;
}
// zero out diagonal counts
for (i = 0; i < 2*N_QUEENS - 1; ++i)
{
diag1Counts[i] = 0;
diag2Counts[i] = 0;
}
// record counts along rows, cols, and both diagonals
for (i = 0; i < N_CELLS; ++i)
{
if (b[i])
{
r = row(i);
c = col(i);
diag1Counts[r + c] += 1;
diag2Counts[c - r + (N_QUEENS - 1)] += 1;
rowCounts[r] += 1;
colCounts[c] += 1;
}
}
// get total # of pairs for rows and cols
for (i = 0; i < N_QUEENS; ++i)
{
currCost += (rowCounts[i] * (rowCounts[i] - 1)) / 2;
currCost += (colCounts[i] * (colCounts[i] - 1)) / 2;
}
// get total # of pairs for both diagonals
for (i = 0; i < 2*N_QUEENS - 1; ++i)
{
currCost += (diag1Counts[i] * (diag1Counts[i] - 1)) / 2;
currCost += (diag2Counts[i] * (diag2Counts[i] - 1)) / 2;
}
return currCost;
}