This program plays the Tower of Hanoi puzzle in GRAPHIC
mode, illustrating recursive programming on the TI-82.
----begin documentation----
This program shows and solves the well-known Tower of
Hanoi puzzle in GRAPHIC mode. For text mode, see
the companion program group TWRHANOI.82g
There are two parts; TWHANOIG and HANMVG. The first sets
up the game, the second is a recursive subroutine to move
the disks.
The Tower of Hanoi puzzle is as follows: There are three
vertical pegs, usually attached to a board. On the middle
peg are stacked several disks of decreasing radius (of
course each disk has a hole in its center for the peg):
| | |
| -|- | >A tower of Hanoi
| --|-- | >puzzle with 5
| ---|--- | >disks.
| ----|---- |
| -----|----- |
#########################################
The object is simple: move the tower of disks to another
peg, given two restrictions: a) You may only move ONE disk
at a time, i.e. only one disk may be un-pegged at any time,
and b) you may never put a larger disk atop a smaller one.
This problem is often used in introductory math or computing
courses to illustrate recursion (why this is so is left as
an exercise; watch how the program solves the problem).
When the program is run, it asks for the number of disks
you wish to start with (up to 7). Unlike the text version,
there is no input for delay time; this could easily be added
at the end of HANMVG.
The program then draws the pegs and disks, and after a brief
delay, the disks move from peg to peg, one at a time, with
no disk ever over a smaller disk.
Programming notes: The list L4 keeps track of how "tall" each
row is. The matrix "[A]" keeps track of what number/disk
is in which position.
The TI-82 will allow recursive program calls and
correctly maintains the program's flow of control, but
does not allow for local variables. To simulate local vari-
ables, we use the lists L1, L2, and L3.
The real variable D keeps track of the depth of recursion we
are currently at; at level D, we move L1(D) disks from peg
L2(D) to peg L3(D). Each time the subroutine is called re-
cursively, D is incremented. Each time a subroutine ter-
minates, D is decremented.
Written by Prof. Mark Janeba
Dept. of Math
Willamette University
Salem, OR 97301
e-mail: mjaneba< at >willamette.edu
web page: http://www.willamette.edu/~mjaneba/
----end documentation----
----begin ASCII----
\START82\
\COMMENT=Program file dated 04/02/94, 08:49
\NAME=TWHANOIG
\FILE=mydata\twhanoig.82P
Disp "NO. OF DISKS"
Input "(7 MAX) ",N
ClrDraw
{3,7}\->\dim [A]
Fill(0,[A])
AxesOff
FnOff
1\->\Xmin:95\->\Xmax
1\->\Ymin:63\->\Ymax
{16,48,80}\->\\L5\
Line(16,1,16,24)
Line(48,1,48,24)
Line(80,1,80,24
For(I,1,N,1)
3(I-1)+1\->\H
Line(\L5\(2)-2(N-I+1)-1,H,\L5\(2)-2,H)
Line(\L5\(2)+2(N-I+1)+1,H,\L5\(2)+2,H)
H+1\->\H
Line(\L5\(2)-2(N-I+1)-1,H,\L5\(2)-2,H)
Line(\L5\(2)+2(N-I+1)+1,H,\L5\(2)+2,H)
N-I+1\->\[A](2,I)
End
{0,N,0}\->\\L4\
1\->\D
N\->\\L1\(D)
2\->\\L2\(D):3\->\\L3\(D)
prgm[HANMVG
\STOP82\
\START82\
\COMMENT=Program file dated 04/02/94, 08:53
\NAME=\@\HANMVG
\FILE=mydata\zhanmvg.82P
If \L1\(D)>1:Then
\L1\(D)-1\->\\L1\(D+1)
\L2\(D)\->\\L2\(D+1)
\L3\(D)+1\->\X
If X=4:1\->\X
If X=\L2\(D):Then
X+1\->\X:
If X=4:1\->\X
X\->\\L3\(D+1)
Else
X\->\\L3\(D+1)
End
D+1\->\D
prgm[HANMVG
1\->\\L1\(D+1)
\L2\(D)\->\\L2\(D+1)
\L3\(D)\->\\L3\(D+1)
D+1\->\D
prgm[HANMVG
\L1\(D)-1\->\\L1\(D+1)
\L3\(D)\->\\L3\(D+1)
\L3\(D)+1\->\X
If X=4:1\->\X
If X=\L2\(D):Then
X+1\->\X
If X=4:1\->\X
End
X\->\\L2\(D+1)
D+1\->\D
prgm[HANMVG
D-1\->\D
Else
[A](\L2\(D),\L4\(\L2\(D)))\->\X
3(\L4\(\L2\(D))-1)+1\->\H
For(I,2,2X+1,1)
Pt-Off(\L5\(\L2\(D))-I,H)
Pt-Off(\L5\(\L2\(D))+I,H)
Pt-Off(\L5\(\L2\(D))+I,H+1)
Pt-Off(\L5\(\L2\(D))-I,H+1)
End
X\->\[A](\L3\(D),\L4\(\L3\(D))+1)
\L4\(\L2\(D))-1\->\\L4\(\L2\(D))
\L4\(\L3\(D))+1\->\\L4\(\L3\(D))
3(\L4\(\L3\(D))-1)+1\->\H
Line(\L5\(\L3\(D))-2X-1,H,\L5\(\L3\(D))-2,H)
Line(\L5\(\L3\(D))+2X+1,H,\L5\(\L3\(D))+2,H
Line(\L5\(\L3\(D))-2X-1,H+1,\L5\(\L3\(D))-2,H+1)
Line(\L5\(\L3\(D))+2X+1,H+1,\L5\(\L3\(D))+2,H+1)
D-1\->\D
End
\STOP82\
----end ASCII----
----begin UUE----
begin 644 twhanoig.82g
M*BI423@R*BH:"@!'