Debian Codegraph @

fifteen.sh

raw symbols SCIP
1 #!/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 swap
42 {
43 local tmp
44
45 tmp=${Puzzle[$1]}
46 Puzzle[$1]=${Puzzle[$2]}
47 Puzzle[$2]=$tmp
48 }
49
50
51 function 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 swap $pos1 $pos2
60 done
61 }
62
63
64 function PrintPuzzle
65 {
66 local i1 i2 puzpos
67 puzpos=0
68
69 clear
70 echo "Enter quit to exit."; echo # Better that than Ctl-C.
71
72 echo ",----.----.----.----." # Top border.
73 for i1 in {1..4}
74 do
75 for i2 in {1..4}
76 do
77 printf "| %2s " "${Puzzle[$puzpos]}"
78 (( puzpos++ ))
79 done
80 echo "|" # Right-side border.
81 test $i1 = 4 || echo "+----+----+----+----+"
82 done
83 echo "'----'----'----'----'" # Bottom border.
84 }
85
86
87 function GetNum
88 { # Test for valid input.
89 local puznum garbage
90
91 while true
92 do
93 echo "Moves: $moves" # Also counts invalid moves.
94 read -p "Number to move: " puznum garbage
95 if [ "$puznum" = "quit" ]; then echo; exit $E_PREMATURE_EXIT; fi
96 test -z "$puznum" -o -n "${puznum//[0-9]/}" && continue
97 test $puznum -gt 0 -a $puznum -lt $SQUARES && break
98 done
99 return $puznum
100 }
101
102
103 function GetPosFromNum
104 { # $1 = puzzle-number
105 local puzpos
106
107 for puzpos in {0..15}
108 do
109 test "${Puzzle[$puzpos]}" = "$1" && break
110 done
111 return $puzpos
112 }
113
114
115 function Move
116 { # $1=Puzzle-pos
117 test $1 -gt 3 && test "${Puzzle[$(( $1 - 4 ))]}" = " "\
118 && swap $1 $(( $1 - 4 )) && return 0
119 test $(( $1%4 )) -ne 3 && test "${Puzzle[$(( $1 + 1 ))]}" = " "\
120 && swap $1 $(( $1 + 1 )) && return 0
121 test $1 -lt 12 && test "${Puzzle[$(( $1 + 4 ))]}" = " "\
122 && swap $1 $(( $1 + 4 )) && return 0
123 test $(( $1%4 )) -ne 0 && test "${Puzzle[$(( $1 - 1 ))]}" = " " &&\
124 swap $1 $(( $1 - 1 )) && return 0
125 return 1
126 }
127
128
129 function Solved
130 {
131 local pos
132
133 for pos in {0..14}
134 do
135 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 Jumble
145
146 while true # Loop continuously until puzzle solved.
147 do
148 echo; echo
149 PrintPuzzle
150 echo
151 while true
152 do
153 GetNum
154 puznum=$?
155 GetPosFromNum $puznum
156 puzpos=$?
157 ((moves++))
158 Move $puzpos && break
159 done
160 Solved && break
161 done
162
163 echo;echo
164 PrintPuzzle
165 echo; echo "BRAVO!"; 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.