-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmaze.st
92 lines (82 loc) · 1.98 KB
/
maze.st
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
"Maze generator in Smalltalk"
"Joe Wingbermuehle"
"2010-10-06"
Object subclass: #Maze
instanceVariableNames: 'width height maze rand'
classVariableNames: ''
poolDictionaries: ''
category: nil
!
!Maze methodsFor: 'Initialization'!
init
width := 39.
height := 23.
rand := Random new.
maze := ByteArray new: ((height + 1) * width + 1).
1 to: height do: [ :y |
1 to: width do: [ :x |
maze at: (y * width + x) put: 1.
].
].
^self.
!
!Maze class methodsFor: 'Creation'!
new
| r |
r := super new.
r init.
^r
!!
!Maze methodsFor: 'Display'!
print
1 to: height do: [ :y |
1 to: width do: [ :x |
((maze at: (y * width + x)) = 1) ifTrue: [ '[]' display ]
ifFalse: [ ' ' display ].
].
'' displayNl.
]
!!
!Maze methodsFor: 'Generation'!
"Get a random direction."
get_direction
^rand between: 0 and: 3
!
"Start carving at x, y"
carveat: x and: y
| dir count x1 y1 x2 y2 dx dy |
dir := self get_direction.
count := 0.
[ count < 4 ] whileTrue: [
dx := 0. dy := 0.
dir = 0 ifTrue: [ dx := 1 ].
dir = 1 ifTrue: [ dy := 1 ].
dir = 2 ifTrue: [ dx := -1 ].
dir = 3 ifTrue: [ dy := -1 ].
x1 := x + dx. y1 := y + dy.
x2 := x1 + dx. y2 := y1 + dy.
(x2 > 1) & (x2 < width) & (y2 > 1) & (y2 < height) ifTrue: [
((maze at: (y1 * width + x1)) = 1) ifTrue: [
((maze at: (y2 * width + x2)) = 1) ifTrue: [
maze at: (y1 * width + x1) put: 0.
maze at: (y2 * width + x2) put: 0.
self carveat: x2 and: y2.
].
].
].
count := count + 1.
dir := (dir + 1) \\ 4.
].
!
"Generate the maze."
generate
maze at: (2 * width + 2) put: 0.
self carveat: 2 and: 2.
maze at: (1 * width + 2) put: 0.
maze at: (height * width + width - 1) put: 0.
!!
"Generate and display a random maze."
| m |
m := Maze new.
m generate.
m print.