-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.java
200 lines (150 loc) · 4.81 KB
/
maze.java
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JPanel;
public class Board extends JPanel{
private Cell[][] cells;
private HashMap<String, Cell> walls;
private LinkedList<String> mapKeys;
private int cellWidth;
private int cellHeight;
public Board(int cellWidth, int cellHeight) {
setBackground(Color.BLACK);
walls = new HashMap<String, Cell>();
mapKeys = new LinkedList<String>();
this.cellWidth = cellWidth;
this.cellHeight = cellHeight;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
drawMaze(g2d);
}
/**
* Draws the maze onto the board
**/
public void drawMaze(Graphics2D g2d) {
//int cellx = cells.length-1;
//int celly = cells[0].length - 1;
for (int i=0; i < cells.length-1; i++) {
for (int j=0; j < cells[i].length - 1; j++) {
if (cells[i][j] != null) {
if (cells[i][j].isPassable()) {
System.out.println("Row: " + i + " Column: " + j);
g2d.setColor(Color.RED);
g2d.drawRect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
}
} else {
//g2d.setColor(Color.BLACK);
//g2d.drawRect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
}
}
}
}
/**
* Creates cells and tries to generate a maze
**/
public void createMaze() {
Random seed = new Random();
int width = this.getWidth();
int height = this.getHeight();
int cellsPerRow = width/cellWidth;
int cellsPerColumn = height/cellHeight;
cells = new Cell[cellsPerRow][cellsPerColumn];
//Pick the first cell
Cell firstCell = new Cell(cellHeight, cellWidth, true);
int theRow = seed.nextInt(cellsPerRow);
int theColumn = seed.nextInt(cellsPerColumn);
System.out.println("First row: " + theRow + " First Column: " + theColumn);
cells[theRow][theColumn] = firstCell;
//Add the adjacent walls to the map
addAdjacentWalls(theRow, theColumn, cellWidth, cellHeight);
Cell newCell = new Cell(cellHeight, cellWidth, false);
/**
* Loop through the walls
*/
while (!mapKeys.isEmpty()) {
//Get a random key from the Keymap
String posKey = mapKeys.remove(seed.nextInt(mapKeys.size()));
newCell = walls.get(posKey);
//Split the pos string
String parts[] = posKey.split(" ");
theRow = Integer.parseInt(parts[0]);
theColumn = Integer.parseInt(parts[1]);
System.out.println("The Row: " + theRow);
System.out.println("The Column: " + theColumn);
//Check that the opposite cells haven't been picked
if (!this.checkAdjacent(theRow, theColumn)) {
//Add the cell to the floor map
newCell.setPassable(true);
cells[theRow][theColumn] = newCell;
//Add the adjacent walls to the map
addAdjacentWalls(theRow, theColumn, cellWidth, cellHeight);
}
}
}
/**
* Checks the surrounding cells of the cell at [row][column]
*
* @param theRow
* @param theColumn
* @param cellHeight
* @param cellWidth
**/
public boolean checkAdjacent(int theRow, int theColumn) {
for (int i=-1; i<2; i++) {
for (int j=-1; j<2; j++) {
//Check for array out of bounds exceptions
if (!((theRow == 0 && i == -1 ) || (theColumn == 0 && j == -1))) {
//Make sure the corners aren't selected.
if (!((i+j)%2 == 0 || theRow + i > cells.length || theColumn + j > cells[theColumn].length -1)) {
if (cells[theRow+i][theColumn+j] == null ||!cells[theRow+i][theColumn+j].isPassable()) {
return false;
}
}
}
}
}
return true;
}
/**
* Adds the cells above, below, left and right of the cell specified by cellColumn and cellRow
* to the walls map
*
* @param cellColumn
* @param cellRow
* @param cellWidth
* @param cellHeight
*/
public void addAdjacentWalls(int cellRow, int cellColumn, int cellWidth, int cellHeight) {
for (int i=-1; i<2; i++) {
for (int j=-1; j<2; j++) {
//Check for array out of bounds exceptions
if (!((cellRow == 0 && i == -1 ) || (cellColumn == 0 && j == -1))) {
//Make sure the corners aren't selected.
if (!((i+j)%2 == 0 || cellRow + i > cells.length || cellColumn + j > cells[cellColumn].length -1)) {
Cell aWall = new Cell(cellHeight, cellWidth, false);
cellRow += i;
cellColumn += j;
//Create the key
String pos = "" + cellRow +" "+ cellColumn;
//Check that the wall has not already been added.
if (!walls.containsKey(pos)) {
//Add the wall
mapKeys.add(pos);
walls.put(pos , aWall);
} else {
mapKeys.remove(pos);
walls.remove(pos);
}
}
}
}
}
}
}