趣味 Shell 脚本游戏大揭秘
1. 美国州首府问答游戏
1.1 游戏介绍与准备
当你拥有从文件中随机选择一行内容的工具时,就能编写各种问答游戏。这里有一个美国 50 个州首府的问答游戏,所需的数据文件state.capitals.txt可从 http://www.nostarch.com/wcss2/ 下载。下载后将文件保存到/usr/lib/games目录。
1.2 代码实现
#!/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