-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMaze.scala
68 lines (57 loc) · 1.51 KB
/
Maze.scala
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
/* Maze generator in Scala
* Joe Wingbermuehle
* 2010-10-16
*/
object Maze {
private var width: Int = 0
private var height: Int = 0
private var maze: Array[Array[Int]] = null
/* Show the maze. */
def show {
maze.foreach(row =>
{
row.foreach(block => print(if(block == 1) "[]" else " "))
println
}
)
}
/* Start carving at x, y. */
private def carve(x: Int, y: Int) {
def update_pos(dir: Int, x: Int, y: Int): (Int, Int) = dir match {
case 0 => (x + 1, y + 0)
case 1 => (x + 0, y + 1)
case 2 => (x - 1, y + 0)
case _ => (x + 0, y - 1)
}
var dir: Int = (scala.math.random * 4.0).toInt
var count: Int = 0
while(count < 4) {
val (x1, y1) = update_pos(dir, x, y)
val (x2, y2) = update_pos(dir, x1, y1)
if(x2 > 0 && x2 < width && y2 > 0 && y2 < height) {
if(maze(y1)(x1) == 1 && maze(y2)(x2) == 1) {
maze(y1)(x1) = 0
maze(y2)(x2) = 0
carve(x2, y2)
}
}
count += 1
dir = (dir + 1) % 4
}
}
/* Generate a maze. */
def generate(w: Int, h: Int) {
width = w
height = h
maze = Array.fill[Int](height, width)(1)
maze(1)(1) = 0
carve(1, 1)
maze(0)(1) = 0
maze(height - 1)(width - 2) = 0
}
/* Generate and display a random maze. */
def main(args: Array[String]) {
generate(39, 23)
show
}
}