Debian Codegraph @

hanoi2a.bash

raw symbols SCIP
1 #! /bin/bashscip-shell filesystem . . `/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 repeatscip-shell shell abs-guide 10-4 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 FromRodscip-shell shell abs-guide 10-4 FromRod(). {
52 local rod summit weight sequence
53
54 while truescip-shell system . . true.; do
55 rod=$1
56 testscip-shell system . . test. ${rod/[^123]/} || continue
57
58 sequence=$(echoscip-shell system . . echo. $(seqscip-shell system . . seq. 0 $disks1 | tacscip-shell system . . tac.))
59 for summit in $sequence; do
60 eval weight=\${Rod${rod}[$summit]}
61 testscip-shell system . . test. $weight -ne 0 &&
62 { echoscip-shell system . . echo. "$rod $summit $weight"; return; }
63 done
64 done
65 }
66
67
68 function ToRodscip-shell shell abs-guide 10-4 ToRod(). { # $1=previous (FromRod) weight
69 local rod firstfree weight sequence
70
71 while truescip-shell system . . true.; do
72 rod=$2
73 testscip-shell system . . test. ${rod/[^123]} || continue
74
75 sequence=$(echoscip-shell system . . echo. $(seqscip-shell system . . seq. 0 $disks1 | tacscip-shell system . . tac.))
76 for firstfree in $sequence; do
77 eval weight=\${Rod${rod}[$firstfree]}
78 testscip-shell system . . test. $weight -gt 0 && { (( firstfree++ )); break; }
79 done
80 testscip-shell system . . test. $weight -gt $1 -o $firstfree = 0 &&
81 { echoscip-shell system . . echo. "$rod $firstfree"; return; }
82 done
83 }
84
85
86 function PrintRodsscip-shell shell abs-guide 10-4 PrintRods(). {
87 local disk rod empty fill sp sequence
88
89 tputscip-shell system . . tput. cup 5 0
90
91 repeatscip-shell shell abs-guide 10-4 repeat(). " " $spaces1
92 echoscip-shell system . . echo. -n "|"
93 repeatscip-shell shell abs-guide 10-4 repeat(). " " $spaces2
94 echoscip-shell system . . echo. -n "|"
95 repeatscip-shell shell abs-guide 10-4 repeat(). " " $spaces2
96 echoscip-shell system . . echo. "|"
97
98 sequence=$(echoscip-shell system . . echo. $(seqscip-shell system . . seq. 0 $disks1 | tacscip-shell system . . 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 repeatscip-shell shell abs-guide 10-4 repeat(). " " $empty
104 testscip-shell system . . test. $fill -gt 0 && repeatscip-shell shell abs-guide 10-4 repeat(). "*" $fill || echoscip-shell system . . echo. -n "|"
105 repeatscip-shell shell abs-guide 10-4 repeat(). " " $empty
106 done
107 echoscip-shell system . . echo.
108 done
109 repeatscip-shell shell abs-guide 10-4 repeat(). "=" $basewidth # Print "base" beneath rods.
110 echoscip-shell system . . echo.
111 }
112
113
114 displayscip-shell shell abs-guide 10-4 display(). ()
115 {
116 echoscip-shell system . . echo.
117 PrintRodsscip-shell shell abs-guide 10-4 PrintRods().
118
119 # Get rod-number, summit and weight
120 first=( `FromRoscip-shell shell abs-guide 10-4 FromRod().d $1` )
121 eval Rod${first[0]}[${first[1]}]=0
122
123 # Get rod-number and first-free position
124 second=( `ToRoscip-shell shell abs-guide 10-4 ToRod().d ${first[2]} $2` )
125 eval Rod${second[0]}[${second[1]}]=${first[2]}
126
127
128 if [scip-shell system . . `[`. "${Rod3[lastmove_t]}" = 1 ]
129 then # Last move? If yes, then display final position.
130 tputscip-shell system . . tput. cup 0 0
131 echoscip-shell system . . echo.; echoscip-shell system . . echo. "+ Final Position: $Moves moves"
132 PrintRodsscip-shell shell abs-guide 10-4 PrintRods().
133 fi
134
135 sleepscip-shell system . . sleep. $DELAY
136 }
137
138 # From here down, almost the same as original (hanoi.bash) script.
139
140 dohanoiscip-shell shell abs-guide 10-4 dohanoi().() { # Recursive function.
141 case $1 in
142 0)
143 ;;
144 *)
145 dohanoiscip-shell shell abs-guide 10-4 dohanoi(). "$(($1-1))" $2 $4 $3
146 if [scip-shell system . . `[`. "$Moves" -ne 0 ]
147 then
148 tputscip-shell system . . tput. cup 0 0
149 echoscip-shell system . . echo.; echoscip-shell system . . echo. "+ Position after move $Moves"
150 fi
151 ((Moves++))
152 echoscip-shell system . . echo. -n " Next move will be: "
153 echoscip-shell system . . echo. $2 "-->" $3
154 displayscip-shell shell abs-guide 10-4 display(). $2 $3
155 dohanoiscip-shell shell abs-guide 10-4 dohanoi(). "$(($1-1))" $4 $3 $2
156 ;;
157 esac
158 }
159
160 setup_arraysscip-shell shell abs-guide 10-4 setup_arrays(). ()
161 {
162 local dim n elem
163
164 let "dim1 = $1 - 1"
165 elem=$dim1
166
167 for n in $(seqscip-shell system . . 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 tputscip-shell system . . tput. civis
181 clearscip-shell system . . clear.
182
183 setup_arraysscip-shell shell abs-guide 10-4 setup_arrays(). $DISKS
184
185 tputscip-shell system . . tput. cup 0 0
186 echoscip-shell system . . echo.; echoscip-shell system . . 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 dohanoiscip-shell shell abs-guide 10-4 dohanoi(). $1 1 3 2
193 # Total moves = 2^n - 1, where n = # of disks.
194 echoscip-shell system . . echo.
195 exit 0;
196 ;;
197 *)
198 echoscip-shell system . . echo. "$0: Illegal value for number of disks";
199 exit $E_BADPARAM;
200 ;;
201 esac
202 ;;
203 *)
204 echoscip-shell system . . echo. "usage: $0 N"
205 echoscip-shell system . . 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.