-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cpp
192 lines (160 loc) · 4.52 KB
/
Node.cpp
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
//James Aubin
//522009777
//CSCE 420
//Due: February 12, 2018
//Node.cpp
#include "Node.h"
using namespace std;
Node::Node(vector<vector<int>> boardState) :
parent(nullptr), boardState(boardState), moveCost(0), totalCost(0)
{
}
Node::Node(vector<vector<int>> prevState, Direction newMove)
{
this->boardState = prevState;
tuple<int, int> emptyPosition = getEmptyPosition(this->boardState);
int emptyRow = get<0>(emptyPosition);
int emptyCol = get<1>(emptyPosition);
//perform moves on the new node by swapping the empty tile with its neighbor
switch (newMove)
{
case Node::Direction::UP:
swap(this->boardState.at(emptyRow).at(emptyCol), this->boardState.at(emptyRow - 1).at(emptyCol));
break;
case Node::Direction::DOWN:
swap(this->boardState.at(emptyRow).at(emptyCol), this->boardState.at(emptyRow + 1).at(emptyCol));
break;
case Node::Direction::LEFT:
swap(this->boardState.at(emptyRow).at(emptyCol), this->boardState.at(emptyRow).at(emptyCol - 1));
break;
case Node::Direction::RIGHT:
swap(this->boardState.at(emptyRow).at(emptyCol), this->boardState.at(emptyRow).at(emptyCol + 1));
break;
default:
break;
}
}
void Node::print()
{
for (int i = 0; i < this->boardState.size(); i++)
{
for (int j = 0; j < this->boardState.at(i).size(); j++)
{
if (this->boardState.at(i).at(j) != -1)
{
cout << this->boardState.at(i).at(j) << " ";
}
}
cout << endl;
}
}
void Node::updateTotalCost(vector<vector<int>> goalState)
{
this->totalCost = this->moveCost + this->heuristic(goalState);
}
void Node::setMoveCost(int moveCost) { this->moveCost = moveCost; }
void Node::setTotalCost(int totalCost) { this->totalCost = totalCost; }
void Node::setParent(Node* parent) { this->parent = parent; }
Node* Node::getParent() { return this->parent; }
bool Node::isBounded(vector<vector<int>> boardState, int rowNum, int colNum)
{
return (boardState.at(rowNum).at(colNum) != -1);
}
vector<Node*> Node::getSuccessors()
{
vector<Node::Direction> validMoves = getValidMoves(this->boardState);
vector<Node*> successorVec;
for (int i = 0; i < validMoves.size(); i++)
{
Node* currSuccessor = new Node(this->boardState, validMoves.at(i));
currSuccessor->pathDirection = validMoves.at(i);
currSuccessor->parent = this;
successorVec.push_back(currSuccessor);
}
return successorVec;
}
vector<vector<int>> Node::getBoardState() { return this->boardState; }
Node::Direction Node::getDirection() { return this->pathDirection; }
int Node::getTotalCost() { return this->totalCost; }
int Node::getMoveCost() { return this->moveCost; }
int Node::heuristic(std::vector<std::vector<int>> goalState)
{
int numDifferences = 0;
for (int i = 0; i < goalState.size(); i++)
{
for (int j = 0; j < goalState.at(i).size(); j++)
{
if(this->boardState.at(i).at(j) != goalState.at(i).at(j))
{
numDifferences++;
}
}
}
return numDifferences;
}
vector<Node::Direction> Node::getValidMoves(vector<vector<int>> boardState)
{
//get the row/col position of the empty puzzle slot
tuple<int, int> emptyPosition = getEmptyPosition(boardState);
int emptyRow = get<0>(emptyPosition);
int emptyCol = get<1>(emptyPosition);
vector<Node::Direction> validMoves;
//check above the empty slot
if (isBounded(boardState, emptyRow - 1, emptyCol))
{
validMoves.push_back(Node::Direction::UP);
}
//check below the empty slot
if (isBounded(boardState, emptyRow + 1, emptyCol))
{
validMoves.push_back(Node::Direction::DOWN);
}
//check to the left of the empty slot
if (isBounded(boardState, emptyRow, emptyCol - 1))
{
validMoves.push_back(Node::Direction::LEFT);
}
//check to the right of the empty slot
if (isBounded(boardState, emptyRow, emptyCol + 1))
{
validMoves.push_back(Node::Direction::RIGHT);
}
return validMoves;
}
tuple<int, int> Node::getEmptyPosition(vector<vector<int>> boardState)
{
for (int i = 0; i < boardState.size(); i++)
{
for (int j = 0; j < boardState.at(i).size(); j++)
{
if (boardState.at(i).at(j) == 0)
{
return make_tuple(i, j);
}
}
}
return make_tuple(-1, -1);
}
ostream& operator<<(std::ostream& os, const Node::Direction& direction)
{
switch (direction)
{
case Node::Direction::UP:
cout << "UP";
break;
case Node::Direction::DOWN:
cout << "DOWN";
break;
case Node::Direction::LEFT:
cout << "LEFT";
break;
case Node::Direction::RIGHT:
cout << "RIGHT";
break;
default:
cout << "?";
break;
}
//~ os << direction;
return os;
}