-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
225 lines (207 loc) · 7.39 KB
/
main.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
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
#include "Node.hpp"
#include "dfs.hpp"
#include "bfs.hpp"
// Thanks to Cat Plus Plus answer: https://stackoverflow.com/questions/6486289/how-can-i-clear-console
#if defined _WIN32
#include <windows.h>
void clear() {
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
SetConsoleCursorPosition(console, topLeft);
}
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
void clear(){
std::cout << "\x1B[2J\x1B[H";
}
#elif defined (__APPLE__)
void clear(){
// I did not find any other solution
system("clear");
}
#endif
#ifndef endline
#define endLine '\n';
#endif
int main(){
int rows, columns, searchCostDFS=0, searchCostBFS=0, searchAlgorithm;
std::string answer, piece;
std::cout<< "Would you like see a example? (Y/N)"<< endLine;
std::cin>> answer;
while (answer != "Y" && answer != "N"){
clear();
std::cout<< "Invalid answer"<< endLine;
std::cout<< "Would you like see a example? (Y/N)"<< endLine;
std::cin>> answer;
}
if (answer == "Y"){
clear();
std::cout<< "\t Example"<< endLine;
std::cout<< endLine;
std::cout<< "\t-------------"<< endLine;
std::cout<< "\t| 7 | 2 | 4 |"<< endLine;
std::cout<< "\t-------------"<< endLine;
std::cout<< "\t| 5 | X | 6 |"<< endLine;
std::cout<< "\t-------------"<< endLine;
std::cout<< "\t| 8 | 3 | 1 |"<< endLine;
std::cout<< "\t-------------"<< endLine;
std::cout<< endLine;
std::cout<< "Input: 7 2 4 5 X 6 8 3 1"<< endLine;
}
std::cout<< "Type any input to continue"<< endLine;
std::cin>> answer;
clear();
std::cout<< "Type puzzle's number of rows and columns (i j): ";
// TO-DO: Check input
std::cin>> rows>> columns;
// Initial state of the puzzle
std::string state = "";
// Target state
std::string targetState = "";
// Stores the states of the puzzle to avoid loop more than one time over a state
std::set<std::string> puzzleStates;
// Stores the paths
std::list<std::string> paths;
clear();
std::cout << "What is the initial puzzle?"<< endLine;
std::cout<< "Pieces: ";
for (int i=0; i<rows; i++){
for (int j=0;j<columns; j++){
std::cin >> piece;
// TO-DO: Generalize
state.push_back(piece[0]);
state.push_back(' ');
}
}
// TO-Do: Check input
// Indices for each piece
int indexBlankPiece;
// Ignore ' ' characters
for (int i=0; i<rows*columns*2; i=i+2){
if (state[i] == 'X'){
indexBlankPiece = i/2;
break;
}
}
// Construct the node
Node inititalState = Node(state, searchCostBFS, paths, indexBlankPiece);
std::cout << "\nWhat is the target puzzle to be find?"<< endLine;
std::cout<< "Pieces: ";
for (int i=0; i<rows; i++){
for (int j=0; j<columns; j++){
// TO-DO: Prevent errors
std::cin >> piece;
// std::cout << endLine;
// Convert the piece value to character
targetState.push_back(piece[0]);
targetState.push_back(' ');
}
}
// TO-DO: Check input
clear();
std::cout<< "What search you would like to do?"<< endLine;
std::cout<< "--------------------------------"<< endLine;
std::cout<< "1- Depth-first Search (dfs)"<< endLine;
std::cout<< "2- Breadth-first Search (bfs)"<< endLine;
std::cout<< "--------------------------------"<< endLine;
std::cin>> searchAlgorithm;
while (searchAlgorithm != 1 && searchAlgorithm != 2){
clear();
std::cout<< "Invalid answer!"<< endLine;
std::cout<< endLine;
std::cout<< "What search you would like to do?"<< endLine;
std::cout<< "--------------------------------"<< endLine;
std::cout<< "1- Depth-first Search (dfs)"<< endLine;
std::cout<< "2- Breadth-first Search (bfs)"<< endLine;
std::cout<< "--------------------------------"<< endLine;
std::cin>> searchAlgorithm;
}
// Root will be the first piece whose value is different from rows*columns
int position = 0;
for (std::string::iterator piece = targetState.begin(); piece != targetState.end(); piece++){
if (*piece == 'X'){
break;
}
position++;
}
bool found=true;
// First declaration just for global scope
Node solution = inititalState;
// Comparations through hash
Node targetNode = Node(targetState, searchCostBFS, paths, position);
if (searchAlgorithm == 1){
std::cout << "Puzzle's solution through DFS"<< endLine;
solution = dfs(inititalState, columns, rows, puzzleStates, targetState, &searchCostDFS);
if (solution.index == -1){
std::cout << "It was not possible find a solution"<< endLine;
found = false;
}
else{
std::cout << "\nSearch cost dfs: "<< searchCostDFS<< endLine;
std::cout << "Solution cost dfs: "<< solution.path.size()-1<< endLine;
}
}
else{
std::cout << "Puzzle's solution through BFS..."<< endLine;
inititalState.path.push_back(inititalState.state);
solution = bfs(inititalState, columns, rows, puzzleStates, targetState, &searchCostBFS);
if (solution.index == -1){
std::cout << "It was not possible find a solution"<< endLine;
found = false;
}
else{
std::cout << "\nSearch cost bfs: "<< searchCostBFS << endLine;
// Ignore initial state (-1)
std::cout << "Solution cost bfs: "<< solution.path.size()-1<< endLine;
}
}
answer = "R";
if (found){
std::cout<<"Would you like to see the solution? (Y/N)"<< endLine;
std::cin>> answer;
while (answer != "Y" && answer != "N"){
clear();
std::cout<< "Invalid answer!"<< endLine;
// TO-DO: Clear console
std::cout<<"Would you like to see the solution? (Y/N)"<< endLine;
std::cin>> answer;
}
}
if (answer == "Y"){
int moves = 0;
Node move=inititalState;
for (std::string state: solution.path){
clear();
if (moves != 0){
std::cout<< "\t Move "<< moves<< endLine;
std::cout<< endLine;
move.state = state;
move.printState(columns, rows);
std::cout<< endLine;
std::cout<< endLine;
}
else{
std::cout<< " Initial state "<< endLine;
std::cout<< endLine;
inititalState.printState(columns, rows);
std::cout<< endLine;
std::cout<< endLine;
}
std::cout<< "Type anything to continue: ";
std::cin>> answer;
moves ++;
}
}
clear();
std::cout<< "\t\tPUZZLE'S END"<< endLine;
return 0;
}