人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2022-02-06 16:56 | 文章來源:gibhub

一個簡單的linux猜數(shù)字小游戲源碼

游戲規(guī)則:

猜數(shù)字游戲通常由兩個人玩,一方出數(shù)字,一方猜。出數(shù)字的人要想好一個沒有重復(fù)數(shù)字的 4 個數(shù),不能讓猜的人知道。猜的人就可以開始猜。每猜一個數(shù)字,出數(shù)者就要根據(jù)這個數(shù)字給出幾 A 幾 B,其中 A 前面的數(shù)字表示位置正確的數(shù)的個數(shù),而 B 前的數(shù)字表示數(shù)字正確而位置不對的數(shù)的個數(shù)。如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個 5 的位置對了,記為 1A,而 3 和 4 這兩個數(shù)字對了,而位置沒對,因此記為 2B,合起來就是 1A2B。接著猜的人再根據(jù)出題者的幾 A 幾 B 繼續(xù)猜,直到猜中(即 4A0B)為止。

猜的人有 8 次機(jī)會。

例如:

乙出一個數(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)值,測試需要,開發(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

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

海外服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實(shí)時開通

自選配置、實(shí)時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部