-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
77 lines (63 loc) · 1.73 KB
/
Driver.java
File metadata and controls
77 lines (63 loc) · 1.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
import java.util.Random;
public class Driver
{
public static void main(String[] Args)
{
int width = 10;
int height = 10;
GameBoard b = new GameBoard("Escape!", width, height, "MazeSquare");
// Attempt to read a level number from the command line. Default to level 1.
int level = 1;
try{
level = Integer.parseInt(Args[0]);
}catch (Exception e) {}
Random generator = new Random(level);
int wallCount = 75 + ((int)level / 10);
for (int i=0; i<wallCount; i++)
{
int x = generator.nextInt(width);
int y = generator.nextInt(height);
int direction = generator.nextInt(4);
// Set the relevant square to a wall.
GameSquare s;
s = b.getSquareAt(x, y);
if (s != null)
s.setWall(direction, true);
// Ensure the neightbouring square agrees (if applicable)
if (direction == GameSquare.WALL_LEFT)
{
x--;
direction = GameSquare.WALL_RIGHT;
}
else if (direction == GameSquare.WALL_RIGHT)
{
x++;
direction = GameSquare.WALL_LEFT;
}
else if (direction == GameSquare.WALL_TOP)
{
y--;
direction = GameSquare.WALL_BOTTOM;
}
else if (direction == GameSquare.WALL_BOTTOM)
{
y++;
direction = GameSquare.WALL_TOP;
}
s = b.getSquareAt(x, y);
if (s != null)
s.setWall(direction, true);
}
// Ensure the outside edge of the maze has a border.
for (int x=0; x<width;x++)
{
b.getSquareAt(x, 0).setWall(GameSquare.WALL_TOP, true);
b.getSquareAt(x, height-1).setWall(GameSquare.WALL_BOTTOM, true);
}
for (int y=0; y<height;y++)
{
b.getSquareAt(0, y).setWall(GameSquare.WALL_LEFT, true);
b.getSquareAt(width-1, y).setWall(GameSquare.WALL_RIGHT, true);
}
}
}