小編這次要給大家分享的是linux如何實(shí)現(xiàn)猜數(shù)字小游戲,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

游戲規(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,合起來就是 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
標(biāo)題名稱:linux如何實(shí)現(xiàn)猜數(shù)字小游戲-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article22/dosjcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計(jì)公司、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容