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

Shell脚本实现的猜数字小游戏

程序员文章站 2023-11-09 11:10:46
生成的密码和用户输入可以接受重复数字。 所以相对一般规则的猜数字可能难度要大不少。 本版本规则: a--数字对,位置也对 b--排除a的结果后,数字对,但位置不对...

生成的密码和用户输入可以接受重复数字。
所以相对一般规则的猜数字可能难度要大不少。

本版本规则:

a--数字对,位置也对
b--排除a的结果后,数字对,但位置不对

开始后,系统化初始化一个4位可重复数字,如“1223”。假设用户第一次输入“1234”,那么系统将提示“2a1b”,前两位数字“12”相同并且位置也相同,为“2a”。后两位数字中,用户输入的“3”与密文中“3”相同,但两者位置不同,则为“1b”,最终结果为“2a1b”。

再假设用户此时输入“1232”,那么结果则为“2a2b”,计算方法与前次一样。

代码如下:

#!/bin/bash
clear
echo
echo "###################################################################"
echo "# this is a bash-shell game write by email:breeze7086@gmail.com #"
echo "# the game called *digits*,and this version have repeated numbers #"
echo "#              version 1.0              #"
echo "###################################################################"
echo -e "\n\n"
declare input
declare password
declare a
declare b
declare x
declare y
declare loop
#this funtion init the variable password that user need to guess
init_password()
{
    password=`echo $(($random%10000))`
    echo $password | grep '^[0-9]\{4\}$' >/dev/null 2>&1
    if [ $? != 0 ]
    then
        init_password
    else
        input
    fi
}
#this funtion accept the input from user's keyboard
input()
{
    echo -n "please input a number between 0000-9999:"
    read input
    echo $input | grep '^[0-9]\{4\}$' >/dev/null 2>&1
    if [ $? != 0 ]
    then
        echo "retry a number between 0000-9999 and do not input a char"
        input
    else
        judge
    fi
}
#this funtion is the main funtion
judge()
{
    x=$input
    y=$password
    while [ $input != $password ]
    do
        a=0
        b=0
        judge_a
        judge_b
        loop=`expr $loop + 1`
        echo "****************************"
        echo "*      "$a"a"$b"b      *"
        echo "****************************"
        input
    done
}
#this funtion count the variable a's value
judge_a()
{
        for i in `seq 4`
        do
            var_input=`expr substr "$x" $i 1`
            for j in `seq 4`
            do
                var_password=`expr substr "$y" $j 1`
                if [[ $var_input = $var_password && $var_input != "" && $var_password != "" && $i = $j ]]
                then
                    a=`expr $a + 1`
                    x=`expr substr $x 1 "$[$i-1]"``expr substr $x "$[$i+1]" 4`
                    y=`expr substr $y 1 "$[$i-1]"``expr substr $y "$[$i+1]" 4`
                    judge_a
                fi
            done
        done
}
#this funtion count the variable b's value
judge_b()
{
        for i in `seq 4`
        do
            var_input=`expr substr "$x" $i 1`
            for j in `seq 4`
            do
                var_password=`expr substr "$y" $j 1`
                if [[ $var_input = $var_password && $var_input != "" && $var_password != "" ]]
                then
                    b=`expr $b + 1`
                    x=`expr substr "$x" 1 "$[$i-1]"``expr substr "$x" "$[$i+1]" 4`
                    y=`expr substr "$y" 1 "$[$j-1]"``expr substr "$y" "$[$j+1]" 4`
                    judge_b
                fi
            done
        done
}
#this is the begin of script
loop=1
init_password
echo "#############################################"
echo "#congratulations!you have tried $loop times!  #"
echo "#    the password is $password !       #"
echo "#############################################"