-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.java
More file actions
198 lines (159 loc) · 3.73 KB
/
Copy pathmaze.java
File metadata and controls
198 lines (159 loc) · 3.73 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Maze maze = new Maze(15, 20);
maze.gen2();
// maze.generate();
maze.render();
}
}
class Maze {
private final int rows;
private final int cols;
private Cell[][] cells;
public Maze(int rows, int cols) {
this.rows = rows;
this.cols = cols;
cells = new Cell[rows][cols];
for (int row=0; row<rows; row++){
for (int col=0; col<cols; col++){
cells[row][col] = new Cell(row, col);
}
}
}
public void generate() {
Random rand = new Random();
Cell currentCell = cells[rand.nextInt(rows)][rand.nextInt(cols)];
generate(currentCell);
}
private void generate(Cell currentCell) {
currentCell.visited = true;
List<Cell> neighbors = getNeighbors(currentCell);
for (Cell neighbor : neighbors) {
if (!neighbor.visited) {
removeWalls(currentCell, neighbor);
generate(neighbor);
}
}
}
public void gen2() {
Stack<Cell> stack = new Stack<Cell>();
stack.push(cells[0][0]);
while (!stack.isEmpty()) {
Cell current = stack.peek();
Cell next = chooseNext(current);
if (next != null) {
next.visited = true;
stack.push(next);
removeWalls(current, next);
} else {
stack.pop();
}
}
}
private Cell chooseNext(Cell a) {
List<Cell> neighbors = getNeighbors(a);
for (Cell n : neighbors) {
if (!n.visited) {
return n;
}
}
return null;
}
private void removeWalls(Cell a, Cell b) {
if (a.row > b.row) {
a.up = false;
b.down = false;
}
else if (a.col > b.col) {
a.left = false;
b.right = false;
}
else if (a.row < b.row) {
a.down = false;
b.up = false;
}
else {
a.right = false;
b.left = false;
}
}
private List<Cell> getNeighbors(Cell currentCell) {
List<Cell> neighbors = new ArrayList<Cell>();
int r = currentCell.row;
int c = currentCell.col;
// North
if (r > 0) {
neighbors.add(cells[r-1][c]);
}
// West
if (c > 0) {
neighbors.add(cells[r][c-1]);
}
// South
if (r < rows - 1) {
neighbors.add(cells[r+1][c]);
}
// East
if (c < cols - 1) {
neighbors.add(cells[r][c+1]);
}
Collections.shuffle(neighbors);
return neighbors;
}
public void render() {
char[][] canvas = new char[(rows * 2) + 1][(cols * 3) + 1];
for (int r=0; r<canvas.length; r++) {
for (int c=0; c<canvas[0].length; c++) {
canvas[r][c] = ' ';
}
}
for (Cell[] row : cells) {
for (Cell cell : row) {
cell.paint(canvas);
}
}
// print the output array to System.out
for (char[] row : canvas) {
System.out.println(String.valueOf(row));
}
}
}
class Cell {
final int row;
final int col;
boolean visited = false;
boolean up = true;
boolean right = true;
boolean down = true;
boolean left = true;
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
public void paint(char[][] canvas) {
int x = col * 3;
int y = row * 2;
// Paint the corners.
canvas[y][x] = '+';
canvas[y][x + 3] = '+';
canvas[y + 2][x] = '+';
canvas[y + 2][x + 3] = '+';
// Paint the top edge of this cell.
if (up) {
canvas[y][x +1] = '-';
canvas[y][x +2] = '-';
}
if (down) {
canvas[y + 2][x +1] = '-';
canvas[y + 2][x +2] = '-';
}
if (left) {
canvas[y+1][x] = '|';
}
if (right) {
canvas[y+1][x+3] = '|';
}
}
}