Shell指令碼實現的猜數字小遊戲_linux shell

來源:互聯網
上載者:User

產生的密碼和使用者輸入可以接受重複數字。
所以相對一般規則的猜數字可能難度要大不少。

本版本規則:

A--數字對,位置也對
B--排除A的結果後,數字對,但位置不對

開始後,系統化初始化一個4位可重複數字,如“1223”。假設使用者第一次輸入“1234”,那麼系統將提示“2A1B”,前兩位元字“12”相同並且位置也相同,為“2A”。後兩位元字中,使用者輸入的“3”與密文中“3”相同,但兩者位置不同,則為“1B”,最終結果為“2A1B”。

再假設使用者此時輸入“1232”,那麼結果則為“2A2B”,計算方法與前次一樣。

代碼如下:

#!/bin/bashclearechoecho "###################################################################"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 INPUTdeclare PASSWORDdeclare Adeclare Bdeclare Xdeclare Ydeclare LOOP#This funtion init the variable PASSWORD that user need to guessinit_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 keyboardinput(){    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 funtionjudge(){    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 valuejudge_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 valuejudge_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 scriptLOOP=1init_passwordecho "#############################################"echo "#congratulations!You have tried $LOOP times!  #"echo "#    The password is $PASSWORD !       #"echo "#############################################"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.