linux猜数字程序,用linux实现猜数字小游戏源码

#!/bin/bash

clear

echo

echo "###################################################################"

echo "# this is a bash-shell game write by lee       #"

echo "# this game is infinite frequency 猜数字       #"

echo "#    version 2.1.1.20200421        #"

echo "###################################################################"

echo -e "\n\n"

declare INPUT

declare PASSWORD

declare LEN_PWD

declare A

declare B

declare LOOP

#this function is create random number

random_number()

{

PASSWORD=$RANDOM

LEN_PWD=`echo $PASSWORD | wc -L`

if [[ $LEN_PWD -ne 4 ]]

then

random_number

else

#输出标准值,测试需要,开发完成注释掉

echo $PASSWORD

input

fi

}

#this function is accept the input from user's keyboard

input()

{

read -n4 -p "please input a number between 0000-9999:" input

# 10#${input} 进制转换

if [[ 10#${input} -eq 10#${PASSWORD} ]]

then

echo -e "\n"

echo "#############################################"

echo "#congratulations!You have tried $LOOP times!#"

echo "# The password is $PASSWORD !   #"

echo "#############################################"

exit

elif [[ $LOOP -eq 6 ]]

then

echo -e "\n"

echo "You have tried $LOOP times!Game over!"

exit

else

A=0

B=0

count_a

count_b

echo -e "\n"

echo "****************************"

echo "*  "$A"A"$B"B   *"

echo "****************************"

echo "You have tried $LOOP times! You left `expr 6 - $LOOP` times!"

LOOP=`expr $LOOP + 1`

input

fi

}

#this function is count the variable A's value

count_a()

{

for i in `seq 4`

do

VAR_INPUT=`expr substr $input $i 1`

for j in `seq 4`

do

VAR_PASSWORD=`expr substr $PASSWORD $j 1`

if [[ $VAR_INPUT -eq $VAR_PASSWORD ]] && [[ $i -eq $j ]]

then A=`expr $A + 1`

fi

done

done

}

#this function is count the variable B's value

count_b()

{

for i in `seq 4`

do

VAR_INPUT=`expr substr $input $i 1`

for j in `seq 4`

do

VAR_PASSWORD=`expr substr $PASSWORD $j 1`

if [[ $VAR_INPUT -eq $VAR_PASSWORD ]] && [[ $i -ne $j ]]

then B=`expr $B + 1`

fi

done

done

}

LOOP=1

random_number

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值