[Comprehensive use of shell 2] guessing game (rock, scissors, cloth) with python comparison

First of all, for the realization of boxing guessing, arrays and numbers can be used for direct input and output, but it is still a little more difficult to generate strings. In this project, Python has certain advantages

First look at the overall Python code

1. Python code

import random as r
f = ["布", "石头", "剪刀"]
while True:
    n = input("猜拳(石头,剪刀,布):")
    a = str(r.randint(0, 2))
    if n == "剪刀":
        n = '2'
    elif n == "石头":
        n = '1'
    elif n == "布":
        n = '0'
    else:
        print("输入有误!")
        exit()
    sys = eval(a)-eval(n)
    if a > n and sys == 1 or sys == -2:
        print(f'系统出了{f[int(a)]}')
        print("you win")
    elif n == a:
        print(f'系统出了{f[int(a)]}')
        print("none winner")
    else:
        print(f'系统出了{f[int(a)]}')
        print("you lose~!")

operation result:

In a few words:

1.import After importing the module as, it is an alias, here is r

2. Then define the sequence, here I use a list of strings

3. Then use input to get user input and copy it to n

4. Call randint in the random module to randomly generate an integer between 0 and 2

5. Judge the input and pass parameters

6. According to the law, it can be found that there is a rule in defining scissors as 2, rock as 1, and cloth as 0. When the interval value is 1, whoever is bigger will lose, and when the interval is 2, it will also occur once. Judge this and Pass the result in the form of an array. Note that the subscript in the array must be of int type, so a forced conversion is required. The eval function is used in the calculation, which can be simply understood as removing quotation marks

2. Then the following is the code of the shell

1. The function of stty erase ^H is to back and roll back when the input is wrong, because under normal circumstances, using back will generate a series of ^H

2. The idea is the same as Python, but special attention should be paid to the format. If there are more spaces and less spaces, it may not work.

3. Use echo $((RANDOM%3)) to randomly generate the value of 0 1 2. If you want to generate 1-100, it is:

echo $((1+RANDOM%100))

4. Set the array definition scissors, rock, cloth

5. $a-$n|bc submits the values ​​of two variables to bc for operation

6. -gt in if [ ] is greater than -eq is equal to this and -lt is less than -ne is not equal to -ge is greater than or equal to le is less than or equal to

7. && and || are logical operators who understand everything


#!/bin/bash
stty erase ^H
while [ 1 ]
do
read -p "猜拳(1石头,2剪刀,0布):" n
a=`echo $((RANDOM%3))`
f[0]="布";f[1]="石头";f[2]="剪刀"
if [ $n == "剪刀" ];then
n=2
elif [ $n  == "石头" ];then
n=1
elif [ $n == "布" ];then
n=0
else echo"输入有误!"
fi
sys=`echo "$a-$n"|bc`
if [ $a -gt $n ] && [ $sys -eq 1 ] || [ $sys -eq -2 ];then
        echo -e "系统出了`echo ${f[$a]}`\n you win"
elif [ $n -eq $a ];then
        echo -e "系统出了`echo ${f[$a]}`\n none winner"
else
        echo -e "系统出了`echo ${f[$a]}`\n you lose~!"
fi
done

The result of the operation is as follows

 Summary: When inputting strings and passing values, it is necessary to convert the parameters into numerical values ​​first, so that they can be used in array subscripts and output in the form of arrays, otherwise more judgment is required

Guess you like

Origin blog.csdn.net/qq_53521409/article/details/126573822