forked from managai/escape
/
Labirinth.cpp
181 lines (165 loc) · 3.92 KB
/
Labirinth.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
/* Labirinth class implementation file */
#include <iostream>
#include <cstdlib>
#include "Labirinth.h" // header file
Labirinth::Labirinth(int x, int y)
// constructor passing sides of labirinth
{
x_side = x;
y_side = y;
walls = 0;
exits = 0;
for ( int i = 0; i < x_side; i++ ) {
plane.push_back ( std::vector<int>() );
for ( int j = 0; j < y_side; j++ )
plane[i].push_back ( 0 );
}
}
void Labirinth::setWalls(int w)
// check walls already placed, than insert new wall pieces up to desired number
{
std::cout << "at present time there are " << walls << " walls in labirinth" << std::endl;
int rand_x;
int rand_y;
int i;
int result;
i = 0;
while (walls < w)
{
++i;
std::cout << "placing walls..." << std::endl;
rand_x = (rand() + time(0)) % x_side;
rand_y = (rand() + time(0)) % y_side;
result = readTile(rand_x, rand_y);
std::cout << i << " , (" << rand_x << "," << rand_y << ") , " << result << std::endl;
if ( result == 0)
{
setWall(rand_x, rand_y);
++walls;
std::cout << "placed wall number " << walls << " at (" << rand_x << "," << rand_y << ")" << std::endl;
}
else
{
std::cout << "this is just a wall or exit... another tile will be used" << std::endl;
}
}
std::cout << "at present time there are " << walls << " walls in labirinth" << std::endl;
}
void Labirinth::setExits(int e)
// check exits already placed, than insert new exits up to desired number
{
std::cout << "at present time there are " << exits << " exits in labirinth" << std::endl;
int rand_x;
int rand_y;
int i;
int result;
i = 0;
while (exits < e)
{
++i;
std::cout << "placing exit..." << std::endl;
rand_x = (rand() + time(0)) % x_side;
rand_y = (rand() + time(0)) % y_side;
result = readTile(rand_x, rand_y);
std::cout << i << " , (" << rand_x << "," << rand_y << ") , " << result << std::endl;
if ( result == 0)
{
setExit(rand_x, rand_y);
++exits;
std::cout << "placed exit number " << exits << " at (" << rand_x << "," << rand_y << ")" << std::endl;
}
else
{
std::cout << "this is just a wall or exit... another tile will be used" << std::endl;
}
}
std::cout << "at present time there are " << exits << " exits in labirinth" << std::endl;
}
void Labirinth::setWall(int x, int y)
// place a wall in labirinth by setting tile value to 1
{
int i = x;
int j = y;
plane[i][j] = 1;
}
void Labirinth::setExit(int x, int y)
// place the exit in labirinth by setting tile value to 2
{
int i = x;
int j = y;
plane[i][j] = 2;
}
bool Labirinth::isEmpty(int x, int y) const
// return true if tile is empty
{
int i = x;
int j = y;
if (plane[i][j] == 0)
{
return true;
}
else
{
return false;
}
}
bool Labirinth::isWall(int x, int y) const
// return true if tile is a wall
{
int i = x;
int j = y;
if (plane[i][j] == 1)
{
return true;
}
else
{
return false;
}
}
bool Labirinth::isExit(int x, int y) const
// return true if tile is the exit
{
int i = x;
int j = y;
if (plane[i][j] == 2)
{
return true;
}
else
{
return false;
}
}
void Labirinth::print(int robot_x, int robot_y) const
// print on screen the labirinth
// # represent a wall
// X represnt the exit point
// # represent the robot
// blank tiles are represtend by empty spaces
{
system("clear");
for ( int i = 0; i < x_side + 2; i++ ) {
std::cout << "#";
}
std::cout << std::endl;
for ( int i = 0; i < y_side; i++ ) {
std::cout << "#";
for ( int j = 0; j < x_side; j++ ){
if ((j == robot_x) && (i == robot_y))
std::cout << "@";
else if (isExit(j,i))
std::cout << "X";
else if (isWall(j,i))
std::cout << "#";
else
std::cout << " ";
}
std::cout << "#";
std::cout << std::endl;
}
for ( int i = 0; i < x_side + 2; i++ ) {
std::cout << "#";
}
std::cout << std::endl;
}