e873c796e4718d50f6ee4003af10baaedf6b0800
[fw/altos] / src / test / hanoi.lisp
1 ;
2 ; Towers of Hanoi
3 ;
4 ; Copyright © 2016 Keith Packard <keithp@keithp.com>
5 ;
6 ; This program is free software; you can redistribute it and/or modify
7 ; it under the terms of the GNU General Public License as published by
8 ; the Free Software Foundation, either version 2 of the License, or
9 ; (at your option) any later version.
10 ;
11 ; This program is distributed in the hope that it will be useful, but
12 ; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ; General Public License for more details.
15 ;
16
17                                         ; ANSI control sequences
18
19 (define move-to (lambda (col row)
20                   (for-each display (list "\033[" row ";" col "H"))
21                   )
22   )
23
24 (define clear (lambda ()
25                 (display "\033[2J")
26                 )
27   )
28
29 (define display-string (lambda (x y str)
30                          (move-to x y)
31                          (display str)
32                          )
33   )
34
35                                         ; Here's the pieces to display
36
37 (define tower '("     *     " "    ***    " "   *****   " "  *******  " " ********* " "***********"))
38
39                                         ; Here's all of the towers of pieces
40                                         ; This is generated when the program is run
41
42 (define towers ())
43
44 (define 1- (lambda (x) (- x 1)))
45                                         ; Display one tower, clearing any
46                                         ; space above it
47
48 (define display-tower (lambda (x y clear tower)
49                         (cond ((= 0 clear)
50                                (cond ((not (null? tower))
51                                       (display-string x y (car tower))
52                                       (display-tower x (1+ y) 0 (cdr tower))
53                                       )
54                                      )
55                                )
56                               (else 
57                                (display-string x y "                   ")
58                                (display-tower x (1+ y) (1- clear) tower)
59                                )
60                               )
61                         )
62   )
63
64                                         ; Position of the top of the tower on the screen
65                                         ; Shorter towers start further down the screen
66
67 (define tower-pos (lambda (y tower)
68                     (- y (length tower))
69                     )
70   )
71
72                                         ; Display all of the towers, spaced 20 columns apart
73
74 (define display-towers (lambda (x y towers)
75                          (cond ((not (null? towers))
76                                 (display-tower x 0 (tower-pos y (car towers)) (car towers))
77                                 (display-towers (+ x 20) y (cdr towers)))
78                                )
79                          )
80   )
81
82 (define top 0)
83                                         ; Display all of the towers, then move the cursor
84                                         ; out of the way and flush the output
85
86 (define display-hanoi (lambda ()
87                         (display-towers 0 top towers)
88                         (move-to 1 21)
89                         (flush-output)
90                         )
91   )
92
93                                         ; Reset towers to the starting state, with
94                                         ; all of the pieces in the first tower and the
95                                         ; other two empty
96
97 (define reset-towers (lambda ()
98                        (set! towers (list tower () ()))
99                        (set! top (+ (length tower) 3))
100                        (length tower)
101                        )
102   )
103
104                                         ; Replace a tower in the list of towers
105                                         ; with a new value
106
107 (define replace (lambda (list pos member)
108                   (cond ((= pos 0) (cons member (cdr list)))
109                         ((cons (car list) (replace (cdr list) (1- pos) member)))
110                         )
111                   )
112   )
113
114                                         ; Move a piece from the top of one tower
115                                         ; to the top of another
116
117 (define move-delay 10)
118
119 (define move-piece (lambda (from to)
120                      (let* ((from-tower (list-ref towers from))
121                            (to-tower (list-ref towers to))
122                            (piece (car from-tower)))
123                        (set! from-tower (cdr from-tower))
124                        (set! to-tower (cons piece to-tower))
125                        (set! towers (replace towers from from-tower))
126                        (set! towers (replace towers to to-tower))
127                        (display-hanoi)
128 ;                      (delay move-delay)
129                        )
130                      )
131   )
132
133 ; The implementation of the game
134
135 (define _hanoi (lambda (n from to use)
136                  (cond ((= 1 n)
137                         (move-piece from to)
138                         )
139                        (else
140                         (_hanoi (1- n) from use to)
141                         (_hanoi 1 from to use)
142                         (_hanoi (1- n) use to from)
143                         )
144                        )
145                  )
146   )
147
148                                         ; A pretty interface which
149                                         ; resets the state of the game,
150                                         ; clears the screen and runs
151                                         ; the program
152
153 (define hanoi (lambda ()
154                 (let ((len))
155                   (set! len (reset-towers))
156                   (clear)
157                   (_hanoi len 0 1 2)
158                   (move-to 0 23)
159                   #t
160                   )
161                 )
162   )