-
Notifications
You must be signed in to change notification settings - Fork 0
/
85-states
57 lines (43 loc) · 1.71 KB
/
85-states
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
#!/bin/bash
# states - A state capital guessing game. Requires the state capitals
# data file at http://www.intuitive.com/wicked/examples/state.capitals.txt.
db="/usr/lib/games/state.capitals.txt" # Format is State[tab]City
if [ ! -r “$db” ] ; then
echo "$0: Can't open $db for reading." >&2
echo "(get http://www.intuitive.com/wicked/examples/state.capitals.txt" >&2
echo "save the file as $db and you're ready to play!)" >&2
exit 1
fi
guesses=0; correct=0; total=0
while [ "$guess" != "quit" ] ; do
thiskey="$(randomquote $db)"
# $thiskey is the selected line. Now let’s grab state and city info,
# then also have “match” as the all-lowercase version of the city name
state="$(echo $thiskey | cut -d\ -f1 | sed 's/-/ /g')"
city="$(echo $thiskey | cut -d\ -f2 | sed 's/-/ /g')"
match="$(echo $city | tr '[:upper:]' '[:lower:]')"
guess="??" ; total=$(( $total + 1 )) ;
echo ""
echo "What city is the capital of $state?"
# Main loop where all the action takes place. Script loops until
# city is correctly guessed, or the user types “next” to
# skip this one, or “quit” to quit the game
while [ "$guess" != "$match" -a "$guess" != "next" -a "$guess" != "quit" ]
do
/bin/echo -n "Answer: "
read guess
if [ "$guess" = "$match" -o "$guess" = "$city" ] ; then
echo ""
echo "*** Absolutely correct! Well done! ***"
correct=$(( $correct + 1 ))
guess=$match
elif [ "$guess" = "next" -o "$guess" = "quit" ] ; then
echo ""
echo "$city is the capital of $state." # what you SHOULD have known
else
echo "I'm afraid that's not correct."
fi
done
done
echo "You got $correct out of $total presented."
exit 0