Repository navigation
Expand file tree
/
Copy pathMaze.java
More file actions
177 lines (140 loc) · 5.21 KB
/
Copy pathMaze.java
File metadata and controls
177 lines (140 loc) · 5.21 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
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
package amazinglife;
import stackexcercise.*;
public class Maze {
//data fields:
int[][] finalGrid;
String[][] maze;
int r;
int c;
String explorer = "*";
int loc_R = 1;
int loc_C = 1;
boolean cantMove = false;
//creating the stack:
GreeneStack<Location> thePath = new GreeneStack<>();
//constructor:
public Maze(int rows, int columns, int[][] lastGen) {
r = rows;
c = columns;
finalGrid = lastGen;
}
//methods:
///////////////////////////////////////////////////////////////////////////
//creating the maze from the last generation:
public String[][] createMaze() {
maze = new String[r + 2][c + 2];
//top/bottom bounds:
for(int j = 0; j < c + 2; j++) {
maze[0][j] = "|";
maze[r + 1][j] = "|";
}
//left/right bounds:
for(int i = 0; i < r + 2; i++) {
maze[i][0] = "|";
maze[i][c + 1] = "|";
}
//filling the insides:
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
if(finalGrid[i+1][j+1] == 0) {
maze[i+1][j+1] = " ";
}
else if(finalGrid[i+1][j+1] == 1) {
maze[i+1][j+1] = "#";
}
}
}
//setting the value in the first location to "*":
maze[1][1] = explorer;
return maze;
}
///////////////////////////////////////////////////////////////////////////
//method for updating the maze:
public void updateMaze(Location location) throws GreeneEmptyStackException {
int i = location.rowN;
int j = location.columnN;
//right
if(!maze[i][j + 1].equals("#") && (j+1) <= c &&
!maze[i][j + 1].equals(".")) {
//setting previous place to "."
maze[i][j] = ".";
//setting the new location's position:
maze[i][j + 1] = explorer;
loc_R = i;
loc_C = j + 1;
//moving previous location to the stack
thePath.push(location);
}
//down
else if(!maze[i + 1][j].equals("#") && (i+1) <= r &&
!maze[i + 1][j].equals(".")) {
//setting previous place to "."
maze[i][j] = ".";
//setting the new location's position:
maze[i + 1][j] = explorer;
loc_R = i + 1;
loc_C = j;
//moving previous location to the stack
thePath.push(location);
}
//left
else if(!maze[i][j - 1].equals("#") && (j >= 2) &&
!maze[i][j - 1].equals(".")) {
//setting previous place to "."
maze[i][j] = ".";
//setting the new location's position:
maze[i][j - 1] = explorer;
loc_R = i;
loc_C = j - 1;
//moving previous location to the stack
thePath.push(location);
}
//up
else if(!maze[i - 1][j].equals("#") && (i >= 2) &&
!maze[i - 1][j].equals(".")) {
//setting previous place to "."
maze[i][j] = ".";
//setting the new location's position:
maze[i - 1][j] = explorer;
loc_R = i - 1;
loc_C = j;
//moving previous location to the stack
thePath.push(location);
}
//couldn't move
else {
//checking if the stack is empty (no more possible places to move):
if(thePath.isEmpty()) {
cantMove = true;
}
else {
//setting previous place to "."
maze[i][j] = ".";
//going back to the previous location:
Location prevLoc = thePath.pop();
maze[prevLoc.rowN][prevLoc.columnN] = explorer;
//setting the new location's position:
loc_R = prevLoc.rowN;
loc_C = prevLoc.columnN;
}
}
}
////////////////////////////////////////////////////////////////////////////
//Overriding the toString method for output:
public String toString() {
String output = "";
//printing the entire maze (including the bounds):
for(int m = 0; m < r + 2; m++) {
for(int n = 0; n < c + 2; n++) {
if(n == c + 1) {
output = output + maze[m][n] + "\n";
}
else {
output = output + maze[m][n];
}
}
}
return output;
}
///////////////////////////////////////////////////////////////////////////
}