The generated password and user input can be repeated.
Therefore, it may be much more difficult to guess general rules.
Current version rules:
A -- number pair, location pair
B -- after excluding the result of A, the number is correct, but the position is incorrect.
Initialize a four-digit repeatable number systematically, such as "1223 ". If you enter "1234" for the first time, the system will prompt "2A1B". The first two digits "12" are the same and the locations are also the same, which is "2A ". In the last two digits, the "3" entered by the user is the same as the "3" in the password, but the two are in different positions. The result is "1B" and the final result is "2A1B ".
Assume that the user enters "1232" at this time, and the result is "2A2B". The calculation method is the same as the previous one.
The Code is as follows:
#!/bin/bashclearechoecho "###################################################################"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 INPUTdeclare PASSWORDdeclare Adeclare Bdeclare Xdeclare Ydeclare LOOP#This funtion init the variable PASSWORD that user need to guessinit_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 keyboardinput(){ 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 funtionjudge(){ 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 valuejudge_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 valuejudge_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 scriptLOOP=1init_passwordecho "#############################################"echo "#congratulations!You have tried $LOOP times! #"echo "# The password is $PASSWORD ! #"echo "#############################################"