-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.h
126 lines (117 loc) · 2.13 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
#ifndef _Node_h_
#define _Node_h_
using namespace std;
class Node
{
public:
Node(int size)
{
boardSize = size;
int temp = ((size * size) - 1);
for(spacing = 0; temp > 0; spacing++) temp /= 10;
Weight = 0;
expandedNodes = 0;
MaxNodesInQueue = 0;
}
Node(Node* n)
{
this->boardSize = n->boardSize;
this->spacing = n->spacing;
expandedNodes = 0;
MaxNodesInQueue = 0;
this->Weight = n->Weight + 1;
this->State = n->State;
this->Moves = n->Moves;
}
~Node()
{
State.clear();
Moves.clear();
}
int findIndex(const vector<int> &v)
{
for (int i = 0; i < boardSize * boardSize; i++)
if(v[i] == -1)
return i;
}
void moveUp()
{
int index = findIndex(State);
if(index - boardSize >= 0)
{
swap(index, index - boardSize);
Moves.push_back(1);
}
}
void moveLeft()
{
int index = findIndex(State);
if(index - 1 >= 0 && (index - 1) / boardSize == index / boardSize)
{
swap(index, index - 1);
Moves.push_back(2);
}
}
void moveDown()
{
int index = findIndex(State);
if(index + boardSize < boardSize * boardSize)
{
swap(index, index + boardSize);
Moves.push_back(3);
}
}
void moveRight()
{
int index = findIndex(State);
if(index / boardSize == (index + 1) / boardSize)
{
swap(index, index + 1);
Moves.push_back(4);
}
}
void Display()
{
system("CLS");
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
cout << State[boardSize * i + j] + 1;
cout << endl;
}
}
void print(vector<int> v)
{
int s = v.size(), temp;
for(int i = 0; i < boardSize; i++)
{
cout << string(16,' ');
for(int j = 0; j < boardSize; j++)
{
temp = v[boardSize*i + j] + 1;
if(temp == 0)
cout << left << setw(spacing) << 'b' << " ";
else
cout << left << setw(spacing) << temp << " ";
}
if(i < boardSize - 1)
cout << endl;
}
}
void swap(const int &a, const int &b)
{
int temp = State[a];
State[a] = State[b];
State[b] = temp;
}
vector<Node*> Children;
vector<int> State;
vector<int> Moves;
int spacing;
int boardSize;
int expandedNodes;
int MaxNodesInQueue;
int Weight;
int H;
};
#endif //_Node_h_