#!/bin/bash # unscramble - Picks a word, scrambles it, and asks the user to guess # what the original word (or phrase) was. setglobal 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 setglobal match = $[randomquote $wordlib] echo "Picked out a word!" setglobal len = $[echo $match | wc -c | sed 's/[^[:digit:]]//g] setglobal scrambled = ''""; setglobal 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 } setglobal newgame = ''""; setglobal guesses = '0'; setglobal correct = '0'; setglobal total = '0' while not test $guess = "quit" { scrambleword echo "" echo "You need to unscramble: $scrambled" setglobal guess = '"??'" ; setglobal guesses = '0' setglobal total = $shExpr(' $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 { setglobal guesses = $shExpr(' $guesses + 1 ') echo "" echo "*** You got it with tries = $(guesses)! Well done!! ***" echo "" setglobal correct = $shExpr(' $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." setglobal guesses = $shExpr(' $guesses + 1 ') } } } echo "Done. You correctly figured out $correct out of $total scrambled words." exit 0