Debian Codegraph @

fifteen.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # fifteen.sh
3
4 # Classic "Fifteen Puzzle"
5 # Author: Antonio Macchi
6 # Lightly edited and commented by ABS Guide author.
7 # Used in ABS Guide with permission. (Thanks!)
8
9 # The invention of the Fifteen Puzzle is attributed to either
10 #+ Sam Loyd or Noyes Palmer Chapman.
11 # The puzzle was wildly popular in the late 19th-century.
12
13 # Object: Rearrange the numbers so they read in order,
14 #+ from 1 - 15: ________________
15 # | 1 2 3 4 |
16 # | 5 6 7 8 |
17 # | 9 10 11 12 |
18 # | 13 14 15 |
19 # ----------------
20
21
22 #######################
23 # Constants #
24 SQUARES=16 #
25 FAIL=70 #
26 E_PREMATURE_EXIT=80 #
27 #######################
28
29
30 ########
31 # Data #
32 ########
33
34 Puzzle=( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 " " )
35
36
37 #############
38 # Functions #
39 #############
40
41 function swapscip-shell shell abs-guide 10-4 swap().
42 {
43 local tmp
44
45 tmp=${Puzzle[$1]}
46 Puzzle[$1]=${Puzzle[$2]}
47 Puzzle[$2]=$tmp
48 }
49
50
51 function Jumblescip-shell shell abs-guide 10-4 Jumble().
52 { # Scramble the pieces at beginning of round.
53 local i pos1 pos2
54
55 for i in {1..100}
56 do
57 pos1=$(( $RANDOM % $SQUARES))
58 pos2=$(( $RANDOM % $SQUARES ))
59 swapscip-shell shell abs-guide 10-4 swap(). $pos1 $pos2
60 done
61 }
62
63
64 function PrintPuzzlescip-shell shell abs-guide 10-4 PrintPuzzle().
65 {
66 local i1 i2 puzpos
67 puzpos=0
68
69 clearscip-shell system . . clear.
70 echoscip-shell system . . echo. "Enter quit to exit."; echoscip-shell system . . echo. # Better that than Ctl-C.
71
72 echoscip-shell system . . echo. ",----.----.----.----." # Top border.
73 for i1 in {1..4}
74 do
75 for i2 in {1..4}
76 do
77 printfscip-shell system . . printf. "| %2s " "${Puzzle[$puzpos]}"
78 (( puzpos++ ))
79 done
80 echoscip-shell system . . echo. "|" # Right-side border.
81 testscip-shell system . . test. $i1 = 4 || echoscip-shell system . . echo. "+----+----+----+----+"
82 done
83 echoscip-shell system . . echo. "'----'----'----'----'" # Bottom border.
84 }
85
86
87 function GetNumscip-shell shell abs-guide 10-4 GetNum().
88 { # Test for valid input.
89 local puznum garbage
90
91 while truescip-shell system . . true.
92 do
93 echoscip-shell system . . echo. "Moves: $moves" # Also counts invalid moves.
94 read -p "Number to move: " puznum garbage
95 if [scip-shell system . . `[`. "$puznum" = "quit" ]; then echoscip-shell system . . echo.; exit $E_PREMATURE_EXIT; fi
96 testscip-shell system . . test. -z "$puznum" -o -n "${puznum//[0-9]/}" && continue
97 testscip-shell system . . test. $puznum -gt 0 -a $puznum -lt $SQUARES && break
98 done
99 return $puznum
100 }
101
102
103 function GetPosFromNumscip-shell shell abs-guide 10-4 GetPosFromNum().
104 { # $1 = puzzle-number
105 local puzpos
106
107 for puzpos in {0..15}
108 do
109 testscip-shell system . . test. "${Puzzle[$puzpos]}" = "$1" && break
110 done
111 return $puzpos
112 }
113
114
115 function Movescip-shell shell abs-guide 10-4 Move().
116 { # $1=Puzzle-pos
117 testscip-shell system . . test. $1 -gt 3 && testscip-shell system . . test. "${Puzzle[$(( $1 - 4 ))]}" = " "\
118 && swapscip-shell shell abs-guide 10-4 swap(). $1 $(( $1 - 4 )) && return 0
119 testscip-shell system . . test. $(( $1%4 )) -ne 3 && testscip-shell system . . test. "${Puzzle[$(( $1 + 1 ))]}" = " "\
120 && swapscip-shell shell abs-guide 10-4 swap(). $1 $(( $1 + 1 )) && return 0
121 testscip-shell system . . test. $1 -lt 12 && testscip-shell system . . test. "${Puzzle[$(( $1 + 4 ))]}" = " "\
122 && swapscip-shell shell abs-guide 10-4 swap(). $1 $(( $1 + 4 )) && return 0
123 testscip-shell system . . test. $(( $1%4 )) -ne 0 && testscip-shell system . . test. "${Puzzle[$(( $1 - 1 ))]}" = " " &&\
124 swap $1 $(( $1 - 1 )) && return 0
125 return 1
126 }
127
128
129 function Solvedscip-shell shell abs-guide 10-4 Solved().
130 {
131 local pos
132
133 for pos in {0..14}
134 do
135 testscip-shell system . . test. "${Puzzle[$pos]}" = $(( $pos + 1 )) || return $FAIL
136 # Check whether number in each square = square number.
137 done
138 return 0 # Successful solution.
139 }
140
141
142 ################### MAIN () #######################{
143 moves=0
144 Jumblescip-shell shell abs-guide 10-4 Jumble().
145
146 while truescip-shell system . . true. # Loop continuously until puzzle solved.
147 do
148 echoscip-shell system . . echo.; echoscip-shell system . . echo.
149 PrintPuzzlescip-shell shell abs-guide 10-4 PrintPuzzle().
150 echoscip-shell system . . echo.
151 while truescip-shell system . . true.
152 do
153 GetNumscip-shell shell abs-guide 10-4 GetNum().
154 puznum=$?
155 GetPosFromNumscip-shell shell abs-guide 10-4 GetPosFromNum(). $puznum
156 puzpos=$?
157 ((moves++))
158 Movescip-shell shell abs-guide 10-4 Move(). $puzpos && break
159 done
160 Solvedscip-shell shell abs-guide 10-4 Solved(). && break
161 done
162
163 echoscip-shell system . . echo.;echoscip-shell system . . echo.
164 PrintPuzzlescip-shell shell abs-guide 10-4 PrintPuzzle().
165 echoscip-shell system . . echo.; echoscip-shell system . . echo. "BRAVO!"; echoscip-shell system . . echo.
166
167 exit 0
168 ###################################################}
169
170 # Exercise:
171 # --------
172 # Rewrite the script to display the letters A - O,
173 #+ rather than the numbers 1 - 15.