linux realizes guessing digital game source code

  • 2021-07-26 09:12:55
  • OfStack

A simple linux guessing numbers game source code

Rules of the game:

The number guessing game is usually played by two people, one side gives the number and the other side guesses. The person who gives the number should think of a 4 numbers without repeating the number, and can't let the guesser know. He who guesses can start guessing. Every time a number is guessed, the counting person will give several A and several B according to this number, where the number before A indicates the number of numbers with correct position, while the number before B indicates the number of numbers with correct position but wrong position. If the correct answer is 5234, and the guesser guesses 5346, it is 1A2B, in which one 5 has the right position, which is recorded as 1A, while the two numbers 3 and 4 are correct, but the position is not correct, so it is recorded as 2B, which together is 1A2B. Then the guesser continues to guess according to the number of A and B of the questioner until the guess is correct (i.e. 4A0B).

Guess people have 8 chances.

For example:

B gives a number, and A guesses.
A and B
1234 1A0B
5678 2A1B
5674 1A1B
5638 1A1B
2678 2A2B
6278 4A0B (Guess Right)

Source code:


#!/bin/bash
clear
echo
echo "###################################################################"
echo "# this is a bash-shell game write by lee       #"
echo "# this game is infinite frequency  Guess numbers        #"
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
 # Output standard values, test needs, and comment out after development 
 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}  Binary conversion 
 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

Related articles: