-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
722 lines (665 loc) · 16.6 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <list>
#include <chrono>
#include <stdio.h>
#include <time.h>
#define RESET "\033[0m"
#define YELLOW "\033[1m\033[33m" /* Bold Yellow */
#define RED "\033[1m\033[31m" /* Bold Red */
#define GREEN "\033[1m\033[32m" /* Bold Green */
#define MAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define CYAN "\033[1m\033[36m" /* Bold Cyan */
#include "room.h"
void delay(int number_of_seconds)
{
// Converting time into milli_seconds
int milli_seconds = 1000 * number_of_seconds;
// Storing start time
clock_t start_time = clock();
// looping till required time is not achieved
while (clock() < start_time + milli_seconds)
;
}
int getAdjacentRoom(int roomNumber, int size);
int mazeRow(int roomNumber, int size);
int mazeColumn(int roomNumber, int size);
void openDoors(Room *room1, Room *room2, int size);
bool checkComplete(Room **maze, int size);
void updatePaths(Room **maze, int size, int oldPath, int newPath);
void showMaze(Room** maze, int size);
void DFS(Room **maze , int size, int flag);
std::list<Room>& nextPath(Room **maze, int size , std::pair<int,int> ,
std::list<Room>& way , std::list<Room> &total );
bool isIn (Room *room, std::vector<Room>& vector);
bool isIn (Room *room, std::list<Room>& list);
void showDFS(Room **maze, int size, std::list<Room>& total );
void cleanVisited(Room** maze, int size);
void resetParent(Room** maze, int size);
void BFS(Room** maze, int size, int flag);
std::list<Room>& nextBreadth(Room** maze, int size, std::pair<int,int> ,
std::list<Room>& queue);
void BFSPath(Room** maze, int size, std::list<Room>& path);
void showBFS(Room **maze, int size);
void showCleanedD(Room **maze, int size, std::list<Room>& way);
void showCleanedB(Room **maze, int size, std::list<Room>& path);
int main(){
int size;
srand((unsigned)time(0));
std::cout<<MAGENTA;
std::cout<<"Hey! This is a program that builds a square form maze and then solves it! \n";
std::cout<< " in two method : Breadth First Search (BFS) and Depth First Search (DFS) \n";
delay(50);
std::cout<< "lets see how it is!\n You just need to press the size of side of the maze";
std::cout<<"you want! (as intiger number and greater than 3)\n";
std::cout << " or you can press -1 for random size : ";
std::cin >> size;
while(size <= 3 && size != -1){
std::cin.clear();
std::cin.ignore();
std::cout << "Invalid size. Size must be a positive integer greater";
std::cout << " than 3: ";
std::cin >> size;
}
if(size == -1){
srand((unsigned)time(0));
size = rand() % 20 + 4;
std::cout << "Size randomly selected to be " << size << "\n";
}
std::cout<<RESET;
// create 2D array to be our maze
Room** maze = new Room*[size];
for(int i = 0; i < size; i++){
maze[i] = new Room[size];
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
maze[i][j].setRoomNumber((i * size) + j);
maze[i][j].setPathNumber((i * size) + j);
}
}
while(!checkComplete(maze, size))
{
int roomCount = size * size;
int room = rand() % roomCount;
int adjacentRoom = getAdjacentRoom(room, size);
int row1 = mazeRow(room, size);
int col1 = mazeColumn(room, size);
int row2 = mazeRow(adjacentRoom, size);
int col2 = mazeColumn(adjacentRoom, size);
if(maze[row1][col1].getPathNumber()!= maze[row2][col2].getPathNumber())
{
openDoors(&maze[row1][col1], &maze[row2][col2], size);
if(maze[row1][col1].getPathNumber() > maze[row2][col2].getPathNumber())
{
updatePaths(maze, size, maze[row1][col1].getPathNumber(),
maze[row2][col2].getPathNumber());
}
else {
updatePaths(maze, size, maze[row2][col2].getPathNumber(),
maze[row1][col1].getPathNumber());
}
}
}
// Open the north door of Room 0 and the South door of the last room
maze[0][0].setNorth(1);
maze[size - 1][size - 1].setSouth(1);
showMaze(maze , size);
int w {};
std::cout<<CYAN;
std::cout<<"Which method you want to solve this maze?"<<std::endl;
std::cout<<"press 1 for BFS method and 2 for DFS method"<<std::endl;
std::cin>>w;
while(w != 1 && w != 2){
std::cin.clear();
std::cout << "Invalid input! you should press 1 or 2 for this mean:\n";
std::cout << " 1 -----> BFS \n 2 -----> DFS \n";
std::cin >> w;
}
int flag{};
if(w == 1)
BFS(maze, size, flag);
if(w == 2)
DFS(maze , size, flag);
std::cout<<RESET;
//maze destructor
for(int i = 0; i < size; i++){
delete[] maze[i];
}
delete[] maze;
return 0;
}
int getAdjacentRoom(int roomNumber, int size){
int adj, maxNumber, delta, addSub;
bool valid = 0;
maxNumber = (size * size) - 1;
while(valid == 0){
valid = 1;
delta = rand() % 2;
addSub = rand() % 2;
if (delta == 0){
delta = size;
}
if(addSub == 1){
adj = roomNumber + delta;
} else {
adj = roomNumber - delta;
}
// 4 special cases where it would not be adjacent
if((adj - roomNumber == 1) && (adj % size == 0)){
valid = 0;
}
if((roomNumber - adj == 1) && (roomNumber % size == 0)){
valid = 0;
}
if((adj < 0) || (adj > maxNumber)){
valid = 0;
}
}
return adj;
}
int mazeRow(int roomNumber, int size){
return roomNumber / size;
}
int mazeColumn(int roomNumber, int size){
return roomNumber % size;
}
void openDoors(Room *room1, Room *room2, int size){
if(room1->getRoomNumber() - room2->getRoomNumber() == 1){
room1->setWest(1);
room2->setEast(1);
}
if(room2->getRoomNumber() - room1->getRoomNumber() == 1){
room2->setWest(1);
room1->setEast(1);
}
if(room1->getRoomNumber() - room2->getRoomNumber() == size){
room1->setNorth(1);
room2->setSouth(1);
}
if(room2->getRoomNumber() - room1->getRoomNumber() == size){
room2->setNorth(1);
room1->setSouth(1);
}
}
bool checkComplete(Room **maze, int size){
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(maze[i][j].getPathNumber() != 0){
return false;
}
}
}
return true;
}
void updatePaths(Room **maze, int size, int oldPath, int newPath){
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(maze[i][j].getPathNumber() == oldPath){
maze[i][j].setPathNumber(newPath);
}
}
}
}
void showMaze(Room** maze, int size)
{
std::cout << "\nThe Maze you made:\n";
std::cout << GREEN;
for(int i = 0; i < size; i++){
if(maze[0][i].getNorth() == 0){
std::cout << "+---";
} else {
std::cout << "+ ";
}
}
std::cout << "+\n";
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(maze[i][j].getWest() == 0){
std::cout << "| ";
} else {
std::cout << " ";
}
}
std::cout << "|\n";
for(int j = 0; j < size; j++){
if(maze[i][j].getSouth() == 0){
std::cout << "+---";
} else {
std::cout << "+ ";
}
}
std::cout << "+\n";
}
std::cout<<RESET<<std::endl;
}
void DFS( Room** maze , int size, int flag )
{
char a;
std::list<Room> way;
std::list<Room> total;
resetParent(maze, size);
cleanVisited(maze, size);
way.push_back(maze[0][0]);
total.push_back(maze[0][0]);
way = nextPath( maze, size , {0 ,0} , way , total);
showDFS(maze, size, total);
showCleanedD(maze , size , way);
if(!flag)
{
flag++;
std::cout<<CYAN<<"Do you want to see the solution in BFS method too?\n";
std::cout<<"press y for yes and n for no\n";
std::cin>> a;
if(a=='y')
BFS(maze, size, flag);
}
}
std::list<Room>& nextPath(Room **maze , int size, std::pair<int,int> location ,
std::list<Room>& way , std::list<Room>& total )
{
int x {} , y {}, xp {}, yp {};
y = location.first;
x = location.second;
int final { way.back().getRoomNumber()};
maze[y][x].setVisited(1);
if(final ==(size*size)-1)
return way;
if(final !=(size*size)-1)
{
if( y!=0 && maze[y][x].getNorth() && !maze[y-1][x].getVisited() )
{
maze[y-1][x].setParent(maze[y][x].getRoomNumber());
way.push_back(maze[y-1][x]);
total.push_back(maze[y-1][x]);
way = nextPath(maze , size , {y-1 , x},way , total);
if(way.back().getRoomNumber()==(size*size)-1)
return way;
}
if( x!=size-1 && maze[y][x].getEast() && !maze[y][x+1].getVisited() )
{
maze[y][x+1].setParent(maze[y][x].getRoomNumber());
way.push_back(maze[y][x+1]);
total.push_back(maze[y][x+1]);
way = nextPath(maze, size , {y , x+1}, way , total );
if(way.back().getRoomNumber()==(size*size)-1)
return way;
}
if( y!=size-1 && maze[y][x].getSouth() && !maze[y+1][x].getVisited() )
{
maze[y+1][x].setParent(maze[y][x].getRoomNumber());
way.push_back(maze[y+1][x]);
total.push_back(maze[y+1][x]);
way = nextPath(maze, size , {y+1 , x}, way , total );
if(way.back().getRoomNumber()==(size*size)-1)
return way;
}
if( x!=0 && maze[y][x].getWest() && !maze[y][x-1].getVisited() )
{
maze[y][x-1].setParent(maze[y][x].getRoomNumber());
way.push_back(maze[y][x-1]);
total.push_back(maze[y][x-1]);
way = nextPath(maze, size , {y , x-1}, way ,total );
if(way.back().getRoomNumber()==(size*size)-1)
return way;
}
way.pop_back();
xp = mazeColumn(maze[y][x].getParent(), size);
yp = mazeRow(maze[y][x].getParent(),size);
total.push_back(maze[yp][xp]);
return way;
}
return way;
}
bool isIn (Room *room, std::list<Room>& list)
{
std::list<Room>::iterator it;
for (it = list.begin(); it != list.end(); ++it)
{
if(*room == *it)
return true;
}
return false;
}
void showDFS(Room **maze, int size, std::list<Room>& total )
{
int check {} , check2 {};
while(total.size())
{
std::cout << "\033[2J\033[1;1H";
delay (300);
std::cout << "\nDFS method solving Maze:\n"<<GREEN<<std::endl;
for(int i = 0; i < size; i++){
if(maze[0][i].getNorth() == 0){
std::cout<< "+---";
}
else
{
std::cout<<GREEN<< "+ "<<GREEN;
}
}
std::cout<< GREEN << "+\n";
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++)
{
check =0;
if( maze[i][j]==total.front() && !check2)
{
check =1;
check2 =1;
total.pop_front();
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN<< "| "<<YELLOW<<"@ "<<GREEN ;
}
else {
std::cout<<YELLOW<< " @ "<<GREEN;
}
}
if(!check)
{
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN << "| ";
}
else {
std::cout << " ";
}
}
}
std::cout << "|\n";
for(int j = 0; j < size; j++)
{
check=0;
if(maze[i][j].getSouth() == 0)
{
std::cout <<GREEN<< "+---";
}
else {
std::cout << "+ ";
}
}
std::cout << "+\n"<<RESET;
}
check2 =0;
}
std::cout<<std::endl;
}
void showCleanedD(Room **maze, int size , std::list<Room>& way)
{
std::cout <<"\n the main path you have been through @DFS method:\n"<<GREEN<<std::endl;
int check {};
for(int i = 0; i < size; i++){
if(maze[0][i].getNorth() == 0){
std::cout<< "+---";
}
else
{
std::cout<< "+ "<< RED <<"* "<<GREEN;
}
}
std::cout<< GREEN << "+\n";
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++)
{
check =0;
if( isIn(&maze[i][j], way))
{
check =1;
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN<< "| "<<RED<<"* "<<GREEN ;
}
else {
std::cout<<RED<< " * "<<GREEN;
}
}
if(!check)
{
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN << "| ";
}
else {
std::cout << " ";
}
}
}
std::cout << "|\n";
for(int j = 0; j < size; j++)
{
check=0;
if(maze[i][j].getSouth() == 0)
{
std::cout <<GREEN<< "+---";
}
else {
if(i == (size-1) && j==(size-1))
std::cout << "+ "<<RED<<"* "<<GREEN;
else
std::cout << "+ ";
}
}
std::cout << "+\n"<<RESET;
}
std::cout<<std::endl;
}
void cleanVisited(Room** maze, int size)
{
for (int i {}; i < size; i++)
for (int j {}; j < size; j++)
maze[i][j].setVisited(0);
}
void resetParent(Room** maze, int size)
{
for (int i {}; i < size; i++)
for (int j {}; j < size; j++)
maze[i][j].setParent(0);
}
void BFS (Room** maze, int size, int flag)
{
char a;
std::list<Room> queue;
maze[0][0].setLevel(0);
resetParent(maze, size);
cleanVisited(maze, size);
queue.push_back(maze[0][0]);
queue = nextBreadth(maze, size, {0, 0}, queue);
std::list<Room> path;
path.push_back(maze[size-1][size-1]);
BFSPath(maze, size, path);
showBFS(maze, size);
showCleanedB(maze, size, path);
if(!flag)
{
flag++;
std::cout<<CYAN<<"Do you want to see the solution in DFS method too?\n";
std::cout<<"press y for yes and n for no\n";
std::cin>> a;
if(a=='y')
DFS(maze, size, flag);
}
}
std::list<Room>& nextBreadth (Room** maze, int size, std::pair<int,int> location
, std::list<Room>& queue)
{
int y {location.first};
int x {location.second};
maze[y][x].setVisited(1);
Room final { queue.back()};
if( final.getRoomNumber()== (size*size)-1 )
{
return queue;
}
if( final.getRoomNumber()!= (size*size)-1 )
{
if( y!=0 && maze[y][x].getNorth() && !maze[y-1][x].getVisited() )
{
maze[y-1][x].setLevel(maze[y][x].getLevel()+1);
queue.push_back(maze[y-1][x]);
maze[y-1][x].setParent(maze[y][x].getRoomNumber());
}
if( x!=size-1 && maze[y][x].getEast() && !maze[y][x+1].getVisited() )
{
maze[y][x+1].setLevel(maze[y][x].getLevel()+1);
queue.push_back(maze[y][x+1]);
maze[y][x+1].setParent(maze[y][x].getRoomNumber());
}
if( y!=size-1 && maze[y][x].getSouth() && !maze[y+1][x].getVisited() )
{
maze[y+1][x].setLevel(maze[y][x].getLevel()+1);
queue.push_back(maze[y+1][x]);
maze[y+1][x].setParent(maze[y][x].getRoomNumber());
}
if( x!=0 && maze[y][x].getWest() && !maze[y][x-1].getVisited() )
{
maze[y][x-1].setLevel(maze[y][x].getLevel()+1);
queue.push_back(maze[y][x-1]);
maze[y][x-1].setParent(maze[y][x].getRoomNumber());
}
}
queue.pop_front();
int n {queue.front().getRoomNumber()};
int ny { mazeRow(n, size)} , nx { mazeColumn(n, size)};
queue = nextBreadth(maze, size, {ny, nx}, queue);
return queue;
}
void BFSPath(Room** maze, int size, std::list<Room>& path)
{
int next { path.back().getParent()};
int ny {mazeRow(next, size)} , nx {mazeColumn(next, size)};
while(path.back().getRoomNumber() != 0)
{
path.push_back(maze[ny][nx]);
next = path.back().getParent();
ny = mazeRow(next, size);
nx = mazeColumn(next, size);
}
}
void showBFS(Room **maze, int size )
{
int check {} ;
int L { maze[size-1][size-1].getLevel() +1 };
int level {};
while(L)
{
L--;
std::cout << "\033[2J\033[1;1H";
delay (500);
std::cout<<RESET << "\nBFS method solving Maze:\n"<<GREEN<<std::endl;
for(int i = 0; i < size; i++){
if(maze[0][i].getNorth() == 0){
std::cout<< "+---";
}
else
{
std::cout<<GREEN<< "+ "<<GREEN;
}
}
std::cout<< GREEN << "+\n";
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++)
{
check =0;
if( maze[i][j].getLevel()==level )
{
check =1;
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN<< "| "<<YELLOW<<"@ "<<GREEN ;
}
else {
std::cout<<YELLOW<< " @ "<<GREEN;
}
}
if(!check)
{
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN << "| ";
}
else {
std::cout << " ";
}
}
}
std::cout << "|\n";
for(int j = 0; j < size; j++)
{
check=0;
if(maze[i][j].getSouth() == 0)
{
std::cout <<GREEN<< "+---";
}
else {
std::cout << "+ ";
}
}
std::cout << "+\n"<<RESET;
}
level++;
std::cout<< " current level: "<< level-1 <<std::endl;
}
std::cout<<std::endl;
}
void showCleanedB(Room **maze, int size, std::list<Room>& path)
{
std::cout<<"the main path you have been through @BFS method:"<<std::endl;
int check {};
for(int i = 0; i < size; i++){
if(maze[0][i].getNorth() == 0){
std::cout<<GREEN<< "+---";
}
else
{
std::cout<<GREEN<< "+ "<< RED <<"* "<<GREEN;
}
}
std::cout<< GREEN << "+\n";
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++)
{
check =0;
if( isIn(&maze[i][j], path))
{
check =1;
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN<< "| "<<RED<<"* "<<GREEN ;
}
else {
std::cout<<RED<< " * "<<GREEN;
}
}
if(!check)
{
if(maze[i][j].getWest() == 0)
{
std::cout<<GREEN << "| ";
}
else {
std::cout << " ";
}
}
}
std::cout << "|\n";
for(int j = 0; j < size; j++)
{
check=0;
if(maze[i][j].getSouth() == 0)
{
std::cout <<GREEN<< "+---";
}
else {
if(i == (size-1) && j==(size-1))
std::cout << "+ "<<RED<<"* "<<GREEN;
else
std::cout << "+ ";
}
}
std::cout << "+\n"<<RESET;
}
std::cout<<std::endl;
}