-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess-word.sh
executable file
·104 lines (85 loc) · 3.1 KB
/
guess-word.sh
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
#
#https://codeberg.org/ersen/bin
#
usage() {
printf "Usage: %s [OPTION]...
A word guessing game
-f FILE use FILE to pick a new word (default: /usr/share/dict/words)
-h print this help and exit\n" "$program"
}
summary() {
printf "\n\nSUMMARY: correctly guessed %i out of %i words\n" "$won" "$rounds"
}
die() {
printf "%s\n" "$@"
exit 1
} >&2
program="${0##*"/"}"
file="/usr/share/dict/words"
while getopts ":f:h" opt; do
case "$opt" in
"f" ) file="$OPTARG" ;;
"h" ) usage && exit ;;
":" ) die "$program: $OPTARG: missing argument" ;;
* ) die "$program: $OPTARG: invalid option" ;;
esac
done
if [[ ! -r "$file" ]]; then
die "$program: $file: file not readable"
fi
trap 'summary' EXIT
won="0"
rounds="0"
declare -l guess word
while true; do
# shuf [-n|--head-count]
word="$(shuf -n 1 "$file")"
letters=""
steps="${#word}"
# tr [-c|--complement]
partial="$(tr -c "\n$letters" "_" <<<"$word")"
printf "[ROUND %i]\n" "$(( ++rounds ))"
while true; do
printf "Steps from the gallows (%i): [" "$steps"
for (( i = 0; i < steps; ++i )); do
printf "#"
done
for (( i = 0; i < ${#word} - steps; ++i )); do
printf "-"
done
printf "]\nThe word so far: %s\n" "$partial"
if [[ -n "$letters" ]]; then
printf "(Guessed: %s) " "$letters"
fi
read -rp "Guess a letter: " guess
if [[ "$guess" == "$word" ]]; then
printf "You can go now! Found the word: '%s'\n\n" "$word"
(( ++won ))
break
elif (( ${#guess} != 1 )); then
printf "Guess only one letter at a time\n"
elif [[ "$letters" == *"$guess"* ]]; then
printf "Tried '%s' before\n" "$guess"
elif [[ "$word" == *"$guess"* ]]; then
# tr [-c|--complement] [-d|--delete]
letters="$(sed 's/./&\n/g' <<<"$letters$guess" | sort | tr -d '\n')"
partial="$(tr -c "\n$letters" "_" <<<"$word")"
if [[ "$partial" == "$word" ]]; then
printf "You can go now! Found the word: '%s'\n\n" "$word"
(( ++won ))
break
else
printf "The letter '%s' is found in the word\n" "$guess"
fi
elif (( steps == 1 )); then
printf "End of the line! Could not found the word '%s'\n\n" "$word"
break
else
# tr [-d|--delete]
letters="$(sed 's/./&\n/g' <<<"$letters$guess" | sort | tr -d '\n')"
printf "Try again! The letter '%s' does not appear in the word\n" "$guess"
(( --steps ))
fi
done
done