-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm
executable file
·263 lines (221 loc) · 7.13 KB
/
hm
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#! /bin/bash
## hm -- A HangMan Game implemented by Bash Script.
## Comment:
# To play with this game. You should have a vocabulary file which is just
# a plain text file with each line a word.
# `hm' will pick randomly a word from this file to play with. You can query
# the word definition using external dictionary program -- the default is to
# use program 'dictd'--you can set yours by editing function 'hm_dict', but try to
# make the query answer to be output to stdout using plain text.
# Type `hm -h' to get help message.
# Enjoy!
## Code:
declare -r MAX_GUESS=7 # maximum guesses permitted.
declare -r DIC_COST=3 # the cost to query a word
declare -r VOCAB_PATH="./vocab.txt" # default vocabulary file path
declare -r non_char='*' # pint as a char which has not unrecognized.
function hm_dict { dict "$1" ;} #
declare special_chars=('-' ' ' "'") # word-constitute except for alpha.
declare vocab=${1:-$VOCAB_PATH}
declare -a words
declare select_word # the word which you are guessing
declare Num # the number of words read from vocab.
extract () {
# delete blank lines,and trailing/ending blanks of each word.
readarray -t -O 1 words <<< "$(sed -r '{/^\s*$/d; s/^\s+//; s/\s+$//}' $vocab)"
Num=${#words[@]}
}
random-in-place () # produce a random permutation of $words
{
local ran; local tmpw; local tmpb
random () # 获得一个在$1与$2间(包括边界)的随机数
{
modulus=$(($2-$1+1))
ran=$(($1+$RANDOM%$modulus))
}
for i in $(seq $Num)
do
random $i $Num # get a random var between $i and $Num
# swap
tmpw=${words[$i]}
words[$i]=${words[$ran]}
words[$ran]=$tmpw
done
}
match () # 检测单个字母是否猜中
{
local index
local tmp=$matches
output_index () { echo $select_word| fold -w 1 | sed -rn "/[$guess]/=";}
# 如果猜中,则更新matches
while read index && [[ -n $index ]];do
matches=$(sed -r "s/./$guess/$index" <<< $matches) # copy the match to corresponding index of $matches
done <<< "$(output_index)" # output the index(es) of match
[[ $tmp != $matches ]];return $? # if some changes happened,return True,otherwise,False
}
go_on_prompt () # go to next turn
{
flush () { while read -e -t1 ;do :; done;} # flush input
dic_err="查找释义失败"
flush
echo '继续?(是:default 否:n 查询:? )'
local c;read c
case $c in
'?' )
if ! hm_dict "$select_word"; then
echo dic_err
fi
go_on_prompt
;;
'n')
flush; return 1 ;;
*)
flush;return 0 ;;
esac
}
play ()
{
trash_num=0
matches="$(printf ${non_char}%.0s $(seq ${#select_word}) )" # 用non_char 装填
trash=( ) # 猜错的字母
show-dic (){
tmp="{s/$select_word/###/Ig;s/${select_word%?}/###/Ig }"
hm_dict "$select_word" 2> /dev/null |
sed -r "$tmp"
} # 屏蔽该单词,与它的变形
guesses= # initial $guesses
say_guess () {
echo "你目前的猜测: ${matches[@]} 错误猜测: ${trash[@]} 你还有$(($MAX_GUESS-$trash_num))次机会"
echo -n "猜猜我是谁? "
return 0
}
for guess in "${special_chars[@]}";do match;done # show special chars
while say_guess && read -N1 guess
do
while [[ -z $guess ]];do read -N1 guess; done # guess can't be blank
guess=${guess,,} # modify to lower case
case $guess in
[[:alpha:]] )
if [[ $guess = [$guesses] ]];then
echo 重复猜测;continue
else
guesses=$guess"$guesses" # add this guess to guesses
fi
if match;then
if [[ $matches = $select_word ]];then
echo "你成功了,你猜测的单词是$select_word"
go_on_prompt
return $?
fi
else
(( trash_num++ ))
trash[$trash_num]=$guess
if [[ $trash_num > $MAX_GUESS ]]; then
echo "你已用尽所有机会. 您猜测的单词是 $select_word"
go_on_prompt
return $?
fi
fi ;;
'?') show-dic;(( trash_num=trash_num + DIC_COST ));;
':') echo {a..z} | sed "s/[${guesses:- }]//g";; # 打印为猜测的字母
'^')
echo "您猜测的单词是 $select_word"
return 0;;
*) echo "无意义字符" ;;
esac
done
}
rounds=1 # 游戏进行的圈数
hangman ()
{
say_goodbye () { echo "何日君再来?"; exit 0;}
if [[ $rounds -gt $Num ]]
then
echo -n "词库已经用完,yesterday once more?(是:y;否:n)"
read ans
if [[ $ans = y ]]
then
exec $0
else
say_goodbye
fi
fi
select_word=${words[$rounds]}
if play
then
(( rounds++))
hangman
else
say_goodbye
fi
}
get_vocab () {
if ! [[ -f $vocab ]];then
echo "No $vocab exists";exit 1
fi
}
add_words () {
extract
for i in "$@"
do
local flag=uniq
for j in "${words[@]}";do
if [[ "$i" = "$j" ]];then
echo "- $i"
flag=repeat
break
fi
done
if [[ $flag = uniq ]];then
echo -e "\n$i" >> $vocab
echo "+ $i"
fi
done
echo done
exit 0
}
help() {
cat <<EOF
*USAGE*
Play Game:
$0 [vocab]
Get Help:
$0 [--help|help]
Add Words to vocabulary:
$0 [vocab] (-a | --add) WORD...
Note: vocab is for vocabulary file path. the default is ./vocab.txt
*PLAY RULES*
You have $MAX_GUESS chances to guess a word. You can query its definition
(the word in the output will be replaced by ### literally),and it will cost you
$DIC_COST chances.
While guessing, you can type multple guesses at once,for example,you guess this
word consists 'e','a' and 'c', it's ok to type in "eac RET" at once.
while playing, some character you input has special meaning:
? query dictionary the meaning of the word you are guessing;
: print the candidate character for guess;
^ quit guessing this word.
EOF
}
# ==============================Main==========
########################################
case $1 in
'--help'|'-h') # show help message
help
;;
'-a'|'--add')
get_vocab
add_words "${@:2}"
;;
*)
if [[ "$2" =~ '-a|--add' ]];then
get_vocab "$1"
add_words "${@:3}"
else # play game
help
get_vocab "$1"
extract
random-in-place
hangman
fi ;;
esac
## hm ends here.