-
Notifications
You must be signed in to change notification settings - Fork 0
/
83-unscramble
72 lines (56 loc) · 1.79 KB
/
83-unscramble
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# unscramble - Picks a word, scrambles it, and asks the user to guess
# what the original word (or phrase) was.
wordlib="/usr/lib/games/long-words.txt"
scrambleword()
{
# Pick a word randomly from the wordlib, and scramble it.
# Original word is $match and scrambled word is $scrambled
match="$(randomquote $wordlib)"
echo "Picked out a word!"
len=$(echo $match | wc -c | sed 's/[^[:digit:]]//g')
scrambled=""; 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 [ ! -r $wordlib ] ; then
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
fi
newgame=""; guesses=0; correct=0; total=0
until [ "$guess" = "quit" ] ; do
scrambleword
echo ""
echo "You need to unscramble: $scrambled"
guess="??" ; guesses=0
total=$(( $total + 1 ))
while [ "$guess" != "$match" -a "$guess" != "quit" -a "$guess" != "next" ]
do
echo ""
/bin/echo -n "Your guess (quit|next) : "
read guess
if [ "$guess" = "$match" ] ; then
guesses=$(( $guesses + 1 ))
echo ""
echo "*** You got it with tries = ${guesses}! Well done!! ***"
echo ""
correct=$(( $correct + 1 ))
elif [ "$guess" = "next" -o "$guess" = "quit" ] ; then
echo "The unscrambled word was \"$match\". Your tries: $guesses"
else
echo "Nope. That's not the unscrambled word. Try again."
guesses=$(( $guesses + 1 ))
fi
done
done
echo "Done. You correctly figured out $correct out of $total scrambled words."
exit 0