Debian Codegraph @

hanoi2.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.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 repeatscip-shell shell abs-guide 10-4 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 FromRodscip-shell shell abs-guide 10-4 FromRod(). {
54 local rod summit weight sequence
55
56 while truescip-shell system . . true.; do
57 rod=$1
58 testscip-shell system . . test. ${rod/[^123]/} || continue
59
60 sequence=$(echoscip-shell system . . echo. $(seqscip-shell system . . seq. 0 $disks1 | tacscip-shell system . . tac.))
61 for summit in $sequence; do
62 eval weight=\${Rod${rod}[$summit]}
63 testscip-shell system . . test. $weight -ne 0 &&
64 { echoscip-shell system . . echo. "$rod $summit $weight"; return; }
65 done
66 done
67 }
68
69
70 function ToRodscip-shell shell abs-guide 10-4 ToRod(). { # $1=previous (FromRod) weight
71 local rod firstfree weight sequence
72
73 while truescip-shell system . . true.; do
74 rod=$2
75 testscip-shell system . . test. ${rod/[^123]} || continue
76
77 sequence=$(echoscip-shell system . . echo. $(seqscip-shell system . . seq. 0 $disks1 | tacscip-shell system . . tac.))
78 for firstfree in $sequence; do
79 eval weight=\${Rod${rod}[$firstfree]}
80 testscip-shell system . . test. $weight -gt 0 && { (( firstfree++ )); break; }
81 done
82 testscip-shell system . . test. $weight -gt $1 -o $firstfree = 0 &&
83 { echoscip-shell system . . echo. "$rod $firstfree"; return; }
84 done
85 }
86
87
88 function PrintRodsscip-shell shell abs-guide 10-4 PrintRods(). {
89 local disk rod empty fill sp sequence
90
91
92 repeatscip-shell shell abs-guide 10-4 repeat(). " " $spaces1
93 echoscip-shell system . . echo. -n "|"
94 repeatscip-shell shell abs-guide 10-4 repeat(). " " $spaces2
95 echoscip-shell system . . echo. -n "|"
96 repeatscip-shell shell abs-guide 10-4 repeat(). " " $spaces2
97 echoscip-shell system . . echo. "|"
98
99 sequence=$(echoscip-shell system . . echo. $(seqscip-shell system . . seq. 0 $disks1 | tacscip-shell system . . 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 repeatscip-shell shell abs-guide 10-4 repeat(). " " $empty
105 testscip-shell system . . test. $fill -gt 0 && repeatscip-shell shell abs-guide 10-4 repeat(). "*" $fill || echoscip-shell system . . echo. -n "|"
106 repeatscip-shell shell abs-guide 10-4 repeat(). " " $empty
107 done
108 echoscip-shell system . . echo.
109 done
110 repeatscip-shell shell abs-guide 10-4 repeat(). "=" $basewidth # Print "base" beneath rods.
111 echoscip-shell system . . echo.
112 }
113
114
115 displayscip-shell shell abs-guide 10-4 display(). ()
116 {
117 echoscip-shell system . . echo.
118 PrintRodsscip-shell shell abs-guide 10-4 PrintRods().
119
120 # Get rod-number, summit and weight
121 first=( `FromRoscip-shell shell abs-guide 10-4 FromRod().d $1` )
122 eval Rod${first[0]}[${first[1]}]=0
123
124 # Get rod-number and first-free position
125 second=( `ToRoscip-shell shell abs-guide 10-4 ToRod().d ${first[2]} $2` )
126 eval Rod${second[0]}[${second[1]}]=${first[2]}
127
128
129 echoscip-shell system . . echo.; echoscip-shell system . . echo.; echoscip-shell system . . echo.
130 if [scip-shell system . . `[`. "${Rod3[lastmove_t]}" = 1 ]
131 then # Last move? If yes, then display final position.
132 echoscip-shell system . . echo. "+ Final Position: $Moves moves"; echoscip-shell system . . echo.
133 PrintRodsscip-shell shell abs-guide 10-4 PrintRods().
134 fi
135 }
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 echoscip-shell system . . echo. "+ Position after move $Moves"
149 fi
150 ((Moves++))
151 echoscip-shell system . . echo. -n " Next move will be: "
152 echoscip-shell system . . echo. $2 "-->" $3
153 displayscip-shell shell abs-guide 10-4 display(). $2 $3
154 dohanoiscip-shell shell abs-guide 10-4 dohanoi(). "$(($1-1))" $4 $3 $2
155 ;;
156 esac
157 }
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 setup_arraysscip-shell shell abs-guide 10-4 setup_arrays(). $DISKS
180 echoscip-shell system . . echo.; echoscip-shell system . . 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 dohanoiscip-shell shell abs-guide 10-4 dohanoi(). $1 1 3 2
187 # Total moves = 2^n - 1, where n = number of disks.
188 echoscip-shell system . . echo.
189 exit 0;
190 ;;
191 *)
192 echoscip-shell system . . echo. "$0: Illegal value for number of disks";
193 exit $E_BADPARAM;
194 ;;
195 esac
196 ;;
197 *)
198 clearscip-shell system . . clear.
199 echoscip-shell system . . echo. "usage: $0 N"
200 echoscip-shell system . . 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.