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

运行效果

在这里插入图片描述

代码

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值