欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux實(shí)現(xiàn)猜數(shù)字小游戲源碼

 更新時(shí)間:2020年04月21日 15:26:47   作者:D2cOneluo  
這篇文章主要為大家詳細(xì)介紹了linux實(shí)現(xiàn)猜數(shù)字小游戲源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一個(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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論