4 ; Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
17 ; ANSI control sequences
19 (define move-to (lambda (col row)
20 (for-each display (list "\033[" row ";" col "H"))
24 (define clear (lambda ()
29 (define display-string (lambda (x y str)
35 ; Here's the pieces to display
37 (define tower '(" * " " *** " " ***** " " ******* " " ********* " "***********"))
39 ; Here's all of the towers of pieces
40 ; This is generated when the program is run
44 (define one- (lambda (x) (- x 1)))
45 (define one+ (lambda (x) (+ x 1)))
46 ; Display one tower, clearing any
49 (define display-tower (lambda (x y clear tower)
51 (cond ((not (null? tower))
52 (display-string x y (car tower))
53 (display-tower x (one+ y) 0 (cdr tower))
58 (display-string x y " ")
59 (display-tower x (one+ y) (one- clear) tower)
65 ; Position of the top of the tower on the screen
66 ; Shorter towers start further down the screen
68 (define tower-pos (lambda (y tower)
73 ; Display all of the towers, spaced 20 columns apart
75 (define display-towers (lambda (x y towers)
76 (cond ((not (null? towers))
77 (display-tower x 0 (tower-pos y (car towers)) (car towers))
78 (display-towers (+ x 20) y (cdr towers)))
84 ; Display all of the towers, then move the cursor
85 ; out of the way and flush the output
87 (define display-hanoi (lambda ()
88 (display-towers 0 top towers)
94 ; Reset towers to the starting state, with
95 ; all of the pieces in the first tower and the
98 (define reset-towers (lambda ()
99 (set! towers (list tower () ()))
100 (set! top (+ (length tower) 3))
105 ; Replace a tower in the list of towers
108 (define replace (lambda (list pos member)
109 (cond ((= pos 0) (cons member (cdr list)))
110 ((cons (car list) (replace (cdr list) (one- pos) member)))
115 ; Move a piece from the top of one tower
116 ; to the top of another
118 (define move-delay 10)
120 (define move-piece (lambda (from to)
121 (let* ((from-tower (list-ref towers from))
122 (to-tower (list-ref towers to))
123 (piece (car from-tower)))
124 (set! from-tower (cdr from-tower))
125 (set! to-tower (cons piece to-tower))
126 (set! towers (replace towers from from-tower))
127 (set! towers (replace towers to to-tower))
134 ; The implementation of the game
136 (define _hanoi (lambda (n from to use)
141 (_hanoi (one- n) from use to)
142 (_hanoi 1 from to use)
143 (_hanoi (one- n) use to from)
149 ; A pretty interface which
150 ; resets the state of the game,
151 ; clears the screen and runs
154 (define hanoi (lambda ()
156 (set! len (reset-towers))