-
Notifications
You must be signed in to change notification settings - Fork 1
/
towers.fiv
187 lines (186 loc) · 3.24 KB
/
towers.fiv
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
HANOI
!00000140
: hanoi
depth 1 < if
cr cr
." Hanoi expects the number of pieces on the stack." cr
." For example, to solve a five piece towers of hanoi " cr
." puzzle, type: " cr cr
." 5 HANOI" cr cr
exit
endif
towers ;
-*-
v
(N)
!0000000E
variable (N)
-*-
>
N
!0000000D
: N (N) @ ;
-*-
>
RING
!0000001C
variable ring
12 1+ allot
-*-
>
POS
!0000003C
: POS
( location pos -> coordinate )
N N + 1+ * N + ;
-*-
>
HALFDISPLAY
!00000043
: HALFDISPLAY
( color size --- )
0 DO DUP EMIT LOOP DROP ;
-*-
>
<DISPLAY>
!00000087
: <DISPLAY>
( line color size --- )
stack ab|abab HALFDISPLAY stack abc|bca 3 < IF 32 ELSE 186 ( | )
ENDIF EMIT HALFDISPLAY ;
-*-
>
DISPLAY
!000000A0
: DISPLAY
( size pos line color --- )
SWAP >R stack abc|caba - R@ ( color size pos-size line )
GOTOXY R> ( color size line ) stack abc|cab <DISPLAY> ;
-*-
>
PRESENCE
!00000049
: PRESENCE
( tower ring presence -> boolean )
RING + C@ = negate ;
-*-
>
LINE
!0000007B
: LINE
( tower line -> display-line-of-top )
4 SWAP N 0 DO DUP I PRESENCE 0= negate stack abc|bca + SWAP LOOP DROP ;
-*-
>
RAISE
!00000092
: RAISE
( size tower --- )
DUP POS SWAP LINE 2 SWAP DO
stack ab|abab I 32 DISPLAY stack ab|abab I 1- 205 DISPLAY
-1 +LOOP drop DROP ;
-*-
>
LOWER
!0000008C
: LOWER
( size tower --- )
DUP POS SWAP LINE 1+ 2 DO
stack ab|abab I 1- 32 DISPLAY stack ab|abab I 205 DISPLAY
LOOP drop DROP ;
-*-
>
MOVELEFT
!0000008C
: MOVELEFT
( size source.tower destiny.tower --- )
POS SWAP POS 1- DO DUP I 1+ 1 32 DISPLAY
DUP I 1 205 DISPLAY -1 +LOOP DROP ;
-*-
>
MOVERIGHT
!00000089
: MOVERIGHT
( size source.tower destiny.tower --- )
POS 1+ SWAP POS 1+ DO DUP I 1- 1 32 DISPLAY
DUP I 1 205 DISPLAY LOOP DROP ;
-*-
>
TRAVERSE
!0000006C
: TRAVERSE
( size source.tower destiny.tower --- )
stack ab|abab > IF MOVELEFT ELSE MOVERIGHT ENDIF ;
-*-
>
MOVE
!000000CC
: MOVE
( size source.tower destiny.tower --- )
?TERM if key 32 = not if 0 N 4 + GOTOXY ABORT endif endif
stack abc|cabab RAISE stack abc|abbca TRAVERSE
stack ab|abab RING + 1- C! SWAP LOWER ;
-*-
>
MULTIMOV
!000000D1
: MULTIMOV
( size source destiny spare --- )
3 PICK 1 = IF DROP MOVE ELSE
stack abcd|bcda 1- stack abcd|dabcdacb MULTIMOV
stack abcd|abcdbca 1+ stack abc|cab MOVE
stack abc|cba MULTIMOV ENDIF ;
-*-
>
MAKETOWER
!00000051
: MAKETOWER
( tower --- )
POS 4 N + 3 DO DUP I GOTOXY 186 EMIT LOOP DROP ;
-*-
>
MAKEBASE
!00000050
: MAKEBASE
( no arguments )
0 N 4 + GOTOXY N 6 * 3 + 0 DO 177 EMIT LOOP ;
-*-
>
MAKERING
!0000004C
: MAKERING
( tower size --- )
stack ab|abab RING + 1- C! SWAP LOWER ;
-*-
>
SETUP
!0000008D
: SETUP ( no arguments )
CLS
N 1+ 0 DO 1 RING I + C! LOOP
3 0 DO I MAKETOWER LOOP
MAKEBASE
1 N DO 0 I MAKERING -1 +LOOP
;
-*-
>
TOWERS
!000000DD
: TOWERS
( quantity --- )
1 MAX 12 MIN (N) !
SETUP
3 0 GOTOXY ." Fifth Demo press <esc> to quit"
N 2 0 1
BEGIN
OVER POS N 4 + GOTOXY
stack abcd|acdbacdb MULTIMOV
2 0 do 7 emit loop
0 UNTIL ;
-*-
>
DEMO
!00000012
: demo 5 hanoi ;
-*-
^