-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
245 lines (219 loc) · 4.97 KB
/
Copy pathboard.cpp
File metadata and controls
245 lines (219 loc) · 4.97 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
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
#include <iostream>
#include <iomanip>
#include <map>
#include <cmath>
#include <cstdlib>
#include <stdexcept>
#include "board.h"
using namespace std;
//default constructor
Board::Board(){}
Board::Board(int dim, int numInitMoves, int seed )
{
_size = dim*dim;
_tiles = new int[_size];
srand(seed);
for(int i=0; i < _size; i++){
_tiles[i] = i;
}
int blankLoc = 0;
while(numInitMoves > 0){
int r = rand()%4;
int randNeighbor = -1;
if(r == 0){
int n = blankLoc - dim;
if(n >= 0){
randNeighbor = n;
}
}
else if(r == 1){
int w = blankLoc - 1;
if(blankLoc % dim != 0){
randNeighbor = w;
}
}
else if(r == 2){
int s = blankLoc + dim;
if(s < _size){
randNeighbor = s;
}
}
else {
int e = blankLoc + 1;
if(blankLoc % dim != dim-1){
randNeighbor = e;
}
}
if(randNeighbor > -1){
_tiles[blankLoc] = _tiles[randNeighbor];
_tiles[randNeighbor] = 0;
blankLoc = randNeighbor;
numInitMoves--;
}
}
}
Board::~Board(){
delete [] _tiles;
}
Board::Board(const Board& other){
this->_size=other._size;
this->_tiles=new int[_size];
for(int i=0; i<this->size(); i++){
_tiles[i]=(other)[i];
}
}
void Board::move(int tile)
{
int side_dim = dim();
int tr, tc, br, bc;
// find tile row and column
int i=-1;
while(_tiles[++i] != tile);
tr = i / side_dim;
tc = i % side_dim;
// find blank row and column
int j=-1;
while(_tiles[++j] != 0);
br = j / side_dim;
bc = j % side_dim;
if( abs(static_cast<double>(tr-br)) + abs(static_cast<double>(tc-bc)) != 1){
cout << "Invalid move of tile " << tile << " at ";
cout << tr << "," << tc << " and blank spot at ";
cout << br << "," << bc << endl;
return;
}
// Swap tile and blank spot
_tiles[j] = tile;
_tiles[i] = 0;
}
// Generate new boards representing all the potential moves of tiles into
// the current blank tile location. The returned map should have
// the key as the tile moved and the value as a new Board object with the
// configuration reflecting the move of that tile into the blank spot
map<int, Board*> Board::potentialMoves() const
{
map<int, Board*>myMap;
for(int i=0; i<this->size(); i++){
if (_tiles[i]==0){
//found tile continaing zero
int zero_tile=i;
//able to move up
if(zero_tile/dim()>0){
Board* UBoard=new Board(*this);
UBoard->move(_tiles[zero_tile-dim()]);
myMap.insert(make_pair(_tiles[zero_tile-dim()],UBoard));
}
//able to move down
if(zero_tile/dim()<dim()-1){
Board* DBoard=new Board(*this);
DBoard->move(_tiles[zero_tile+dim()]);
myMap.insert(make_pair(_tiles[zero_tile+dim()], DBoard));
}
//able to move left
if(zero_tile%dim()>0){
Board* LBoard=new Board(*this);
LBoard->move(_tiles[zero_tile-1]);
myMap.insert(make_pair(_tiles[zero_tile-1], LBoard));
}
//able to move right
if(zero_tile%dim()<dim()-1){
Board* RBoard=new Board(*this);
RBoard->move(_tiles[zero_tile+1]);
myMap.insert(make_pair(_tiles[zero_tile+1], RBoard));
}
}
}
return myMap;
}
const int& Board::operator[](int loc) const
{
return _tiles[loc];
}
int Board::size() const
{
return _size;
}
int Board::dim() const
{
return static_cast<int>(sqrt(_size));
}
void Board::printRowBanner(ostream& os) const
{
int side_dim = dim();
if(side_dim == 0) return;
os << '+';
for(int i=0; i < side_dim; i++){
os << "--+";
}
os << endl;
}
//checks if we found solved state
bool Board::solved() const{
bool solved=true;
for(int i=0; i<size(); i++){
if(i!=_tiles[i]){
solved=false;
}
}
return solved;
}
//compares heuristic values of board tiles
bool Board::operator<(const Board& rhs) const{
for(int i=0; i<size(); i++){
if(this->_tiles[i]<rhs[i]){
return true;
}
else if(this->_tiles[i]>rhs[i]){
return false;
}
}
return false;
}
//prints format
ostream& operator<<(ostream &os, const Board &b){
int i=0;
int j=0;
while(i<=2*b.dim()){
//for odd values of i, we have tile number format
//for even values of i, we have barrier format
if(i%2==0){
b.printRowBanner(os);
}
else{
os<<"|";
for(int k=0; k<b.dim(); k++){
if(b[j]==0){
os<<" "<<"|";
}
else if(b[j]<10){
os<<" "<<b[j]<<"|";
}
else{
os<<b[j]<<"|";
}
j++;
}
os<<endl;
}
i++;
}
return os;
}
//assignment operator for board
Board& Board::operator=(const Board& other){
//makes sure board isn't assigned to itself
if(&other==this){
return *this;
}
//if already contains tiles, delete them
if(_tiles){
delete [] _tiles;
}
//copy contents over
this->_size=other._size;
this->_tiles=new int[_size];
for(int i=0; i<this->size(); i++){
_tiles[i]=(other)[i];
}
return *this;
}