forked from FredyR4zox/15-Puzzle-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.h
217 lines (174 loc) · 5.08 KB
/
Node.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
207
208
209
210
211
212
213
214
215
216
217
/*Node Libraries for the 15-Puzzle
It contains a configuration that is the board, a parent node,
a char that is the movement that originated the board (Config and all the parents),
a depth to retain how many moves we needed to do to reach the configuration
and a pathCost to ....................*/
#include <iostream>
#include <array>
#include <unordered_set>
#include "Config.h"
using namespace std;
class Node{
friend inline bool operator<(const Node& left, const Node& right);
friend inline bool operator>(const Node& left, const Node& right);
private:
Config cfg;
Node *parent;
char move; //Start: 's'; Up: 'u'; Down: 'd'; Left: 'l'; Right: 'r'
unsigned int depth;
unsigned int pathCost;
array<Node*, 4> children;
public:
Node();
Node(const array<int, 16>& arr);
Node(const Config& cfg_);
Node(Node& node, const char& move);
~Node();
void setPathCost(const unsigned int cost);
inline Config getConfig() const;
inline Node* getParent() const;
inline char getMove() const;
inline unsigned int getDepth() const;
inline unsigned int getPathCost() const;
inline array<Node*, 4> getChildren() const;
void display();
array<Node*, 4> makeDescendants(unordered_set<Config>* hashSet);
string makePath();
};
//Constructor for the Node class (for args: void, vector<int> , Config and (Node,char))
Node::Node(){
cfg = Config();
parent = NULL;
move = 's';
depth = 0;
pathCost = 0;
for(unsigned int i=0; i<4; i++)
children[i] = NULL;
}
Node::Node(const array<int, 16>& arr){
cfg = Config(arr);
parent = NULL;
move = 's';
depth = 0;
pathCost = 0;
for(unsigned int i=0; i<4; i++)
children[i] = NULL;
}
Node::Node(const Config& cfg_){
cfg = cfg_;
parent = NULL;
move = 's';
depth = 0;
pathCost = 0;
for(unsigned int i=0; i<4; i++)
children[i] = NULL;
}
Node::Node(Node& node, const char& mv){
cfg = node.getConfig();
cfg.move(mv);
parent = &node;
move = mv;
depth = node.getDepth() + 1;
pathCost = 0;
for(unsigned int i=0; i<4; i++)
children[i] = NULL;
}
Node::~Node(){
for(unsigned int i=0; i<4; i++)
if(children[i]!=NULL)
delete children[i];
}
void Node::setPathCost(unsigned int cost){
pathCost = cost;
}
//Getters
inline Config Node::getConfig() const{
return cfg;
}
inline Node* Node::getParent() const{
return parent;
}
inline char Node::getMove() const{
return move;
}
inline unsigned int Node::getDepth() const{
return depth;
}
inline unsigned int Node::getPathCost() const{
return pathCost;
}
inline array<Node*, 4> Node::getChildren() const{
return children;
}
//Print the board on stdout
void Node::display(){
cout << "Depth: " << depth << "\tMove: " << move << " \tParent: " << parent << endl;
cfg.display();
}
//Make the descendants of the current node
//i.e.: move up, down, left and right
//It will generate 4 Nodes at max (node not on limit of rows or columns)
//And 2 at min (Node in corner)
array<Node*, 4> Node::makeDescendants(unordered_set<Config>* hashSet){
array<char, 4> moves = cfg.possibleMoves();
array<Node*, 4> l = {};
unsigned int index = 0;
for(unsigned int i=0; i<4 && moves[i]!=0; i++){
Node *node = new Node(*this, moves[i]);
if(hashSet != NULL){
if(hashSet->insert(node->getConfig()).second == false){
delete node;
continue;
}
}
for(unsigned int i=0; i<4; i++){
if(this->children[i]==NULL){
this->children[i] = node;
break;
}
}
l[index] = node;
index++;
}
return l;
}
//When the solution is found, print what moves were made
string Node::makePath(){
string path = "";
Node *node = this;
while(node != NULL){
char c = node->getMove();
switch(c){
case 's':
path += "tratS";
break;
case 'u':
path += "pu >- ";
break;
case 'd':
path += "nwod >- ";
break;
case 'l':
path += "tfel >- ";
break;
case 'r':
path += "thgir >- ";
break;
}
node = node->getParent();
}
return string(path.rbegin(), path.rend()); //reverse the string
}
//Operator overloading for sorting using pathCost
struct compareNodes{
bool operator()(const Node* left, const Node* right) const{
return (left->getPathCost() > right->getPathCost());
}
};
inline bool operator<(const Node& left, const Node& right){
return (left.pathCost < right.pathCost);
}
inline bool operator>(const Node& left, const Node& right){
return (left.pathCost > right.pathCost);
}
//mudar Node & para Node& etc