-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.cpp
381 lines (345 loc) · 6.51 KB
/
Puzzle.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "Puzzle.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <iomanip>
#include <string>
#include "PrintXY.h"
using namespace std;
Puzzle::Puzzle()
{
cout << " Choose the puzzle size:" << endl;
cout << " 1- (3X3)" << endl;
cout << " 2- (4X4)" << endl;
cout << " 3- (5X5)" << endl;
cout << " 4- Scores" << endl;
bool input{ true };
while (input)
{
char ch = _getch();
ch -= '0'; // converting char to int
switch (ch)
{
case 1:
case 2:
case 3:
setDimensions(ch+2);
input = false;
break;
case 4:
read();
break;
default:
cout << "Wrong input! ";
break;
}
}
}
void Puzzle::setDimensions(int _side)
{
cout << "This is setDImensions() function " << endl;
this->ROWS = _side;
this->COLS = _side;
}
void Puzzle::make()
{
system("cls");
int count{ 1 };
for (int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
puzzle[r][c] = count;
count++;
if (r == ROWS - 1 && c == COLS - 1)
{
puzzle[r][c] = 0;
}
}
}
}
void Puzzle::print() const
{
system("cls");
int count{ 1 };
for (int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
// Set color
if (checkTileLocation(r, c) == 1)
{
SetConsoleTextAttribute(currentConsole, GREEN);
}
else if (checkTileLocation(r, c) == -1)
{
SetConsoleTextAttribute(currentConsole, RED);
}
else
{
SetConsoleTextAttribute(currentConsole, WHITE);
}
// Print with selected color
cout << left << setw(tileWidth) << puzzle[r][c];
}
cout << endl;
}
// Setting color and inserting number of moves below the puzzle
SetConsoleTextAttribute(currentConsole, WHITE);
printXY("------------------------------", 0, ROWS);
printXY("Moves: ", 0, ROWS+1);
}
void Puzzle::swap(int _ROW, int _COL, int _direction)
{
int temp{ 0 };
if (_direction == UP)
{
temp = puzzle[_ROW][_COL];
puzzle[_ROW][_COL] = puzzle[_ROW - 1][_COL];
puzzle[_ROW - 1][_COL] = temp;
}
else if (_direction == DOWN)
{
temp = puzzle[_ROW][_COL];
puzzle[_ROW][_COL] = puzzle[_ROW + 1][_COL];
puzzle[_ROW + 1][_COL] = temp;
}
else if (_direction == LEFT)
{
temp = puzzle[_ROW][_COL];
puzzle[_ROW][_COL] = puzzle[_ROW][_COL - 1];
puzzle[_ROW][_COL - 1] = temp;
}
else if (_direction == RIGHT)
{
temp = puzzle[_ROW][_COL];
puzzle[_ROW][_COL] = puzzle[_ROW][_COL + 1];
puzzle[_ROW][_COL + 1] = temp;
}
}
bool Puzzle::slide()
{
int zeroRow{ 0 };
int zeroCol{ 0 };
// finding the position of zero or * tile
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
if (puzzle[r][c] == 0) {
zeroRow = r;
zeroCol = c;
break;
}
}
}
char userinput = _getch();
switch (userinput)
{
case 'w':
case 'W':
if (zeroRow - 1 < 0)
{
return false;
}
swap(zeroRow, zeroCol, UP);
updateScreen(zeroRow-1, zeroCol);
updateScreen(zeroRow, zeroCol);
break;
case 's':
case 'S':
if (zeroRow + 1 >= ROWS)
{
return false;
}
swap(zeroRow, zeroCol, DOWN);
updateScreen(zeroRow + 1, zeroCol);
updateScreen(zeroRow, zeroCol);
break;
case 'a':
case 'A':
if (zeroCol - 1 < 0)
{
return false;
}
swap(zeroRow, zeroCol, LEFT);
updateScreen(zeroRow, zeroCol - 1);
updateScreen(zeroRow, zeroCol);
break;
case 'd':
case 'D':
if (zeroCol + 1 >= COLS)
{
return false;
}
swap(zeroRow, zeroCol, RIGHT);
updateScreen(zeroRow, zeroCol + 1);
updateScreen(zeroRow, zeroCol);
break;
default:
printXY("wrong input", 0, ROWS + 2);
//cerr << "wrong input" << endl;
return false;
}
Moves++;
printXY(attempts(), 8, ROWS+1);
return true;
}
bool Puzzle::slide(int _direction)
{
// This gets direction as integer and is used for shuffling the tiles
int zeroRow{ 0 };
int zeroCol{ 0 };
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
if (puzzle[r][c] == 0) {
zeroRow = r;
zeroCol = c;
break;
}
}
}
switch (_direction)
{
case 1:
if (zeroRow - 1 < 0)
{
return false;
}
swap(zeroRow, zeroCol, UP);
break;
case 2:
if (zeroRow + 1 >= ROWS)
{
return false;
}
swap(zeroRow, zeroCol, DOWN);
break;
case 3:
if (zeroCol - 1 < 0)
{
return false;
}
swap(zeroRow, zeroCol, LEFT);
break;
case 4:
if (zeroCol + 1 >= COLS)
{
return false;
}
swap(zeroRow, zeroCol, RIGHT);
break;
default:
cout << "wrong input" << endl;
return false;
}
return true;
}
void Puzzle::shuffle()
{
int direction;
int counter = 1;
srand(time(NULL));
while (counter <= 10000) {
direction = rand() % 4 + 1;
// If slide in the direction is valid counter goes up
if (slide(direction)) {
counter++;
}
}
}
bool Puzzle::winCheck() const
{
int counter = 1;
for (int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
if (puzzle[r][c] != counter)
{
if (r == ROWS-1 && c == COLS-1 && puzzle[r][c] == 0)
{
cout << "------ Solved -----" << endl;
return true;
}
return false;
}
counter++;
}
}
}
void Puzzle::updateScreen(int r, int c) const
{
// Set color
if (checkTileLocation(r, c) == 1) {
SetConsoleTextAttribute(currentConsole, GREEN); // the tile is in correct location
}
else if (checkTileLocation(r, c) == -1)
{
SetConsoleTextAttribute(currentConsole, RED); // the tile is not in the correct location
}
// Print with the selected color
if (puzzle[r][c] != 0) {
printXY(puzzle[r][c], c*tileWidth, r, tileWidth); // the specific movement (change) in the array will be printed
}
else
{
printXY("*", c*tileWidth, r, tileWidth);
}
SetConsoleTextAttribute(currentConsole, WHITE);
}
int Puzzle::checkTileLocation(int r, int c) const
{
//If tile is a blank space, return 0.
if (puzzle[r][c] == 0) {
return 0;
}
//If tile is in its correct location, return 1.
else if (puzzle[r][c] == ((r * COLS) + (c + 1))) {
return 1;
}
//If tile is in incorrect location, return -1.
else {
return -1;
}
return 0;
}
int Puzzle::attempts()
{
return Moves;
}
void Puzzle::write()
{
ofstream ofile("records.txt", ios_base::app);
if (ofile.is_open())
{
cout << "Please, insert your name :" << endl;
string name;
getline(cin, name);
ofile << name << " score in " << this->ROWS << "X" << this-> ROWS
<< " is : " << attempts() << endl;
}
else
{
cerr << " Could not open file" << endl;
}
}
void Puzzle::read() const
{
ifstream ifile("records.txt");
if (ifile.is_open())
{
while (!ifile.eof())
{
cout << "------------------------------" << endl;
string line;
getline(ifile, line);
cout << line << endl;
}
}
else
{
cerr << " Could not open file" << endl;
}
}
Puzzle::~Puzzle()
{
}