Linux implements the source code of the number guessing game

Linux implements the source code of the number guessing game

A simple Linux guessing game source code

Game rules:

The number guessing game is usually played by two people, one person gives a number and the other guesses. The person who gives the numbers must think of a set of 4 numbers without any repeated digits, and must not let the person who is guessing know the numbers. The person who guesses can start guessing. Every time a number is guessed, the person who gives the number must give a number of A and B based on this number, where the numbers before A represent the number of numbers in the correct position, and the numbers before B represent the number of numbers that are correct but in the wrong position. If the correct answer is 5234, and the guesser guesses 5346, then it is 1A2B, where one 5 is in the correct position, so it is recorded as 1A, and the numbers 3 and 4 are correct but not in the correct position, so it is recorded as 2B, which together is 1A2B. The next person to guess continues to guess based on the A and B numbers given by the person who asked the question until he guesses correctly (that is, 4A0B).

The person who guesses has 8 chances.

For example:

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

Source code:

#!/bin/bash
clear
echo
echo "# ...
echo "# this is a bash-shell game written by lee #"
echo "# this game is infinite frequency guess the number#"
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 to create a random number
random_number()
{
 PASSWORD=$RANDOM
 LEN_PWD=`echo $PASSWORD | wc -L`
 if [[ $LEN_PWD -ne 4 ]]
 then
 random_number
 else
 #Output standard value, required for testing, comment out echo $PASSWORD after development
 input
 fi
}

#this function is to accept the input from the user's keyboard
input()
{
 read -n4 -p "please input a number between 0000-9999:" input
# 10#${input} base conversionif [[ 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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • 101 shell script guessing game codes
  • Shell script to implement the number guessing game

<<:  Practical skills that must be mastered in calling UNI-APP components in the easycom mode

>>:  MySQL 8.0.18 uses clone plugin to rebuild MGR implementation

Recommend

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...

MySQL full-text fuzzy search MATCH AGAINST method example

MySQL 4.x and above provide full-text search supp...

On Visual Design and Interaction Design

<br />In the entire product design process, ...

How to quickly delete all tables in MySQL without deleting the database

This article uses an example to describe how to q...

MySQL Series 3 Basics

Table of contents Tutorial Series 1. Introduction...

How to use time as a judgment condition in MySQL

Background: During the development process, we of...

Detailed explanation of Vue development Sort component code

Table of contents <template> <ul class=&...

JavaScript function syntax explained

Table of contents 1. Ordinary functions 2. Arrow ...

Understanding the MySQL query optimization process

Table of contents Parsers and preprocessors Query...

Some references about colors in HTML

In HTML, colors are represented in two ways. One i...

Implementation code for adding links to FLASH through HTML (div layer)

Today a client wants to run an advertisement, and ...

Echarts tutorial on how to implement tree charts

Treemaps are mainly used to visualize tree-like d...

Example of pre-rendering method for Vue single page application

Table of contents Preface vue-cli 2.0 version vue...

WeChat applet uniapp realizes the left swipe to delete effect (complete code)

WeChat applet uniapp realizes the left swipe to d...