blob: e873c796e4718d50f6ee4003af10baaedf6b0800 (
plain) (
blame)
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
|
;
; Towers of Hanoi
;
; Copyright © 2016 Keith Packard <keithp@keithp.com>
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; ANSI control sequences
(define move-to (lambda (col row)
(for-each display (list "\033[" row ";" col "H"))
)
)
(define clear (lambda ()
(display "\033[2J")
)
)
(define display-string (lambda (x y str)
(move-to x y)
(display str)
)
)
; Here's the pieces to display
(define tower '(" * " " *** " " ***** " " ******* " " ********* " "***********"))
; Here's all of the towers of pieces
; This is generated when the program is run
(define towers ())
(define 1- (lambda (x) (- x 1)))
; Display one tower, clearing any
; space above it
(define display-tower (lambda (x y clear tower)
(cond ((= 0 clear)
(cond ((not (null? tower))
(display-string x y (car tower))
(display-tower x (1+ y) 0 (cdr tower))
)
)
)
(else
(display-string x y " ")
(display-tower x (1+ y) (1- clear) tower)
)
)
)
)
; Position of the top of the tower on the screen
; Shorter towers start further down the screen
(define tower-pos (lambda (y tower)
(- y (length tower))
)
)
; Display all of the towers, spaced 20 columns apart
(define display-towers (lambda (x y towers)
(cond ((not (null? towers))
(display-tower x 0 (tower-pos y (car towers)) (car towers))
(display-towers (+ x 20) y (cdr towers)))
)
)
)
(define top 0)
; Display all of the towers, then move the cursor
; out of the way and flush the output
(define display-hanoi (lambda ()
(display-towers 0 top towers)
(move-to 1 21)
(flush-output)
)
)
; Reset towers to the starting state, with
; all of the pieces in the first tower and the
; other two empty
(define reset-towers (lambda ()
(set! towers (list tower () ()))
(set! top (+ (length tower) 3))
(length tower)
)
)
; Replace a tower in the list of towers
; with a new value
(define replace (lambda (list pos member)
(cond ((= pos 0) (cons member (cdr list)))
((cons (car list) (replace (cdr list) (1- pos) member)))
)
)
)
; Move a piece from the top of one tower
; to the top of another
(define move-delay 10)
(define move-piece (lambda (from to)
(let* ((from-tower (list-ref towers from))
(to-tower (list-ref towers to))
(piece (car from-tower)))
(set! from-tower (cdr from-tower))
(set! to-tower (cons piece to-tower))
(set! towers (replace towers from from-tower))
(set! towers (replace towers to to-tower))
(display-hanoi)
; (delay move-delay)
)
)
)
; The implementation of the game
(define _hanoi (lambda (n from to use)
(cond ((= 1 n)
(move-piece from to)
)
(else
(_hanoi (1- n) from use to)
(_hanoi 1 from to use)
(_hanoi (1- n) use to from)
)
)
)
)
; A pretty interface which
; resets the state of the game,
; clears the screen and runs
; the program
(define hanoi (lambda ()
(let ((len))
(set! len (reset-towers))
(clear)
(_hanoi len 0 1 2)
(move-to 0 23)
#t
)
)
)
|