Linux:猜数游戏脚本
2020-08-06 09:03
162 查看
猜数游戏
执行脚本会随机生成一个1-100之间的数字,由执行者猜,一共有6次机会: 如果在6次内猜对了,程序提示赢了并退出;
如果6次都没猜对则提示输了,并给出答案。 在猜的过程中程序会给出提示,如果猜的数字大于答案则提示大了; 如果所猜数字小于答案则提示小了。
如果输入为空或不是数字则会浪费一次机会。 所有的提示信息可自定义
猜数游戏也是初级编程的一个很经典的练习,下面用shell写一下:
num=$[ $RANDOM%100+1 ] for ((i=5;i>=0;i--)) do read -p "Please tell me your number:" user_num if [[ $user_num =~ ^[0-9]+$ ]] then [ $user_num -eq $num ] && echo -e "\033[5mBingo!\033[0m" && break [ $user_num -lt $num ] && echo -e "The answer is \033[31mgreater\033[0m than yours." [ $user_num -gt $num ] && echo -e "The answer is \033[31mless\033[0m than yours." fi [[ $i -ne 0 ]] && echo -e "You still have \033[41;37m$i\033[0m chances" || echo -e "\033[31mGame over!\033[0m The answer is \033[32m$num\033[0m. Be smarter next time!" done
结果如下:
[root@Li~]# bash guess.sh Please tell me your number:56 The answer is greater than yours. You still have 5 chances Please tell me your number:66 The answer is greater than yours. You still have 4 chances Please tell me your number:76 The answer is greater than yours. You still have 3 chances Please tell me your number:86 The answer is greater than yours. You still have 2 chances Please tell me your number:99 The answer is less than yours. You still have 1 chances Please tell me your number:90 Bingo!
(部分字体是有样式的。)
相关文章推荐
- 脚本编程之骰子游戏 | Linux 中国
- Linux下shell脚本制作俄罗斯方块游戏
- 网络游戏服务器开发::学习了两天python写了一个linux下自动安装程序的脚本
- 网络游戏服务器开发:脚本系统的制作,linux下使用tolua制作Lua脚本系统(不需要PKG文件)
- shell脚本实现猜数游戏
- 使用game-to-flatpak脚本将商业Linux游戏安装程序转换为Flatpak应用程序
- Linux脚本设计练习2——一个游戏菜单的简单实现
- 用 Python 脚本实现对 Linux 服务器的监控
- expect实现批量修改linux密码脚本分享
- 监控Linux网络流量的脚本
- 使用Shell脚本对Linux系统和进程资源进行监控
- linux下简单对拍shell脚本
- 6.游戏特别离不开脚本(5)-脚本的“粒度”
- Linux环境自动生成OGG监控脚本和snmp oids
- 在linux环境下,求弹珠台游戏代码。
- Linux下Maven+SVN自动打包脚本
- Bash 脚本实现每次登录到 Shell 时可以查看 Linux 系统信息
- Linux 运行shell脚本
- Linux开机自动启动脚本
- 不错的linux下通用的java程序启动脚本