#!/bin/bash # unscramble - Picks a word, scrambles it, and asks the user to guess # what the original word (or phrase) was. setvar wordlib = ""/usr/lib/games/long-words.txt"" proc scrambleword { # Pick a word randomly from the wordlib, and scramble it. # Original word is $match and scrambled word is $scrambled setvar match = "$(randomquote $wordlib)" echo "Picked out a word!" setvar len = $(echo $match | wc -c | sed 's/[^[:digit:]]//g') setvar scrambled = """"; setvar lastval = '1' for (( val=1; $val < $len ; )) do if [ $(($RANDOM % 2)) -eq 1 ] ; then scrambled=$scrambled$(echo $match | cut -c$val) else scrambled=$(echo $match | cut -c$val)$scrambled fi val=$(( $val + 1 )) done } if test ! -r $wordlib { echo "$0: Missing word library $wordlib" >&2 echo "(online: http://www.intuitive.com/wicked/examples/long-words.txt" >&2 echo "save the file as $wordlib and you're ready to play!)" >&2 exit 1 } setvar newgame = """"; setvar guesses = '0'; setvar correct = '0'; setvar total = '0' while ! test $guess = "quit" { scrambleword echo "" echo "You need to unscramble: $scrambled" setvar guess = ""??"" ; setvar guesses = '0' setvar total = $(( $total + 1 )) while [ "$guess" != "$match" -a "$guess" != "quit" -a "$guess" != "next" ] { echo "" /bin/echo -n "Your guess (quit|next) : " read guess if test $guess = $match { setvar guesses = $(( $guesses + 1 )) echo "" echo "*** You got it with tries = ${guesses}! Well done!! ***" echo "" setvar correct = $(( $correct + 1 )) } elif test $guess = "next" -o $guess = "quit" { echo "The unscrambled word was \"$match\". Your tries: $guesses" } else { echo "Nope. That's not the unscrambled word. Try again." setvar guesses = $(( $guesses + 1 )) } } } echo "Done. You correctly figured out $correct out of $total scrambled words." exit 0