Debian Codegraph @

hanoi2a.bash

raw symbols SCIP
1 #! /bin/bash
2 # The Towers Of Hanoi
3 # Original script (hanoi.bash) copyright (C) 2000 Amit Singh.
4 # All Rights Reserved.
5 # http://hanoi.kernelthread.com
6
7 # hanoi2.bash
8 # Version 2: modded for ASCII-graphic display.
9 # Uses code contributed by Antonio Macchi,
10 #+ with heavy editing by ABS Guide author.
11 # This variant also falls under the original copyright, see above.
12 # Used in ABS Guide with Amit Singh's permission (thanks!).
13
14
15 # Variables #
16 E_NOPARAM=86
17 E_BADPARAM=87 # Illegal no. of disks passed to script.
18 E_NOEXIT=88
19 DELAY=2 # Interval, in seconds, between moves. Change, if desired.
20 DISKS=$1
21 Moves=0
22
23 MWIDTH=7
24 MARGIN=2
25 # Arbitrary "magic" constants, work okay for relatively small # of disks.
26 # BASEWIDTH=51 # Original code.
27 let "basewidth = $MWIDTH * $DISKS + $MARGIN" # "Base" beneath rods.
28 # Above "algorithm" could likely stand improvement.
29
30 # Display variables.
31 let "disks1 = $DISKS - 1"
32 let "spaces1 = $DISKS"
33 let "spaces2 = 2 * $DISKS"
34
35 let "lastmove_t = $DISKS - 1" # Final move?
36
37
38 declare -a Rod1 Rod2 Rod3
39
40 #################
41
42
43 function repeat { # $1=char $2=number of repetitions
44 local n # Repeat-print a character.
45
46 for (( n=0; n<$2; n++ )); do
47 echo -n "$1"
48 done
49 }
50
51 function FromRod {
52 local rod summit weight sequence
53
54 while true; do
55 rod=$1
56 test ${rod/[^123]/} || continue
57
58 sequence=$(echo $(seq 0 $disks1 | tac))
59 for summit in $sequence; do
60 eval weight=\${Rod${rod}[$summit]}
61 test $weight -ne 0 &&
62 { echo "$rod $summit $weight"; return; }
63 done
64 done
65 }
66
67
68 function ToRod { # $1=previous (FromRod) weight
69 local rod firstfree weight sequence
70
71 while true; do
72 rod=$2
73 test ${rod/[^123]} || continue
74
75 sequence=$(echo $(seq 0 $disks1 | tac))
76 for firstfree in $sequence; do
77 eval weight=\${Rod${rod}[$firstfree]}
78 test $weight -gt 0 && { (( firstfree++ )); break; }
79 done
80 test $weight -gt $1 -o $firstfree = 0 &&
81 { echo "$rod $firstfree"; return; }
82 done
83 }
84
85
86 function PrintRods {
87 local disk rod empty fill sp sequence
88
89 tput cup 5 0
90
91 repeat " " $spaces1
92 echo -n "|"
93 repeat " " $spaces2
94 echo -n "|"
95 repeat " " $spaces2
96 echo "|"
97
98 sequence=$(echo $(seq 0 $disks1 | tac))
99 for disk in $sequence; do
100 for rod in {1..3}; do
101 eval empty=$(( $DISKS - (Rod${rod}[$disk] / 2) ))
102 eval fill=\${Rod${rod}[$disk]}
103 repeat " " $empty
104 test $fill -gt 0 && repeat "*" $fill || echo -n "|"
105 repeat " " $empty
106 done
107 echo
108 done
109 repeat "=" $basewidth # Print "base" beneath rods.
110 echo
111 }
112
113
114 display ()
115 {
116 echo
117 PrintRods
118
119 # Get rod-number, summit and weight
120 first=( `FromRod $1` )
121 eval Rod${first[0]}[${first[1]}]=0
122
123 # Get rod-number and first-free position
124 second=( `ToRod ${first[2]} $2` )
125 eval Rod${second[0]}[${second[1]}]=${first[2]}
126
127
128 if [ "${Rod3[lastmove_t]}" = 1 ]
129 then # Last move? If yes, then display final position.
130 tput cup 0 0
131 echo; echo "+ Final Position: $Moves moves"
132 PrintRods
133 fi
134
135 sleep $DELAY
136 }
137
138 # From here down, almost the same as original (hanoi.bash) script.
139
140 dohanoi() { # Recursive function.
141 case $1 in
142 0)
143 ;;
144 *)
145 dohanoi "$(($1-1))" $2 $4 $3
146 if [ "$Moves" -ne 0 ]
147 then
148 tput cup 0 0
149 echo; echo "+ Position after move $Moves"
150 fi
151 ((Moves++))
152 echo -n " Next move will be: "
153 echo $2 "-->" $3
154 display $2 $3
155 dohanoi "$(($1-1))" $4 $3 $2
156 ;;
157 esac
158 }
159
160 setup_arrays ()
161 {
162 local dim n elem
163
164 let "dim1 = $1 - 1"
165 elem=$dim1
166
167 for n in $(seq 0 $dim1)
168 do
169 let "Rod1[$elem] = 2 * $n + 1"
170 Rod2[$n]=0
171 Rod3[$n]=0
172 ((elem--))
173 done
174 }
175
176
177 ### Main ###
178
179 trap "tput cnorm" 0
180 tput civis
181 clear
182
183 setup_arrays $DISKS
184
185 tput cup 0 0
186 echo; echo "+ Start Position"
187
188 case $# in
189 1) case $(($1>0)) in # Must have at least one disk.
190 1)
191 disks=$1
192 dohanoi $1 1 3 2
193 # Total moves = 2^n - 1, where n = # of disks.
194 echo
195 exit 0;
196 ;;
197 *)
198 echo "$0: Illegal value for number of disks";
199 exit $E_BADPARAM;
200 ;;
201 esac
202 ;;
203 *)
204 echo "usage: $0 N"
205 echo " Where \"N\" is the number of disks."
206 exit $E_NOPARAM;
207 ;;
208 esac
209
210 exit $E_NOEXIT # Shouldn't exit here.
211
212 # Exercise:
213 # --------
214 # There is a minor bug in the script that causes the display of
215 #+ the next-to-last move to be skipped.
216 #+ Fix this.