-
Notifications
You must be signed in to change notification settings - Fork 1
/
Agent function
438 lines (387 loc) · 11.9 KB
/
Agent function
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
#include<iostream>
#include<ctime>
#include<stack>
#include<stdlib.h>
using namespace std;
#define WIDTH 600
#define HEIGHT 600
int keyflag = 0;
const int QUAD_DIMENTION = WIDTH / 17;
const int row = 37;
const int column = 37;
enum SquareType { Corridor, Wall, Start };
class Square {
SquareType type;
char display; //what will square display 0 for wall, ' '(space) for corridor, S for start point
//variables of all possible neighbours of current square
bool neighbour_top;
bool neighbour_left;
bool neighbour_bottom;
bool neighbour_right;
public:
bool visited;//variable if square has been visited
bool AgentVisited;
Square() {
type = Wall;
visited = false;
}
//change the state
void changeType(SquareType t) { type = t; }
void SquareVisited(bool v) { visited = v; }
void ChangeAgentVisited(bool v) { AgentVisited = v; }
void changeDisplay(char d) { display = d; }
void changeNeighbourTop(bool a) { neighbour_top = a; }
void changeNeighbourLeft(bool a) { neighbour_left = a; }
void changeNeighbourRight(bool a) { neighbour_right = a; }
void changeNeighbourBottom(bool a) { neighbour_bottom = a; }
//Getters
SquareType getType() { return type; }
char returnDisplay() { return display; }
//bool isVisited() { return visited; }
bool isAgentVisited() { return AgentVisited; }
bool availableNeighbourTop() { return neighbour_top; }
bool availableNeighbourBottom() { return neighbour_bottom; }
bool availableNeighbourLeft() { return neighbour_left; }
bool availableNeighbourRight() { return neighbour_right; }
};
class Maze {
//Square **matrix;
Square matrix[row][column];
//int row;
//int column;
int AgentPositionX;
int AgentpositionY;
int PlayerStartX;
int PlayerStartY;
public:
Maze(int r, int c) {
//setting all matrix to be a wall
for (int i = 0; i<r; i++) {
for (int j = 0; j<c; j++) {
matrix[i][j].changeDisplay('0');
matrix[i][j].SquareVisited(false);
matrix[i][j].changeNeighbourTop(true);;
matrix[i][j].changeNeighbourBottom(true);
matrix[i][j].changeNeighbourLeft(true);
matrix[i][j].changeNeighbourRight(true);
matrix[i][j].ChangeAgentVisited(false);
}
}
for (int i = 1; i<r - 1; i++) {
matrix[i][0].SquareVisited(true);
matrix[i][c - 1].SquareVisited(true);
matrix[i][1].SquareVisited(true);
matrix[i][c - 2].SquareVisited(true);
for (int j = 1; j<c - 1; j++) {
// Border Cells have fewer accessible walls
matrix[1][j].changeNeighbourTop(false);
matrix[r - 2][j].changeNeighbourBottom(false);
matrix[i][1].changeNeighbourLeft(false);
matrix[i][c - 2].changeNeighbourRight(false);
matrix[0][j].SquareVisited(true);
matrix[r - 1][j].SquareVisited(true);
matrix[1][j].SquareVisited(true);
matrix[r - 2][j].SquareVisited(true);
}
}
}
int getRow() { return row; }
int getColumn() { return column; }
void Redraw() {
for (int i = 0; i<row; i++) {
cout << endl;
matrix[i][0].changeDisplay(' ');
matrix[i][column - 1].changeDisplay(' ');
for (int j = 0; j < column; j++) {
cout << " " << matrix[i][j].returnDisplay();
matrix[0][j].changeDisplay(' ');
matrix[row - 1][j].changeDisplay(' ');
}
}
cout << endl;
int counterR;
int counterC;
for (size_t i = 0; i < row; i++)
{
for (size_t i = 0; i < column; i++)
{
}
}
}
//void GraficDraw() {
// for (size_t i = 0; i < row; i++)
// {
// for (size_t j = 0; j < column; j++)
// {
// if (matrix[i][j].returnDisplay() == '0') {
// glColor3f(0, 0, 1);
// drawQuad(j, i, QUAD_DIMENTION, QUAD_DIMENTION);
// }
// else if (matrix[i][j].returnDisplay() == ' ') {
// glColor3f(0, 1, 0);
// drawQuad(j, i, QUAD_DIMENTION, QUAD_DIMENTION);
// }
// else if (matrix[i][j].returnDisplay() == 'S') {
// glColor3f(1, 0, 0);
// drawQuad(j, i, QUAD_DIMENTION, QUAD_DIMENTION);
// }
// }
// }
// glutPostRedisplay();
//}
void makeStartPositions() {
for (int i = row - 1; i > 0; i--)
{
for (int j = 0; j < column; j++)
{
if (matrix[i][j].returnDisplay() == ' ') {
matrix[i][j].changeType(Start);
matrix[i][j].changeDisplay('S');
PlayerStartX = i;
PlayerStartY = j;
return;
}
}
}
}
void makeEndPositions() {
for (int i = 0; i < row; i++)
{
for (int j = column; j > 0; j--)
{
if (matrix[i][j].returnDisplay() == ' ') {
matrix[i][j].changeType(Start);
matrix[i][j].changeDisplay('S');
AgentPositionX = i;
AgentpositionY = j;
matrix[i][j].ChangeAgentVisited(true);
return;
}
}
}
}
void make() {
srand(time(NULL));
int randX;
int randY;
do
{
randX = ((2 * rand()) + 1) % (column - 1);
} while (!(randX > 3 && randX < column - 4));
do
{
randY = ((2 * rand()) + 1) % (row - 1);
} while (!(randY > 3 && randY < row - 4));
int visitedX = randX;
int visitedY = randY;
int visitedCells = 1;
int totalCells = 0;
int brojac = 0;
/* if(visitedX % 2 == 0 && visitedY % 2 == 0)
totalCells = ((row - 3) / 2)*((column - 3) / 2);
else if (visitedX % 2 == 1 && visitedY % 2 == 0)
totalCells = ((row - 4) / 2)*((column - 3) / 2);
else if (visitedX % 2 == 0 && visitedY % 2 == 1)
totalCells = ((row - 3) / 2)*((column - 4) / 2);
else if (visitedX % 2 == 1 && visitedY % 2 == 1)
totalCells = ((row - 4) / 2)*((column - 4) / 2);*/
stack<int> back_trackX, back_trackY;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
do {
if (((matrix[randY - 2][randX].visited == false) && (matrix[randY][randX].availableNeighbourTop() == true) && (matrix[randY - 2][randX].availableNeighbourBottom() == true)) ||
((matrix[randY + 2][randX].visited == false) && (matrix[randY + 2][randX].availableNeighbourTop() == true) && (matrix[randY][randX].availableNeighbourBottom() == true)) ||
((matrix[randY][randX - 2].visited == false) && (matrix[randY][randX].availableNeighbourLeft() == true) && (matrix[randY][randX - 2].availableNeighbourRight() == true)) ||
((matrix[randY][randX + 2].visited == false) && (matrix[randY][randX + 2].availableNeighbourLeft() == true) && (matrix[randY][randX].availableNeighbourRight() == true))) {
int random = rand() % 4 + 1;
if ((random == 1) && (randY > 1)) {
if (matrix[randY - 2][randX].visited == false) {
matrix[randY - 1][randX].changeDisplay(' ');
matrix[randY - 1][randX].SquareVisited(true);
matrix[randY - 1][randX].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourTop(false);
back_trackX.push(randX);
back_trackY.push(randY);
randY -= 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourBottom(false);
visitedCells++;
}
else
continue;
}
if ((random == 2) && (randY < row - 2)) {
if (matrix[randY + 2][randX].visited == false) {
matrix[randY + 1][randX].changeDisplay(' ');
matrix[randY + 1][randX].SquareVisited(true);
matrix[randY + 1][randX].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourBottom(false);
back_trackX.push(randX);
back_trackY.push(randY);
randY += 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourTop(false);
visitedCells++;
}
else
continue;
}
if ((random == 3) && (randX > 1)) {
if (matrix[randY][randX - 2].visited == false) {
matrix[randY][randX - 1].changeDisplay(' ');
matrix[randY][randX - 1].SquareVisited(true);
matrix[randY][randX - 1].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourLeft(false);
back_trackX.push(randX);
back_trackY.push(randY);
randX -= 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourRight(false);
visitedCells++;
}
else
continue;
}
if ((random == 4) && (randX < column - 2)) {
if (matrix[randY][randX + 2].visited == false) {
matrix[randY][randX + 1].changeDisplay(' ');
matrix[randY][randX + 1].SquareVisited(true);
matrix[randY][randX + 1].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourRight(false);
back_trackX.push(randX);
back_trackY.push(randY);
randX += 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].ChangeAgentVisited(false);
matrix[randY][randX].changeNeighbourLeft(false);
visitedCells++;
}
else
continue;
}
}
else {
randX = back_trackX.top();
back_trackX.pop();
randY = back_trackY.top();
back_trackY.pop();
brojac++;
}
} while (!back_trackX.empty());
}
void findPath(int x, int y, stack<int> &visitedx, stack<int> &visitedy) {
visitedx.push(x);
visitedy.push(y);
bool n = true;
while (n) {
if ((matrix[x + 1][y].returnDisplay() == ' ' || matrix[x + 1][y].returnDisplay() == 'S') && matrix[x + 1][y].isAgentVisited() == false) {
matrix[x + 1][y].changeDisplay('S');
matrix[x][y].changeDisplay(' ');
x += 1;
matrix[x][y].ChangeAgentVisited(true);
visitedx.push(x);
visitedy.push(y);
if (x == PlayerStartX && y == PlayerStartY)
n = false;
// Redraw();
}
else if ((matrix[x][y + 1].returnDisplay() == ' ' || matrix[x][y + 1].returnDisplay() == 'S') && matrix[x][y + 1].isAgentVisited() == false) {
matrix[x][y + 1].changeDisplay('S');
matrix[x][y].changeDisplay(' ');
y += 1;
matrix[x][y].ChangeAgentVisited(true);
visitedx.push(x);
visitedy.push(y);
if (x == PlayerStartX && y == PlayerStartY)
n = false;
// Redraw();
}
else if ((matrix[x - 1][y].returnDisplay() == ' ' || matrix[x - 1][y].returnDisplay() == 'S') && matrix[x - 1][y].isAgentVisited() == false) {
matrix[x - 1][y].changeDisplay('S');
matrix[x][y].changeDisplay(' ');
x -= 1;
matrix[x][y].ChangeAgentVisited(true);
visitedx.push(x);
visitedy.push(y);
if (x == PlayerStartX && y == PlayerStartY)
n = false;
// Redraw();
}
else if ((matrix[x][y - 1].returnDisplay() == ' '|| matrix[x][y - 1].returnDisplay() == 'S') && matrix[x][y - 1].isAgentVisited() == false) {
matrix[x][y - 1].changeDisplay('S');
matrix[x][y].changeDisplay(' ');
y -= 1;
matrix[x][y].ChangeAgentVisited(true);
visitedx.push(x);
visitedy.push(y);
if (x == PlayerStartX && y == PlayerStartY)
n = false;
// Redraw();
}
else {
matrix[x][y].changeDisplay(' ');
visitedx.pop();
visitedy.pop();
x = visitedx.top();
y = visitedy.top();
if (x == PlayerStartX && y == PlayerStartY)
n = false;
// Redraw();
}
}
while(!visitedx.empty()){
cout << visitedx.top() << "---" << visitedy.top() << endl;
visitedx.pop();
visitedy.pop();
}
}
void SmartAgent() {
stack<int> visitedX, visitedY;
findPath(AgentPositionX, AgentpositionY, visitedX, visitedY);
//stack<int> stack1, stack2;
//while (!visitedX.empty()) {
// stack1.push(visitedX.pop());
//}
//while (!visitedY.empty()) {
// stack2.push(visitedY.pop());
//}
//stack<int> vX, vY;
//int size = visitedX.size();
//cout << "velicina:. " << size << endl;
//for (size_t i = 0; i < size ; i++)
//{
// matrix[stack1.top()][stack2.top()].changeDisplay('S');
// stack1.pop();
// stack2.pop();
// Redraw();
//}
}
};
Maze m(37, 37);
void display()
{
//glClearColor(0.0, 0.0, 0.0, 0.0);
//glClear(GL_COLOR_BUFFER_BIT);
//m.make();
// m.makeStartPositions();
// m.makeEndPositions();
// m.GraficDraw();
//square.draw();
/*glutSwapBuffers();*/
}
void main() {
m.make();
m.makeEndPositions();
m.makeStartPositions();
m.Redraw();
m.SmartAgent();
m.Redraw();
system("pause>0");
}