-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.h
215 lines (193 loc) · 3.48 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
#ifndef NODE_H
#define NODE_H
#include <vector>
using namespace std;
class node {
private:
public:
node* parent;
int depth; //depth of the node
int h; //heuristic value
// 3x3 array for numbers -> 0 is blank
int data[3][3];
// default constructor
node(){
depth = 0;
int x = 1;
for(int i = 0; i< 3; i++){
for(int j = 0; j< 3; j++){
data[i][j] = x;
x++;
}
}
data[2][2] = 0;
};
// constructor w/ 9 inputs as vector
node(vector<int> input){
depth = 0;
int x = 0;
for(int i = 0; i< 3; i++){
for(int j = 0; j< 3; j++){
data[i][j] = input.at(x);
x++;
}
}
}
void set_parent(node* p){
parent = p;
}
node* get_parent(){
return parent;
}
void set_h(int val){
h = val;
}
int get_h(){
return h;
}
void increment_depth(){
depth++;
}
void set_depth(int a){
depth = a;
}
int get_depth(){
return depth;
}
// print node (for testing)
void print_node(){
for(int i = 0; i < 3; i++){
for(int j = 0; j< 3; j++){
cout << data[i][j];
}
cout<< endl;
}
cout<< endl;
cout<< endl;
};
// returns if node is equal to input node
bool compare_nodes(node a){
bool same = true;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(a.data[i][j] != data[i][j]){
same = false;
}
}
}
return same;
};
int findX(int x){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(data[i][j] == x){
return j;
}
}
}
return 0;
}
int findY(int y){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(data[i][j] == y){
return i;
}
}
}
return 0;
}
// returns the x coordinate of the blank tile
int findY(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(data[i][j] == 0){
return i;
}
}
}
return 0;
}
// returns the y coordinate of the blank tile
int findX(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(data[i][j] == 0){
return j;
}
}
}
return 0;
}
// OPERATORS - move blank LRDU
bool can_createL(){
if(findX() > 0){
return true;
}
return false;
}
bool can_createR(){
if(findX() < 2){
return true;
}
return false;
}
bool can_createU(){
if(findY() > 0){
return true;
}
return false;
}
bool can_createD(){
if(findY() < 2){
return true;
}
return false;
}
void create_childL(){
int x = findX();
int y = findY();
if(x > 0){
int temp = data[y][x-1];
data[y][x-1] = data[y][x];
data[y][x] = temp;
}
};
void create_childR(){
int x = findX();
int y = findY();
if(x < 2){
int temp = data[y][x+1];
data[y][x+1] = data[y][x];
data[y][x] = temp;
}
};
void create_childD(){
int x = findX();
int y = findY();
if(y < 2){
int temp = data[y+1][x];
data[y+1][x] = data[y][x];
data[y][x] = temp;
}
}
void create_childU(){
int x = findX();
int y = findY();
if(y > 0){
int temp = data[y-1][x];
data[y-1][x] = data[y][x];
data[y][x] = temp;
}
}
vector<int> data_toVector(){
vector<int> vec;
for(int i = 0; i< 3; i++){
for(int j = 0; j< 3; j++){
vec.push_back(data[i][j]);
}
}
return vec;
}
};
#endif