趣味Shell脚本游戏大揭秘
1. 美国州首府问答游戏
当你拥有从文件中随机选择一行的工具时,你可以编写的问答游戏类型便没有限制了。这里有一个美国50个州首府的问答游戏脚本。
-数据准备:从 http://www.nostarch.com/wcss2/ 下载state.capitals.txt文件,并将其保存到/usr/lib/games目录下。该文件包含州名和对应的首府名,州名和首府名之间用制表符分隔,且所有空格都被替换为了连字符。
-代码实现:
#!/bin/bash # states--A state capital guessing game. Requires the state capitals # data file 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 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 whi