Debian Codegraph @

qky.sh

raw symbols SCIP
1 #!/bin/bash
2 # qky.sh
3
4 ##############################################################
5 # QUACKEY: a somewhat simplified version of Perquackey [TM]. #
6 # #
7 # Author: Mendel Cooper <[email protected]> #
8 # version 0.1.02 03 May, 2008 #
9 # License: GPL3 #
10 ##############################################################
11
12 WLIST=/usr/share/dict/word.lst
13 # ^^^^^^^^ Word list file found here.
14 # ASCII word list, one word per line, UNIX format.
15 # A suggested list is the script author's "yawl" word list package.
16 # http://bash.deta.in/yawl-0.3.2.tar.gz
17 # or
18 # http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz
19
20 NONCONS=0 # Word not constructable from letter set.
21 CONS=1 # Constructable.
22 SUCCESS=0
23 NG=1
24 FAILURE=''
25 NULL=0 # Zero out value of letter (if found).
26 MINWLEN=3 # Minimum word length.
27 MAXCAT=5 # Maximum number of words in a given category.
28 PENALTY=200 # General-purpose penalty for unacceptable words.
29 total=
30 E_DUP=70 # Duplicate word error.
31
32 TIMEOUT=10 # Time for word input.
33
34 NVLET=10 # 10 letters for non-vulnerable.
35 VULET=13 # 13 letters for vulnerable (not yet implemented!).
36
37 declare -a Words
38 declare -a Status
39 declare -a Score=( 0 0 0 0 0 0 0 0 0 0 0 )
40
41
42 letters=( a n s r t m l k p r b c i d s i d z e w u e t f
43 e y e r e f e g t g h h i t r s c i t i d i j a t a o l a
44 m n a n o v n w o s e l n o s p a q e e r a b r s a o d s
45 t g t i t l u e u v n e o x y m r k )
46 # Letter distribution table shamelessly borrowed from "Wordy" game,
47 #+ ca. 1992, written by a certain fine fellow named Mendel Cooper.
48
49 declare -a LS
50
51 numelements=${#letters[@]}
52 randseed="$1"
53
54 instructions ()
55 {
56 clear
57 echo "Welcome to QUACKEY, the anagramming word construction game."; echo
58 echo -n "Do you need instructions? (y/n) "; read ans
59
60 if [ "$ans" = "y" -o "$ans" = "Y" ]; then
61 clear
62 echo -e '\E[31;47m' # Red foreground. '\E[34;47m' for blue.
63 cat <<INSTRUCTION1
64
65 QUACKEY is a variant of Perquackey [TM].
66 The rules are the same, but the scoring is simplified
67 and plurals of previously played words are allowed.
68 "Vulnerable" play is not yet implemented,
69 but it is otherwise feature-complete.
70
71 As the game begins, the player gets 10 letters.
72 The object is to construct valid dictionary words
73 of at least 3-letter length from the letterset.
74 Each word-length category
75 -- 3-letter, 4-letter, 5-letter, ... --
76 fills up with the fifth word entered,
77 and no further words in that category are accepted.
78
79 The penalty for too-short (two-letter), duplicate, unconstructable,
80 and invalid (not in dictionary) words is -200. The same penalty applies
81 to attempts to enter a word in a filled-up category.
82
83 INSTRUCTION1
84
85 echo -n "Hit ENTER for next page of instructions. "; read az1
86
87 cat <<INSTRUCTION2
88
89 The scoring mostly corresponds to classic Perquackey:
90 The first 3-letter word scores 60, plus 10 for each additional one.
91 The first 4-letter word scores 120, plus 20 for each additional one.
92 The first 5-letter word scores 200, plus 50 for each additional one.
93 The first 6-letter word scores 300, plus 100 for each additional one.
94 The first 7-letter word scores 500, plus 150 for each additional one.
95 The first 8-letter word scores 750, plus 250 for each additional one.
96 The first 9-letter word scores 1000, plus 500 for each additional one.
97 The first 10-letter word scores 2000, plus 2000 for each additional one.
98
99 Category completion bonuses are:
100 3-letter words 100
101 4-letter words 200
102 5-letter words 400
103 6-letter words 800
104 7-letter words 2000
105 8-letter words 10000
106 This is a simplification of the absurdly baroque Perquackey bonus
107 scoring system.
108
109 INSTRUCTION2
110
111 echo -n "Hit ENTER for final page of instructions. "; read az1
112
113 cat <<INSTRUCTION3
114
115
116 Hitting just ENTER for a word entry ends the game.
117
118 Individual word entry is timed to a maximum of 10 seconds.
119 *** Timing out on an entry ends the game. ***
120 Aside from that, the game is untimed.
121
122 --------------------------------------------------
123 Game statistics are automatically saved to a file.
124 --------------------------------------------------
125
126 For competitive ("duplicate") play, a previous letterset
127 may be duplicated by repeating the script's random seed,
128 command-line parameter \$1.
129 For example, "qky 7633" specifies the letterset
130 c a d i f r h u s k ...
131 INSTRUCTION3
132
133 echo; echo -n "Hit ENTER to begin game. "; read az1
134
135 echo -e "\033[0m" # Turn off red.
136 else clear
137 fi
138
139 clear
140
141 }
142
143
144
145 seed_random ()
146 { # Seed random number generator.
147 if [ -n "$randseed" ] # Can specify random seed.
148 then #+ for play in competitive mode.
149 # RANDOM="$randseed"
150 echo "RANDOM seed set to "$randseed""
151 else
152 randseed="$$" # Or get random seed from process ID.
153 echo "RANDOM seed not specified, set to Process ID of script ($$)."
154 fi
155
156 RANDOM="$randseed"
157
158 echo
159 }
160
161
162 get_letset ()
163 {
164 element=0
165 echo -n "Letterset:"
166
167 for lset in $(seq $NVLET)
168 do # Pick random letters to fill out letterset.
169 LS[element]="${letters[$((RANDOM%numelements))]}"
170 ((element++))
171 done
172
173 echo
174 echo "${LS[@]}"
175
176 }
177
178
179 add_word ()
180 {
181 wrd="$1"
182 local idx=0
183
184 Status[0]=""
185 Status[3]=""
186 Status[4]=""
187
188 while [ "${Words[idx]}" != '' ]
189 do
190 if [ "${Words[idx]}" = "$wrd" ]
191 then
192 Status[3]="Duplicate-word-PENALTY"
193 let "Score[0]= 0 - $PENALTY"
194 let "Score[1]-=$PENALTY"
195 return $E_DUP
196 fi
197
198 ((idx++))
199 done
200
201 Words[idx]="$wrd"
202 get_score
203
204 }
205
206 get_score()
207 {
208 local wlen=0
209 local score=0
210 local bonus=0
211 local first_word=0
212 local add_word=0
213 local numwords=0
214
215 wlen=${#wrd}
216 numwords=${Score[wlen]}
217 Score[2]=0
218 Status[4]="" # Initialize "bonus" to 0.
219
220 case "$wlen" in
221 3) first_word=60
222 add_word=10;;
223 4) first_word=120
224 add_word=20;;
225 5) first_word=200
226 add_word=50;;
227 6) first_word=300
228 add_word=100;;
229 7) first_word=500
230 add_word=150;;
231 8) first_word=750
232 add_word=250;;
233 9) first_word=1000
234 add_word=500;;
235 10) first_word=2000
236 add_word=2000;; # This category modified from original rules!
237 esac
238
239 ((Score[wlen]++))
240 if [ ${Score[wlen]} -eq $MAXCAT ]
241 then # Category completion bonus scoring simplified!
242 case $wlen in
243 3 ) bonus=100;;
244 4 ) bonus=200;;
245 5 ) bonus=400;;
246 6 ) bonus=800;;
247 7 ) bonus=2000;;
248 8 ) bonus=10000;;
249 esac # Needn't worry about 9's and 10's.
250 Status[4]="Category-$wlen-completion***BONUS***"
251 Score[2]=$bonus
252 else
253 Status[4]="" # Erase it.
254 fi
255
256
257 let "score = $first_word + $add_word * $numwords"
258 if [ "$numwords" -eq 0 ]
259 then
260 Score[0]=$score
261 else
262 Score[0]=$add_word
263 fi # All this to distinguish last-word score
264 #+ from total running score.
265 let "Score[1] += ${Score[0]}"
266 let "Score[1] += ${Score[2]}"
267
268 }
269
270
271
272 get_word ()
273 {
274 local wrd=''
275 read -t $TIMEOUT wrd # Timed read.
276 echo $wrd
277 }
278
279 is_constructable ()
280 { # This is the most complex and difficult-to-write function.
281 local -a local_LS=( "${LS[@]}" ) # Local copy of letter set.
282 local is_found=0
283 local idx=0
284 local pos
285 local strlen
286 local local_word=( "$1" )
287 strlen=${#local_word}
288
289 while [ "$idx" -lt "$strlen" ]
290 do
291 is_found=$(expr index "${local_LS[*]}" "${local_word:idx:1}")
292 if [ "$is_found" -eq "$NONCONS" ] # Not constructable!
293 then
294 echo "$FAILURE"; return
295 else
296 ((pos = ($is_found - 1) / 2)) # Compensate for spaces betw. letters!
297 local_LS[pos]=$NULL # Zero out used letters.
298 ((idx++)) # Bump index.
299 fi
300 done
301
302 echo "$SUCCESS"
303 return
304 }
305
306 is_valid ()
307 { # Surprisingly easy to check if word in dictionary ...
308 fgrep -qw "$1" "$WLIST" # ... courtesy of 'grep' ...
309 echo $?
310 }
311
312 check_word ()
313 {
314 if [ -z "$1" ]
315 then
316 return
317 fi
318
319 Status[1]=""
320 Status[2]=""
321 Status[3]=""
322 Status[4]=""
323
324 iscons=$(is_constructable "$1")
325 if [ "$iscons" ]
326 then
327 Status[1]="constructable"
328 v=$(is_valid "$1")
329 if [ "$v" -eq "$SUCCESS" ]
330 then
331 Status[2]="valid"
332 strlen=${#1}
333
334 if [ ${Score[strlen]} -eq "$MAXCAT" ] # Category full!
335 then
336 Status[3]="Category-$strlen-overflow-PENALTY"
337 return $NG
338 fi
339
340 case "$strlen" in
341 1 | 2 )
342 Status[3]="Two-letter-word-PENALTY"
343 return $NG;;
344 * )
345 Status[3]=""
346 return $SUCCESS;;
347 esac
348 else
349 Status[3]="Not-valid-PENALTY"
350 return $NG
351 fi
352 else
353 Status[3]="Not-constructable-PENALTY"
354 return $NG
355 fi
356
357 ### FIXME: Streamline the above code block.
358
359 }
360
361
362 display_words ()
363 {
364 local idx=0
365 local wlen0
366
367 clear
368 echo "Letterset: ${LS[@]}"
369 echo "Threes: Fours: Fives: Sixes: Sevens: Eights:"
370 echo "------------------------------------------------------------"
371
372
373
374 while [ "${Words[idx]}" != '' ]
375 do
376 wlen0=${#Words[idx]}
377 case "$wlen0" in
378 3) ;;
379 4) echo -n " " ;;
380 5) echo -n " " ;;
381 6) echo -n " " ;;
382 7) echo -n " " ;;
383 8) echo -n " " ;;
384 esac
385 echo "${Words[idx]}"
386 ((idx++))
387 done
388
389 ### FIXME: The word display is pretty crude.
390 }
391
392
393 play ()
394 {
395 word="Start game" # Dummy word, to start ...
396
397 while [ "$word" ] # If player just hits return (null word),
398 do #+ then game ends.
399 echo "$word: "${Status[@]}""
400 echo -n "Last score: [${Score[0]}] TOTAL score: [${Score[1]}]: Next word: "
401 total=${Score[1]}
402 word=$(get_word)
403 check_word "$word"
404
405 if [ "$?" -eq "$SUCCESS" ]
406 then
407 add_word "$word"
408 else
409 let "Score[0]= 0 - $PENALTY"
410 let "Score[1]-=$PENALTY"
411 fi
412
413 display_words
414 done # Exit game.
415
416 ### FIXME: The play () function calls too many other functions.
417 ### This verges on "spaghetti code" !!!
418 }
419
420 end_of_game ()
421 { # Save and display stats.
422
423 #######################Autosave##########################
424 savefile=qky.save.$$
425 # ^^ PID of script
426 echo `date` >> $savefile
427 echo "Letterset # $randseed (random seed) ">> $savefile
428 echo -n "Letterset: " >> $savefile
429 echo "${LS[@]}" >> $savefile
430 echo "---------" >> $savefile
431 echo "Words constructed:" >> $savefile
432 echo "${Words[@]}" >> $savefile
433 echo >> $savefile
434 echo "Score: $total" >> $savefile
435
436 echo "Statistics for this round saved in \""$savefile"\""
437 #########################################################
438
439 echo "Score for this round: $total"
440 echo "Words: ${Words[@]}"
441 }
442
443 # ---------#
444 instructions
445 seed_random
446 get_letset
447 play
448 end_of_game
449 # ---------#
450
451 exit $?
452
453 # TODO:
454 #
455 # 1) Clean up code!
456 # 2) Prettify the display_words () function (maybe with widgets?).
457 # 3) Improve the time-out ... maybe change to untimed entry,
458 #+ but with a time limit for the overall round.
459 # 4) An on-screen countdown timer would be nice.
460 # 5) Implement "vulnerable" mode of play for compatibility with classic
461 #+ version of the game.
462 # 6) Improve save-to-file capability (and maybe make it optional).
463 # 7) Fix bugs!!!
464
465 # For more info, reference:
466 # http://bash.deta.in/qky.README.html