新聞中心
一個(gè)簡(jiǎn)單的linux猜數(shù)字小游戲源碼

游戲規(guī)則:
猜數(shù)字游戲通常由兩個(gè)人玩,一方出數(shù)字,一方猜。出數(shù)字的人要想好一個(gè)沒有重復(fù)數(shù)字的 4 個(gè)數(shù),不能讓猜的人知道。猜的人就可以開始猜。每猜一個(gè)數(shù)字,出數(shù)者就要根據(jù)這個(gè)數(shù)字給出幾 A 幾 B,其中 A 前面的數(shù)字表示位置正確的數(shù)的個(gè)數(shù),而 B 前的數(shù)字表示數(shù)字正確而位置不對(duì)的數(shù)的個(gè)數(shù)。如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個(gè) 5 的位置對(duì)了,記為 1A,而 3 和 4 這兩個(gè)數(shù)字對(duì)了,而位置沒對(duì),因此記為 2B,合起來(lái)就是 1A2B。接著猜的人再根據(jù)出題者的幾 A 幾 B 繼續(xù)猜,直到猜中(即 4A0B)為止。
猜的人有 8 次機(jī)會(huì)。
例如:
乙出一個(gè)數(shù)字,甲猜。
甲 乙
1234 1A0B
5678 2A1B
5674 1A1B
5638 1A1B
2678 2A2B
6278 4A0B(猜中)
源碼:
#!/bin/bash
clear
echo
echo "###################################################################"
echo "# this is a bash-shell game write by lee #"
echo "# this game is infinite frequency 猜數(shù)字 #"
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
#輸出標(biāo)準(zhǔn)值,測(cè)試需要,開發(fā)完成注釋掉
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} 進(jìn)制轉(zhuǎn)換
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本篇文章到此結(jié)束,如果您有相關(guān)技術(shù)方面疑問(wèn)可以聯(lián)系我們技術(shù)人員遠(yuǎn)程解決,感謝大家支持本站!
成都創(chuàng)新互聯(lián)科技有限公司,是一家專注于互聯(lián)網(wǎng)、IDC服務(wù)、應(yīng)用軟件開發(fā)、網(wǎng)站建設(shè)推廣的公司,為客戶提供互聯(lián)網(wǎng)基礎(chǔ)服務(wù)!
創(chuàng)新互聯(lián)(www.cdcxhl.com)提供簡(jiǎn)單好用,價(jià)格厚道的香港/美國(guó)云服務(wù)器和獨(dú)立服務(wù)器。創(chuàng)新互聯(lián)成都老牌IDC服務(wù)商,專注四川成都IDC機(jī)房服務(wù)器托管/機(jī)柜租用。為您精選優(yōu)質(zhì)idc數(shù)據(jù)中心機(jī)房租用、服務(wù)器托管、機(jī)柜租賃、大帶寬租用,可選線路電信、移動(dòng)、聯(lián)通等。
名稱欄目:linux實(shí)現(xiàn)猜數(shù)字小游戲源碼
URL分享:http://m.fisionsoft.com.cn/article/cohoddh.html


咨詢
建站咨詢
