博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux猜数字程序,用linux实现猜数字小游戏源码
阅读量:1544 次
发布时间:2019-04-21

本文共 1895 字,大约阅读时间需要 6 分钟。

#!/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

转载地址:http://fyzcy.baihongyu.com/

你可能感兴趣的文章
时间序列分析之熵(Entropy)
查看>>
时间序列分析之排列熵(Permutation Entropy)
查看>>
时间序列分析之增长熵(Increment Entropy)
查看>>
关于人工智能在量化领域应用的一些思考
查看>>
《Option Volatility & Pricing》阅读笔记之 Financial Contracts(金融合约)
查看>>
《Option Volatility & Pricing》阅读笔记之 Option Terminology (期权术语)
查看>>
《Option Volatility & Pricing》阅读笔记之 Theoretical Pricing Model(理论定价模型)
查看>>
《Option Volatility & Pricing》阅读笔记之 Volatility(波动率)
查看>>
联合熵(joined entropy)、条件熵(conditional entropy)、相对熵(relative entropy)、互信息(mutual information)以及相关关系整理
查看>>
平均值和方差的递推公式以及python实现
查看>>
超限学习机(ELM)、在线顺序超限学习机(OS-ELM)
查看>>
数据异常检测方法以及实际应用
查看>>
单峰分布(unimodal distribution)、双峰分布 (bimodal distribution)以及偏态分布(skewness distribution)
查看>>
《概率论与数理统计》之概率函数、概率分布函数与概率密度函数理解
查看>>
集中不等式 (Concentration inequality)
查看>>
《概率论与数理统计》之 typeⅠ、typeⅡ(第一类错误和第二类错误)理解
查看>>
机器学习中参数模型和非参数模型理解
查看>>
深入理解机器学习中的:目标函数,损失函数和代价函数
查看>>
控制图(control chart)以及评估指标(ARL、ATS)
查看>>
统计假设检验之显著性检验(significance test)
查看>>