-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
189 lines (157 loc) · 3.15 KB
/
node.cpp
File metadata and controls
189 lines (157 loc) · 3.15 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
#include <assert.h>
#include <utility>
#include <iostream>
#include "node.h"
using namespace std;
Node::Node() {
cost = 0;
distanceToGoal = 0;
key = 0;
}
Node::Node(const Node& n) {
cost = n.cost;
for (short i = 0; i < 9; ++i) {
state[i] = n.state[i];
}
key = n.key;
distanceToGoal = n.distanceToGoal;
}
Node::~Node() {
}
void Node::set_cost(int c) {
cost = c;
}
int Node::get_cost() {
return cost;
}
void Node::set_distanceToGoal(int d) {
distanceToGoal = d;
}
int Node::get_distanceToGoal() {
return distanceToGoal;
}
void Node::set_state(const short* s) {
assert(s);
for (short i = 0; i < 9; ++i) {
state[i] = s[i];
}
updateKey();
}
short* Node::get_state() {
return state;
}
void Node::set_parent(Node* p) {
parent = p;
}
Node* Node::get_parent() {
return parent;
}
unsigned long Node::get_key() {
return key;
}
bool Node::operator<(const Node& rhs) const {
if (this->cost + this->distanceToGoal < rhs.cost + rhs.distanceToGoal) {
return true;
}
return false;
}
bool Node::operator>(const Node& rhs) const {
if (this->cost + this->distanceToGoal > rhs.cost + rhs.distanceToGoal) {
return true;
}
return false;
}
bool Node::operator==(const Node& rhs) const {
assert(this->state);
assert(rhs.state);
for (short i = 0; i < 9; ++i) {
if (this->state[i] != rhs.state[i])
return false;
}
return true;
}
void Node::updateKey() {
key = 0;
for (short i = 0; i < 9; ++i) {
key = key * 10 + state[i];
}
}
bool Node::move_blank_up(Node& n) {
// Find where the blank tile is
short pos;
for (short i = 0; i < 9; ++i) {
if (this->state[i] == 0) {
pos = i;
}
}
// Check if the blank tile can be moved up
if (pos < 3)
return false;
n = *this;
swap(n.state[pos], n.state[pos-3]);
++n.cost;
n.updateKey();
return true;
}
bool Node::move_blank_right(Node& n) {
// Find where the blank tile is
short pos;
for (short i = 0; i < 9; ++i) {
if (this->state[i] == 0) {
pos = i;
}
}
// check if the blank tile can be moved right
if (pos % 3 == 2)
return false;
n = *this;
swap(n.state[pos], n.state[pos+1]);
++n.cost;
n.updateKey();
return true;
}
bool Node::move_blank_down(Node& n) {
// Find where the blank tile is
short pos;
for (short i = 0; i < 9; ++i) {
if (this->state[i] == 0) {
pos = i;
}
}
// check if the blank tile can be moved down
if (pos > 5)
return false;
n = *this;
swap(n.state[pos], n.state[pos+3]);
++n.cost;
n.updateKey();
return true;
}
bool Node::move_blank_left(Node& n) {
// Find where the blank tile is
short pos;
for (short i = 0; i < 9; ++i) {
if (this->state[i] == 0) {
pos = i;
}
}
// check if the blank tile can be moved left
if (pos % 3 == 0)
return false;
n = *this;
swap(n.state[pos], n.state[pos-1]);
++n.cost;
n.updateKey();
return true;
}
void Node::print() {
cout << "Node g(n): " << this->cost << " Node h(n): " << this->distanceToGoal << "\n";
cout << "Node State:\n";
for (short i = 0; i < 9; ++i) {
cout << this->state[i] << ' ';
if (i % 3 == 2) {
cout << '\n';
}
}
cout << flush;
}