溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Shell中怎么實(shí)現(xiàn)一個(gè)猜數(shù)字小游戲

發(fā)布時(shí)間:2021-08-09 15:58:04 來源:億速云 閱讀:151 作者:Leah 欄目:開發(fā)技術(shù)

Shell中怎么實(shí)現(xiàn)一個(gè)猜數(shù)字小游戲,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

生成的密碼和用戶輸入可以接受重復(fù)數(shù)字。
所以相對(duì)一般規(guī)則的猜數(shù)字可能難度要大不少。

本版本規(guī)則:

A--數(shù)字對(duì),位置也對(duì)
B--排除A的結(jié)果后,數(shù)字對(duì),但位置不對(duì)

開始后,系統(tǒng)化初始化一個(gè)4位可重復(fù)數(shù)字,如“1223”。假設(shè)用戶第一次輸入“1234”,那么系統(tǒng)將提示“2A1B”,前兩位數(shù)字“12”相同并且位置也相同,為“2A”。后兩位數(shù)字中,用戶輸入的“3”與密文中“3”相同,但兩者位置不同,則為“1B”,最終結(jié)果為“2A1B”。

再假設(shè)用戶此時(shí)輸入“1232”,那么結(jié)果則為“2A2B”,計(jì)算方法與前次一樣。

代碼如下:

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

看完上述內(nèi)容,你們掌握Shell中怎么實(shí)現(xiàn)一個(gè)猜數(shù)字小游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI