-
Notifications
You must be signed in to change notification settings - Fork 20
/
maze.swift
75 lines (66 loc) · 1.89 KB
/
maze.swift
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
// Maze generator in Swift
// 2014-12-11 - Joe Wingbermuehle
// 2018-02-20 - Sarp Başaraner - Update for Swift 4
// Build with:
// xcrun -sdk macosx swift maze.swift
import Foundation
class Maze {
enum Cell {
case Space, Wall
}
var data: [[Cell]] = []
// Generate a random maze.
init(width: Int, height: Int) {
for _ in 0 ..< height {
data.append([Cell](repeating: Cell.Wall, count: width))
}
for i in 0 ..< width {
data[0][i] = Cell.Space
data[height - 1][i] = Cell.Space
}
for i in 0 ..< height {
data[i][0] = Cell.Space
data[i][width - 1] = Cell.Space
}
data[2][2] = Cell.Space
self.carve(x: 2, y: 2)
data[1][2] = Cell.Space
data[height - 2][width - 3] = Cell.Space
}
// Carve starting at x, y.
func carve(x: Int, y: Int) {
let upx = [1, -1, 0, 0]
let upy = [0, 0, 1, -1]
var dir = Int(arc4random_uniform(4))
var count = 0
while count < 4 {
let x1 = x + upx[dir]
let y1 = y + upy[dir]
let x2 = x1 + upx[dir]
let y2 = y1 + upy[dir]
if data[y1][x1] == Cell.Wall && data[y2][x2] == Cell.Wall {
data[y1][x1] = Cell.Space
data[y2][x2] = Cell.Space
carve(x: x2, y: y2)
} else {
dir = (dir + 1) % 4
count += 1
}
}
}
// Show the maze.
func show() {
for row in data {
for cell in row {
if cell == Cell.Space {
print(" ", separator: "", terminator: "")
} else {
print("[]", separator: "", terminator: "")
}
}
print("")
}
}
}
let maze = Maze(width: 39, height: 23)
maze.show()