-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpuzzlestate.cpp
More file actions
253 lines (233 loc) · 5.8 KB
/
Copy pathpuzzlestate.cpp
File metadata and controls
253 lines (233 loc) · 5.8 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Author: LOPES Marco
* Purpose: Stock a puzzle state data
* Date : november 2016
*/
#include "puzzlestate.h"
#include <QTextStream>
#include <QVector>
#include <QQueue>
#include <QStack>
#include <QSet>
#include <iostream>
PuzzleState::PuzzleState(){}
/*
* Constructor, Initialise the size of the puzzle and the parent
*
* size: size of the puzzle(3x3 or 4x4)
* parent: parent state or nullptr
*
*/
PuzzleState::PuzzleState(int size, PuzzleState *parent) : size(size), parent(parent){
}
PuzzleState::~PuzzleState()
{
delete leftChildren;
delete rightChildren;
delete topChildren;
delete bottomChildren;
}
void PuzzleState::generateChildrens()
{
int pos;
if (this->position.x() > 0){
pos = this->position.y()*this->size+this->position.x()-1;
leftChildren = this->swap(pos);
}
if (this->position.x() < this->getSize() - 1) {
pos = this->position.y()*this->size+this->position.x()+1;
rightChildren = this->swap(pos);
}
if (this->position.y() > 0) {
pos = (this->position.y()-1)*this->size+this->position.x();
topChildren = this->swap(pos);
}
if (this->position.y() < this->getSize() - 1) {
pos = (this->position.y()+1)*this->size+this->position.x();
bottomChildren = this->swap(pos);
}
}
/*
* Read puzzle configuration from console
*
* returns: void
*/
void PuzzleState::configure()
{
for (int i = 0; i < this->size; ++i) {
for (int j = 0; j < this->size; ++j) {
int number;
std::cin >> number;
this->puzzle << number;
if(number == size*size){
position.setX(j);
position.setY(i);
}
}
}
}
/*
* Configure puzzle with random values
*
* return: void
*/
void PuzzleState::random()
{
for (int position = 1; position <= this->size*this->size; ++position) {
this->puzzle << position;
}
std::random_shuffle(this->puzzle.begin(), this->puzzle.end());
for (int position = 0; position < this->size*this->size; ++position) {
if(this->puzzle.at(position) == size*size){
this->position.setX(position % this->size);
this->position.setY(position / this->size);
break;
}
}
}
/*
* Display the disposition of the puzzle
*
* returns: void
*/
void PuzzleState::display() const
{
QString separation("+---");
for (int i = 0; i < this->size; ++i) {
std::cout << " " << separation.repeated(this->size).toStdString() << "+" << std::endl;
for (int j = 0; j < this->size; ++j) {
if(this->puzzle.at(i*size+j) == size*size)
std::cout << " | ";
else if(this->puzzle.at(i*size+j) <= 9)
std::cout << " | " << this->puzzle.at(i*size+j);
else
std::cout << " |" << this->puzzle.at(i*size+j);
}
std::cout << " | " << std::endl;
}
std::cout << " " << separation.repeated(this->size).toStdString() << "+" << std::endl;
}
/*
* Getter of left children
*
* returns: PuzzleState* left children
*/
PuzzleState *PuzzleState::getLeft()
{
return this->leftChildren;
}
/*
* Getter of right children
*
* returns: PuzzleState* right children
*/
PuzzleState *PuzzleState::getRight()
{
return this->rightChildren;
}
/*
* Getter of top children
*
* returns: PuzzleState* top children
*/
PuzzleState *PuzzleState::getTop()
{
return this->topChildren;
}
/*
* Getter of bottom children
*
* returns: PuzzleState* bottom children
*/
PuzzleState *PuzzleState::getBottom()
{
return this->bottomChildren;
}
/*
* Swap the blank case with pos
*
* pos: the new position of the blank case
*
* returns: PuzzleState* the new state
*/
PuzzleState *PuzzleState::swap(int pos){
int posSizeSquare = this->position.y()*this->size+this->position.x();
PuzzleState *state = new PuzzleState(this->size, this);
state->puzzle = this->puzzle;
state->puzzle.replace(posSizeSquare, state->puzzle.at(pos));
state->puzzle.replace(pos, this->size*this->size);
state->position.setX(pos % this->size);
state->position.setY(pos / this->size);
return state;
}
/*
* Getter of puzzle
*
* returns: QVector<int> the puzzle data
*/
QVector<int> PuzzleState::getPuzzle() const
{
return puzzle;
}
/*
* Getter of parent
*
* returns:PuzzleState* the parent
*/
PuzzleState *PuzzleState::getParent() const
{
return parent;
}
/*
* Getter of size
*
* returns: int the size of the puzzle
*/
int PuzzleState::getSize() const
{
return size;
}
/*
* Getter of position of blank case(9 or 16)
*
* returns: QPoint the position of the blank case
*/
QPoint PuzzleState::getPosition() const
{
return position;
}
/*
* Get the total distance between actual and final state
*
* returns: int the number of cases bad placed
*/
int PuzzleState::manhattan(const PuzzleState &final) const
{
int cost = 0;
for (int line = 0; line < this->size; ++line) {
for (int column = 0; column < this->size; ++column) {
int value = this->puzzle.at(line*this->size+column);
int index = final.getPuzzle().indexOf(value);
int lineFinal = index / this->size;
int columnFinal = index % this->size;
cost += qAbs(line-lineFinal) + qAbs(column-columnFinal);
}
}
return cost;
}
/*
* Get the number of cases bad placed
*
* returns: int the number of cases bad placed
*/
int PuzzleState::badPlaced(const PuzzleState &final) const
{
int count = 0;
for (int line = 0; line < this->size; ++line) {
for (int column = 0; column < this->size; ++column) {
if(this->puzzle.at(line*this->size+column) != final.getPuzzle().at(line*this->size+column))
count++;
}
}
return count;
}