Shell script implementation of guessing number games _linux shell

Source: Internet
Author: User

The generated password and user input can accept duplicate numbers.
So the relative general rule of guessing numbers may be much more difficult.

This version of the rule:

a--numbers are right, position is right.
b--excludes the result of a, the number is correct, but the position is not right

After the start, systematize Initializes a 4-bit repeatable number, such as "1223". Suppose the user enters "1234" for the first time, then the system prompts "2A1B", the first two digits "12" are the same and the location is the same, "2A". In the last two digits, the user enters the same "3" as "3" in the ciphertext, but the position is "1 B" and the end result is "2a1b".

Assuming that the user enters "1232" at this point, the result is "2A2B" and the calculation is the same as the previous one.

The code is as follows:

#!/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 # "echo" # version 1.0 # "echo" ############################################################### # "echo-e" \ n \ Declare INPUT declare PASSWORD declare A declare B declare X declare Y declare LOOP #This funtion INI t the variable PASSWORD that user need to guess Init_password () {password= ' echo $ (($RANDOM%10000)) ' Echo $PASSWOR D |
    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_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 ' does 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 && $V Ar_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} #This funtion count the Vari Able B ' s value judge_b () {For i in ' seq 4 ' does var_input= ' expr substr "$X" $i 1 ' F or J in ' Seq 4 ' does var_password= ' expr substr ' $Y ' $j 1 ' if [[$VAR _input = $VA R_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 sub
        Str "$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" #con gratulations!  You have tried $LOOP times!       # "echo" # The password is $PASSWORD!
# "echo" ############################################# " 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.