Shell腳本實(shí)現(xiàn)的猜數(shù)字小游戲
生成的密碼和用戶(hù)輸入可以接受重復(fù)數(shù)字。
所以相對(duì)一般規(guī)則的猜數(shù)字可能難度要大不少。
本版本規(guī)則:
A--數(shù)字對(duì),位置也對(duì)
B--排除A的結(jié)果后,數(shù)字對(duì),但位置不對(duì)
開(kāi)始后,系統(tǒng)化初始化一個(gè)4位可重復(fù)數(shù)字,如“1223”。假設(shè)用戶(hù)第一次輸入“1234”,那么系統(tǒng)將提示“2A1B”,前兩位數(shù)字“12”相同并且位置也相同,為“2A”。后兩位數(shù)字中,用戶(hù)輸入的“3”與密文中“3”相同,但兩者位置不同,則為“1B”,最終結(jié)果為“2A1B”。
再假設(shè)用戶(hù)此時(shí)輸入“1232”,那么結(jié)果則為“2A2B”,計(jì)算方法與前次一樣。
代碼如下:
#!/bin/bash clear echo echo "###################################################################" 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 INPUT declare PASSWORD declare A declare B declare X declare Y declare LOOP #This funtion init the variable PASSWORD that user need to guess init_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 keyboard input() { 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 funtion judge() { 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 value judge_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 value judge_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 script LOOP=1 init_password echo "#############################################" echo "#congratulations!You have tried $LOOP times! #" echo "# The password is $PASSWORD ! #" echo "#############################################"
相關(guān)文章
shell腳本語(yǔ)言之if條件判斷語(yǔ)句實(shí)例詳解
在寫(xiě)shell腳本的時(shí)候條件判斷是最常用到的,尤其剛寫(xiě)shell腳本的時(shí)候,一些高級(jí)命令語(yǔ)法不會(huì)使用,到處都是if,下面這篇文章主要給大家介紹了關(guān)于shell腳本語(yǔ)言之if條件判斷語(yǔ)句的相關(guān)資料,需要的朋友可以參考下2022-04-04Shell中if的基本語(yǔ)法和常見(jiàn)判斷用法
這篇文章主要介紹了Shell中if的基本語(yǔ)法和常見(jiàn)判斷用法,本文講解了if的基本語(yǔ)法、對(duì)字符串的判斷、對(duì)數(shù)字的判斷、對(duì)文件屬性的判斷、邏輯判斷等內(nèi)容,需要的朋友可以參考下2015-06-06Shell腳本實(shí)現(xiàn)檢測(cè)進(jìn)程是否正在運(yùn)行
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)檢測(cè)進(jìn)程是否正在運(yùn)行,本文給出的代碼簡(jiǎn)潔清晰,并給出了使用方法,需要的朋友可以參考下2015-01-01linux bash中too many arguments問(wèn)題的解決方法
本文介紹下在linux bash shell中出現(xiàn)too many arguments問(wèn)題的解決辦法,有需要的朋友參考學(xué)習(xí)下2013-11-11Shell使用Epoch進(jìn)行日期時(shí)間轉(zhuǎn)換和計(jì)算的幾個(gè)小函數(shù)
這篇文章主要介紹了當(dāng)你遇到一個(gè)date命令不給力的系統(tǒng)時(shí),可以試試這幾個(gè)小函數(shù),需要的朋友可以參考下2016-12-12自動(dòng)化下載并檢測(cè)ftp文件備份的shell腳本
這篇文章主要介紹了自動(dòng)化下載并檢測(cè)ftp文件備份的shell腳本,需要的朋友可以參考下2016-08-08