shell编程实现与机器猜拳游戏(石头剪刀布)

发布于:2021-10-25 01:56:29

运行效果


代码

#!/bin/bash
#猜拳
echo ""
echo "游戏开始啦!!!"
echo ""
logo=1
while [ logo ]
do
current_time=$(date "+%Y-%m-%d %H:%M:%S")
echo $current_time
rebot_type="" #机器人选择变量
rebot_num=$(($RANDOM%3+1)) #随机选择
echo “机器人已选择好”
if [ $rebot_num -eq 1 ]; then
rebot_type="石头"
elif [ $rebot_num -eq 2 ]; then
rebot_type="剪刀"
elif [ $rebot_num -eq 3 ]; then
rebot_type="布"
else
rebot_type="输入错误"
continue
fi

echo "1-石头 2-剪刀 3-布 0-退出" #用户输入
read -p "请输入:" user_num
user_type="";

if [ -z $user_num ]; then
echo "输入不能为空"
echo ""
continue
fi

if [ $user_num == 1 ]; then
user_type="石头"
elif [ $user_num == 2 ]; then
user_type="剪刀"
elif [ $user_num == 3 ]; then
user_type="布"
elif [ $user_num == 0 ]; then
echo "拜拜嘞!您"
break
else
echo "输入错误"
echo ""
continue
fi

echo "机器出的:$rebot_type"
echo "您出的:$user_type"
if [ $user_num -eq $rebot_num ]; then
echo "*局"
elif [ $user_num == 1 ] && [ $rebot_num == 2 ]; then
echo "您赢了,牛逼!"
elif [ $user_num == 2 ] && [ $rebot_num == 3 ]; then
echo "您赢了,牛逼!"
elif [ $user_num == 3 ] && [ $rebot_type == 1 ]; then
echo "您赢了,牛逼!"
else
echo "您失败了,菜!"
fi
echo ""
done

相关推荐

最新更新

猜你喜欢