-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEightPuzzle.h
206 lines (157 loc) · 4.46 KB
/
EightPuzzle.h
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
#ifndef EIGHTPUZZLE_H
#define EIGHTPUZZLE_H
#include <queue>
#include <string>
#include <cmath>
#include "Node.h"
using std::priority_queue;
using std::greater;
class EightPuzzle {
public:
Node* mHead;
Node* mTail;
string mBoardValue, mDirections;
short mG;
vector<Node*> closedList, solvedList;
priority_queue<Node*, vector<Node*>, NodeComp> openList;
EightPuzzle() {
mBoardValue, mDirections = "";
mG = 0;
mHead, mTail = NULL;
}
EightPuzzle(string boardLayout) {
mBoardValue = boardLayout;
mHead = new Node(boardLayout);
mG = 0;
mDirections = "";
mTail = NULL;
}
//Populate tree and store solution
void solve() {
populateTree();
getPath();
}
//Move the blank tile either horizontally or vertically
string moveBlank(string boardLayout, short row, short col) {
short src = row * 3 + col;
short dst = boardLayout.find('0');
boardLayout[dst] = boardLayout[src];
boardLayout[src] = '0';
return boardLayout;
}
//Check if new direction is legal
bool checkBounds(short row, short col) {
return (row < 3) && (row >= 0) && (col < 3) && (col >= 0);
}
//Get direction of move
string getDirection(short row, short col) {
if (row == 1 && col == 0)
return "Down";
if (row == -1 && col == 0)
return "Up";
if (row == 0 && col == 1)
return "Right";
if (row == 0 && col == -1)
return "Left";
return "";
}
//Populate tree until goal is reached
void populateTree() {
Node* curNode = NULL;
Node* curChild = NULL;
vector<short>::size_type childrenSize = NULL;
openList.push(mHead);
mG++;
while(!openList.empty()) {
curNode = openList.top();
closedList.push_back(curNode);
openList.pop();
if(curNode->mBoardLayout == GOAL) {
mTail = curNode;
return;
}
possibleMoves(curNode);
childrenSize = curNode->mChildren.size();
for(size_t i = 0; i < childrenSize ; i++) {
curChild = &curNode->mChildren.at(i);
if(!inClosed(curChild)) {
if(!curChild->inOpen) {
openList.push(curChild);
curChild->inOpen = true;
}
}
}
curChild = NULL;
mG++;
}
}
//Check if board has been examined
bool inClosed(Node* node) {
vector<short>::size_type closedSize = closedList.size();
for(size_t i = 0; i < closedSize; i++) {
if(node->mBoardLayout.compare(closedList.at(i)->mBoardLayout) == 0)
return true;
}
return false;
}
//Generate moves reachable from current position
void possibleMoves(Node* curNode) {
short blank[2];
string newBoard;
blank[0] = curNode->mBlankTilePos[0];
blank[1] = curNode->mBlankTilePos[1];
for(short row = -1; row <= 1; row++) {
for(short col = -1; col <= 1; col++) {
if(abs(row) != abs(col)) {
if(checkBounds(blank[0] + row, blank[1] + col)) {
newBoard = moveBlank(curNode->mBoardLayout, blank[0] + row, blank[1] + col);
curNode->mChildren.push_back(Node(newBoard, getDirection(row, col), curNode, mG));
}
}
}
}
return;
}
//Search solution in tree and store path
void getPath() {
while(mTail->mParent != NULL) {
solvedList.push_back(mTail);
mDirections += mTail->mDirection;
mTail = mTail->mParent;
}
reverse(mDirections.begin(), mDirections.end());
}
//Print a selected board
void printBoard(string board) {
cout << "| " << board[0] << " | " << board[1] << " | " << board[2] << " |\n"
<< "| " << board[3] << " | " << board[4] << " | " << board[5] << " |\n"
<< "| " << board[6] << " | " << board[7] << " | " << board[8] << " |\n";
}
//Print best path in successive boards
void printBoardPath() {
vector<int>::size_type solvedSize = solvedList.size();
cout << "\nSTART\n";
printBoard(mHead->mBoardLayout);
for(short i = solvedSize - 1; i >= 0; i--) {
cout << "\n" << solvedList.at(i)->mDirection << endl;
printBoard(solvedList.at(i)->mBoardLayout);
}
cout << endl;
}
//Print examined boards
void printClosedList() {
vector<short>::size_type closedSize = closedList.size();
cout << "\n - Closed List - " << endl;
cout << "LOOP/BOARD/G + H = F" << endl;
for(size_t i = 0; i < closedSize; i++) {
cout << i << ". "
<< closedList.at(i)->mBoardLayout << " "
<< closedList.at(i)->mG << "+"
<< closedList.at(i)->mH << "="
<< closedList.at(i)->mF
<< endl;
}
cout << endl;
}
};
#endif