Number games have always been a favorite of many people, and in the Linux system, there are also many fun digital games that allow us to try to challenge our brains. These games not only help us relax but also improve our logical thinking and calculation skills. This article will introduce some interesting Linux digital games, allowing you to experience different game fun while learning the Linux system.
A simple linux number guessing game source code
game rules:
Guessing number games are usually played by two people, one party gives the number and the other party guesses it. The person who guesses the numbers must think of 4 numbers without repeating numbers, and cannot let the guesser know. Anyone who guesses can start guessing. Every time a number is guessed, the person who guesses the number must give out how many A and how many B according to this number. The number before A indicates the number of numbers in the correct position, and the number before B indicates the number of numbers with correct numbers but in the wrong position. . For example, the correct answer is 5234, and the guesser guesses 5346, which is 1A2B. There is a 5 in the correct position, which is recorded as 1A, and the two numbers 3 and 4 are correct, but the positions are incorrect, so they are recorded as 2B. , together they are 1A2B. The guesser then continues to guess based on the questioner's number of A's and B's until the guess is correct (i.e. 4A0B).
The person who guesses has 8 chances.
For example:
B puts out a number and A guesses.
A B
1234 1A0B
5678 2A1B
5674 1A1B
5638 1A1B
2678 2A2B
6278 4A0B(guess correctly)
Source code:
#!/bin/bash clear echo echo "###################################################################" echo "# this is a bash-shell game write by lee #" echo "# this game is infinite frequency 猜数字 #" 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 #输出标准值,测试需要,开发完成注释掉 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} 进制转换 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
Through this article, we learned about some interesting Linux digital games, including Sudoku, Minesweeper, 2048, and more. These games are not only entertaining, but also very helpful for our intellectual development. In the Linux system, playing digital games can not only exercise your brain, but also deepen your understanding of Linux commands and operations. I hope readers can try these games, challenge themselves, learn in games, and have fun while learning!
The above is the detailed content of Challenge your brain - play the Linux numbers game. For more information, please follow other related articles on the PHP Chinese website!