-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabirinth.cpp
More file actions
120 lines (110 loc) · 2.05 KB
/
Labirinth.cpp
File metadata and controls
120 lines (110 loc) · 2.05 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
#include <cmath>
#include <iostream>
#include "Labirinth.h"
#include <stdlib.h>
#include <time.h>
using namespace std;
void Labirinth::initRandomMatrix(){
int i, j, v;
for(i = 0; i < alt; i++){
for(j = 0; j < larg; j++){
v = (rand()%10 < 6);
if(!(i == 0 && j==0) && !(i== alt-1 && j==larg-1))
spots[i][j] = (Spot){v,0};
else{
spots[i][j] = (Spot){1,0};
}
// printf("%d ",spots[i][j].opened);
}
// printf("\n");
}
}
bool Labirinth::isSolveable(){
Path path(larg*alt);
Point destino = (Point){alt-1, larg-1};
int flag = 0, x = 0, y = 0;
Point ret;
while(!flag){
spots[x][y].visited = 1;
path.push((Point){x,y});
if(!(x == destino.x && y == destino.y)){
if(x-1 >= 0){
if(!spots[x-1][y].visited && spots[x-1][y].opened){
x--;
continue;
}
}
if(x+1 < alt){
if(!spots[x+1][y].visited && spots[x+1][y].opened){
x++;
continue;
}
}
if(y-1 >= 0){
if(!spots[x][y-1].visited && spots[x][y-1].opened){
y--;
continue;
}
}
if(y+1 < larg){
if(!spots[x][y+1].visited && spots[x][y+1].opened){
y++;
continue;
}
}
path.pop();
ret = path.pop();
if(ret.x != -1){
x = ret.x;
y = ret.y;
continue;
}else{
flag = 1;
}
}else{
flag = 1;
}
}
/* if(path.size > 0)
printCaminho(path, spots);
else{
printvisited(spots);
}*/
bool res = path.getSize() > 0;
// delete path;
return res;
}
Labirinth::Labirinth(int larg, int alt){
srand(time(NULL));
this->larg = larg;
this->alt = alt;
spots = new Spot*[alt];
for (int i = 0; i < alt; ++i)
spots[i] = new Spot[larg];
}
void Labirinth::generate(){
while(!isSolveable()){
initRandomMatrix();
}
}
void Labirinth::print(){
for(int i = 0; i < alt; i++){
for(int j = 0; j < larg; j++){
if(spots[i][j].opened){
cout << " ";
}else{
cout << "██";
}
// cout << spots[i][j].opened;
}
cout << endl;
}
}
int Labirinth::getBlockAt(int i, int j){
if(i < 0 || i >= alt)
return 0;
else if(j < 0 || j >= larg)
return 0;
else
return spots[i][j].opened;
}