欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用shell脚本实现石头剪刀布对战小游戏

程序员文章站 2024-02-20 11:03:40
...
#!/bin/bash

#功能描述:石头剪刀布游戏。
#计算机根据生成的随机数出拳,并提示用户出拳。
#将用户的输入与计算机产生的随机数进行比较,判断输赢。

# 1.石头;2.剪刀;3.布

computer=$[$RANDOM%3+1]
clear
echo "###################"
echo "#    石头剪刀布游戏    #"
echo -e "#\033[32m 请根据下列提示出拳: \033[0m#"
echo "###################"
echo "|-----------------|"
echo "|      1.石头     |"
echo "|      2.剪刀     |"
echo "|      2.布       |"

read -p "请输入1~3的值:" person
clear

case $person in
1)
		if [[ "$computer" == 1 ]];then
				echo "--------------"
				echo "    出拳:石头    "
				echo "计算器出拳:石头"
				echo "--------------"
				echo -e "\033[32m平局.\033[0m"
		elif [[ "$computer" == 2 ]];then
				echo "--------------"
				echo "    出拳:石头    "
				echo "计算器出拳:剪刀"
				echo "--------------"
				echo -e "\033[32m恭喜,你赢了.\033[0m"
		elif [[ "$computer" == 3 ]];then
				echo "--------------"
				echo "    出拳:石头    "
				echo "计算器出拳:布"
				echo "--------------"
				echo -e "\033[32m你输了.\033[0m"
		fi;;
2)
		if [[ "$computer" == 1 ]];then
				echo "--------------"
				echo "    出拳:剪刀    "
				echo "计算器出拳:石头"
				echo "--------------"
				echo -e "\033[32m你输了.\033[0m"
		elif [[ "$computer" == 2 ]];then
				echo "--------------"
				echo "    出拳:剪刀    "
				echo "计算器出拳:剪刀"
				echo "--------------"
				echo -e "\033[32m平局.\033[0m"
		elif [[ "$computer" == 3 ]];then
				echo "--------------"
				echo "    出拳:剪刀    "
				echo "计算器出拳:布"
				echo "--------------"
				echo -e "\033[32m恭喜,你赢了.\033[0m"
		fi;;
3)
		if [[ "$computer" == 1 ]];then
				echo "--------------"
				echo "    出拳:布    "
				echo "计算器出拳:石头"
				echo "--------------"
				echo -e "\033[32m恭喜,你赢了.\033[0m"
		elif [[ "$computer" == 2 ]];then
				echo "--------------"
				echo "    出拳:布    "
				echo "计算器出拳:剪刀"
				echo "--------------"
				echo -e "\033[32m你输了.\033[0m"
		elif [[ "$computer" == 3 ]];then
				echo "--------------"
				echo "    出拳:布    "
				echo "计算器出拳:布"
				echo "--------------"
				echo -e "\033[32m平局.\033[0m"
		fi;;
*)
		echo -e "\033[91m无效的输入值,请输入1~3范围的值\033[0m";;
esac
相关标签: shell shell