Debian Codegraph @

hanoi2.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.00: modded for ASCII-graphic display.
9 # Version 2.01: fixed no command-line param bug.
10 # Uses code contributed by Antonio Macchi,
11 #+ with heavy editing by ABS Guide author.
12 # This variant falls under the original copyright, see above.
13 # Used in ABS Guide with Amit Singh's permission (thanks!).
14
15
16 ### Variables && sanity check ###
17
18 E_NOPARAM=86
19 E_BADPARAM=87 # Illegal no. of disks passed to script.
20 E_NOEXIT=88
21
22 DISKS=${1:-$E_NOPARAM} # Must specify how many disks.
23 Moves=0
24
25 MWIDTH=7
26 MARGIN=2
27 # Arbitrary "magic" constants; work okay for relatively small # of disks.
28 # BASEWIDTH=51 # Original code.
29 let "basewidth = $MWIDTH * $DISKS + $MARGIN" # "Base" beneath rods.
30 # Above "algorithm" could likely stand improvement.
31
32 ### Display variables ###
33 let "disks1 = $DISKS - 1"
34 let "spaces1 = $DISKS"
35 let "spaces2 = 2 * $DISKS"
36
37 let "lastmove_t = $DISKS - 1" # Final move?
38
39
40 declare -a Rod1 Rod2 Rod3
41
42 ### ######################### ###
43
44
45 function repeat { # $1=char $2=number of repetitions
46 local n # Repeat-print a character.
47
48 for (( n=0; n<$2; n++ )); do
49 echo -n "$1"
50 done
51 }
52
53 function FromRod {
54 local rod summit weight sequence
55
56 while true; do
57 rod=$1
58 test ${rod/[^123]/} || continue
59
60 sequence=$(echo $(seq 0 $disks1 | tac))
61 for summit in $sequence; do
62 eval weight=\${Rod${rod}[$summit]}
63 test $weight -ne 0 &&
64 { echo "$rod $summit $weight"; return; }
65 done
66 done
67 }
68
69
70 function ToRod { # $1=previous (FromRod) weight
71 local rod firstfree weight sequence
72
73 while true; do
74 rod=$2
75 test ${rod/[^123]} || continue
76
77 sequence=$(echo $(seq 0 $disks1 | tac))
78 for firstfree in $sequence; do
79 eval weight=\${Rod${rod}[$firstfree]}
80 test $weight -gt 0 && { (( firstfree++ )); break; }
81 done
82 test $weight -gt $1 -o $firstfree = 0 &&
83 { echo "$rod $firstfree"; return; }
84 done
85 }
86
87
88 function PrintRods {
89 local disk rod empty fill sp sequence
90
91
92 repeat " " $spaces1
93 echo -n "|"
94 repeat " " $spaces2
95 echo -n "|"
96 repeat " " $spaces2
97 echo "|"
98
99 sequence=$(echo $(seq 0 $disks1 | tac))
100 for disk in $sequence; do
101 for rod in {1..3}; do
102 eval empty=$(( $DISKS - (Rod${rod}[$disk] / 2) ))
103 eval fill=\${Rod${rod}[$disk]}
104 repeat " " $empty
105 test $fill -gt 0 && repeat "*" $fill || echo -n "|"
106 repeat " " $empty
107 done
108 echo
109 done
110 repeat "=" $basewidth # Print "base" beneath rods.
111 echo
112 }
113
114
115 display ()
116 {
117 echo
118 PrintRods
119
120 # Get rod-number, summit and weight
121 first=( `FromRod $1` )
122 eval Rod${first[0]}[${first[1]}]=0
123
124 # Get rod-number and first-free position
125 second=( `ToRod ${first[2]} $2` )
126 eval Rod${second[0]}[${second[1]}]=${first[2]}
127
128
129 echo; echo; echo
130 if [ "${Rod3[lastmove_t]}" = 1 ]
131 then # Last move? If yes, then display final position.
132 echo "+ Final Position: $Moves moves"; echo
133 PrintRods
134 fi
135 }
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 echo "+ Position after move $Moves"
149 fi
150 ((Moves++))
151 echo -n " Next move will be: "
152 echo $2 "-->" $3
153 display $2 $3
154 dohanoi "$(($1-1))" $4 $3 $2
155 ;;
156 esac
157 }
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 setup_arrays $DISKS
180 echo; echo "+ Start Position"
181
182 case $# in
183 1) case $(($1>0)) in # Must have at least one disk.
184 1)
185 disks=$1
186 dohanoi $1 1 3 2
187 # Total moves = 2^n - 1, where n = number of disks.
188 echo
189 exit 0;
190 ;;
191 *)
192 echo "$0: Illegal value for number of disks";
193 exit $E_BADPARAM;
194 ;;
195 esac
196 ;;
197 *)
198 clear
199 echo "usage: $0 N"
200 echo " Where \"N\" is the number of disks."
201 exit $E_NOPARAM;
202 ;;
203 esac
204
205 exit $E_NOEXIT # Shouldn't exit here.
206
207 # Note:
208 # Redirect script output to a file, otherwise it scrolls off display.