本人学习shell中,类似 md5大决战 写的小游戏,在此有很多问题搞不明白。想请教一下各位大侠。
1,好像是这行
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100]
有时会出现基数过大的错误,不知道为什么?
2,(( )) 和 $[ ] 到底有什么区别?怎么有时可以互换,有时不行?
3,shell 没有形参实参的说法?对传入的$1的更改,貌似直接就改了原数值
[问题]shell 写的小游戏
- liujingjing5
- 帖子: 512
- 注册时间: 2006-10-12 1:21
- liujingjing5
- 帖子: 512
- 注册时间: 2006-10-12 1:21
#!/bin/bash
#说明:运行方法 例如 ./fight 张三 李四
sleep 1
a1=$1 #a为名字
a2=$2
n1=$(echo $1 | md5sum) #n为名字的md5值
n2=$(echo $2 | md5sum)
########################################################################################################
a1_strength=$(echo $n1 | awk '{if(str=(substr($0,1,8))) print str}')#力量为md5前8位
a1_brain=$(echo $n1 | awk '{if(str=(substr($0,9,8))) print str}') #智力为9~16
a1_speed=$(echo $n1 | awk '{if(str=(substr($0,17,8))) print str}') #敏捷为17~24
a1_luck=$(echo $n1 | awk '{if(str=(substr($0,25,8))) print str}') #人品为25~32
a2_strength=$(echo $n2 | awk '{if(str=(substr($0,1,8))) print str}')
a2_brain=$(echo $n2 | awk '{if(str=(substr($0,9,8))) print str}')
a2_speed=$(echo $n2 | awk '{if(str=(substr($0,17,8))) print str}')
a2_luck=$(echo $n2 | awk '{if(str=(substr($0,25,8))) print str}')
#########################################################################################################
function getmd5() #将各属性的md5值转成两位整数
{
string=$(eval echo \$$1)
echo -en $string
str1=$(echo $string | awk '{if(str=(substr($0,1,1))) print str}')
str2=$(echo $string | awk '{if(str=(substr($0,2,1))) print str}')
str3=$(echo $string | awk '{if(str=(substr($0,3,1))) print str}')
str4=$(echo $string | awk '{if(str=(substr($0,4,1))) print str}')
str5=$(echo $string | awk '{if(str=(substr($0,5,1))) print str}')
str6=$(echo $string | awk '{if(str=(substr($0,6,1))) print str}')
str7=$(echo $string | awk '{if(str=(substr($0,7,1))) print str}')
str8=$(echo $string | awk '{if(str=(substr($0,8,1))) print str}')
eval $1=$(echo $[((str1+str2+str3+str4+str5+str6+str7+str8))*11] | awk '{if(str=(substr($0,1,2))) print str}') #将8位数的每位相加再乘以11,取前2位,得到整数
}
############################################################################################################
echo -en "\n数值分析中";sleep 1;echo -n " > ";sleep 1;echo -n " > ";sleep 1;echo -en " > ";sleep 1;echo "";echo ""
echo -e "$1 您的md5值\t\t$2 您的md5值";sleep 1
getmd5 a1_strength #调用方法来进行转换
getmd5 a1_brain
getmd5 a1_speed
getmd5 a1_luck
a1_life=$[$a1_strength*3 + (($a1_brain + $a1_speed))*2];sleep 1 #计算生命值
getmd5 a2_strength
getmd5 a2_brain
getmd5 a2_speed
getmd5 a2_luck
a2_life=$[$a2_strength*3 + (($a2_brain + $a2_speed))*2];sleep 1
##############################################################################################################
echo -e "\n\n对您解码后的数值\t\t对您解码后的数值" ; sleep 1 #打印已得的数值
echo -e "生命:$a1_life\t\t\t生命:$a2_life" ; sleep 1
echo -e "力量:$a1_strength\t\t\t力量:$a2_strength" ; sleep 1
echo -e "智力:$a1_brain\t\t\t智力:$a2_brain" ; sleep 1
echo -e "敏捷:$a1_speed\t\t\t敏捷:$a2_speed" ; sleep 1
echo -e "人品:$a1_luck\t\t\t人品:$a2_luck\n" ; sleep 1
echo -e "------战斗正式开始------\n";sleep 2
#############################################################################################################
signfirst=0 #求攻击优先标记,默认0
randomfirst=$(echo $RANDOM | awk '{if(str=(substr($0,2,1))) print str}')
#若随机数大于5则标志改为1
if [ $randomfirst -ge 5 ];then
signfirst=1
fi
#############################################################################################################
a1_max="a1_strength" #分别求得两位选手的主属性,(属性值最大的)
for mode in a1_strength a1_brain a1_speed
do
eval max=\$$a1_max
eval now=\$$mode
if [ $max -lt $now ];then
a1_max=$mode
fi
done
###################
a2_max="a2_strength"
for mode in a2_strength a2_brain a2_speed
do
eval max=\$$a2_max
eval now=\$$mode
if [ $max -lt $now ];then
a2_max=$mode
fi
done
##############################################################################################################
function attack1() ##########################################普通攻击函数
{
eval ${1}_fight=\$${1}_max #理论攻击力为最大属性值
random_fight=$(echo $RANDOM | awk '{if(str=(substr($0,2,2))) print str}') #生成随机攻击力
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100] #实际攻击为(理论攻击力+随机攻击力)X(1-对方敏捷/100)
eval ${2}_life=\$[\$${2}_life - \$${1}_attack] #计算被攻击者的剩余生命
eval echo -en " \$${1}\$${1}_life 发起攻击";echo -en "\t\t" ; eval echo -e " \$$2\$${2}_life 损失生命值 \$$1_attack"
echo ""
sleep 2
}
function attack2() ##########################################连击攻击函数
{
eval ${1}_fight=\$${1}_max
random_fight=$(echo $RANDOM | awk '{if(str=(substr($0,2,2))) print str}')
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100]
eval ${2}_life=\$[\$${2}_life - \$${1}_attack]
eval echo -e " \$${1}\$${1}_life 把\$${2}\$${2}_life 按在地上一顿暴打!!" ; sleep 1
eval echo -e " \$$2 损失生命值 \$$1_attack" ; sleep 1
eval ${2}_life=\$[\$${2}_life \- \$${1}_attack]
eval echo -e " \$$2 损失生命值 \$$1_attack"
echo ""
sleep 2
}
function attack3() ##########################################投毒攻击函数
{
eval ${2}_strength=\$[\$${2}_strength / 2] #先将被攻击者的各属性减半
eval ${2}_brain=\$[\$${2}_brain / 2]
eval ${2}_speed=\$[\$${2}_speed / 2]
eval ${2}_luck=\$[\$${2}_luck / 2]
eval echo "\$${1}向\$${2}投毒 \$${2}各属性减半"
echo "";sleep 1
eval ${1}_fight=\$${1}_max
random_fight=$(echo $RANDOM | awk '{if(str=(substr($0,2,2))) print str}')
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100]
eval ${2}_life=\$[\$${2}_life - \$${1}_attack]
eval echo -en " \$${1}\$${1}_life 发起攻击";echo -en "\t\t" ; eval echo -e " \$$2\$${2}_life 损失生命值 \$$1_attack"
echo ""
sleep 2
}
function fight() #############################################攻击控制主函数
{
random_luck=$(echo $RANDOM|awk '{if(str=(substr($0,2,2))) print str}')
if [ $[$(eval echo \$${2}_speed)+$random_luck] -ge 100 ];then #若被攻击者的敏捷+随机数 大于 100,直接判定攻击被闪避
eval echo -en " \$${1} 发起攻击";echo -en "\t\t" ; eval echo -e " \$$2 华丽的一个闪避" ; echo "" ;sleep 2
signfirst=$[(($signfirst+1)) % 2]
continue
fi
eval type_fight=\$[$(echo $RANDOM|awk '{if(str=(substr($0,2,2))) print str}') + \$${1}_luck]
if [ $type_fight -ge 100 ] && [ $[$type_fight % 2] -eq 0 ];then
attack2 $1 $2
elif [ $type_fight -ge 100 ] && [ $[$type_fight % 2] -eq 1 ];then
attack3 $1 $2
else
attack1 $1 $2
fi
#调整攻击优先标记
signfirst=$[(($signfirst+1)) % 2] #转换攻击者
#监视是否死人
if [ $(eval echo \$${2}_life) -le 0 ];then
eval echo "\$$2被打败了!"
break
fi
}
###############################################################################################################
while : #循环调用攻击控制函数
do
if [ $signfirst -eq 0 ];then
fight a1 a2
else
fight a2 a1
fi
done
#说明:运行方法 例如 ./fight 张三 李四
sleep 1
a1=$1 #a为名字
a2=$2
n1=$(echo $1 | md5sum) #n为名字的md5值
n2=$(echo $2 | md5sum)
########################################################################################################
a1_strength=$(echo $n1 | awk '{if(str=(substr($0,1,8))) print str}')#力量为md5前8位
a1_brain=$(echo $n1 | awk '{if(str=(substr($0,9,8))) print str}') #智力为9~16
a1_speed=$(echo $n1 | awk '{if(str=(substr($0,17,8))) print str}') #敏捷为17~24
a1_luck=$(echo $n1 | awk '{if(str=(substr($0,25,8))) print str}') #人品为25~32
a2_strength=$(echo $n2 | awk '{if(str=(substr($0,1,8))) print str}')
a2_brain=$(echo $n2 | awk '{if(str=(substr($0,9,8))) print str}')
a2_speed=$(echo $n2 | awk '{if(str=(substr($0,17,8))) print str}')
a2_luck=$(echo $n2 | awk '{if(str=(substr($0,25,8))) print str}')
#########################################################################################################
function getmd5() #将各属性的md5值转成两位整数
{
string=$(eval echo \$$1)
echo -en $string
str1=$(echo $string | awk '{if(str=(substr($0,1,1))) print str}')
str2=$(echo $string | awk '{if(str=(substr($0,2,1))) print str}')
str3=$(echo $string | awk '{if(str=(substr($0,3,1))) print str}')
str4=$(echo $string | awk '{if(str=(substr($0,4,1))) print str}')
str5=$(echo $string | awk '{if(str=(substr($0,5,1))) print str}')
str6=$(echo $string | awk '{if(str=(substr($0,6,1))) print str}')
str7=$(echo $string | awk '{if(str=(substr($0,7,1))) print str}')
str8=$(echo $string | awk '{if(str=(substr($0,8,1))) print str}')
eval $1=$(echo $[((str1+str2+str3+str4+str5+str6+str7+str8))*11] | awk '{if(str=(substr($0,1,2))) print str}') #将8位数的每位相加再乘以11,取前2位,得到整数
}
############################################################################################################
echo -en "\n数值分析中";sleep 1;echo -n " > ";sleep 1;echo -n " > ";sleep 1;echo -en " > ";sleep 1;echo "";echo ""
echo -e "$1 您的md5值\t\t$2 您的md5值";sleep 1
getmd5 a1_strength #调用方法来进行转换
getmd5 a1_brain
getmd5 a1_speed
getmd5 a1_luck
a1_life=$[$a1_strength*3 + (($a1_brain + $a1_speed))*2];sleep 1 #计算生命值
getmd5 a2_strength
getmd5 a2_brain
getmd5 a2_speed
getmd5 a2_luck
a2_life=$[$a2_strength*3 + (($a2_brain + $a2_speed))*2];sleep 1
##############################################################################################################
echo -e "\n\n对您解码后的数值\t\t对您解码后的数值" ; sleep 1 #打印已得的数值
echo -e "生命:$a1_life\t\t\t生命:$a2_life" ; sleep 1
echo -e "力量:$a1_strength\t\t\t力量:$a2_strength" ; sleep 1
echo -e "智力:$a1_brain\t\t\t智力:$a2_brain" ; sleep 1
echo -e "敏捷:$a1_speed\t\t\t敏捷:$a2_speed" ; sleep 1
echo -e "人品:$a1_luck\t\t\t人品:$a2_luck\n" ; sleep 1
echo -e "------战斗正式开始------\n";sleep 2
#############################################################################################################
signfirst=0 #求攻击优先标记,默认0
randomfirst=$(echo $RANDOM | awk '{if(str=(substr($0,2,1))) print str}')
#若随机数大于5则标志改为1
if [ $randomfirst -ge 5 ];then
signfirst=1
fi
#############################################################################################################
a1_max="a1_strength" #分别求得两位选手的主属性,(属性值最大的)
for mode in a1_strength a1_brain a1_speed
do
eval max=\$$a1_max
eval now=\$$mode
if [ $max -lt $now ];then
a1_max=$mode
fi
done
###################
a2_max="a2_strength"
for mode in a2_strength a2_brain a2_speed
do
eval max=\$$a2_max
eval now=\$$mode
if [ $max -lt $now ];then
a2_max=$mode
fi
done
##############################################################################################################
function attack1() ##########################################普通攻击函数
{
eval ${1}_fight=\$${1}_max #理论攻击力为最大属性值
random_fight=$(echo $RANDOM | awk '{if(str=(substr($0,2,2))) print str}') #生成随机攻击力
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100] #实际攻击为(理论攻击力+随机攻击力)X(1-对方敏捷/100)
eval ${2}_life=\$[\$${2}_life - \$${1}_attack] #计算被攻击者的剩余生命
eval echo -en " \$${1}\$${1}_life 发起攻击";echo -en "\t\t" ; eval echo -e " \$$2\$${2}_life 损失生命值 \$$1_attack"
echo ""
sleep 2
}
function attack2() ##########################################连击攻击函数
{
eval ${1}_fight=\$${1}_max
random_fight=$(echo $RANDOM | awk '{if(str=(substr($0,2,2))) print str}')
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100]
eval ${2}_life=\$[\$${2}_life - \$${1}_attack]
eval echo -e " \$${1}\$${1}_life 把\$${2}\$${2}_life 按在地上一顿暴打!!" ; sleep 1
eval echo -e " \$$2 损失生命值 \$$1_attack" ; sleep 1
eval ${2}_life=\$[\$${2}_life \- \$${1}_attack]
eval echo -e " \$$2 损失生命值 \$$1_attack"
echo ""
sleep 2
}
function attack3() ##########################################投毒攻击函数
{
eval ${2}_strength=\$[\$${2}_strength / 2] #先将被攻击者的各属性减半
eval ${2}_brain=\$[\$${2}_brain / 2]
eval ${2}_speed=\$[\$${2}_speed / 2]
eval ${2}_luck=\$[\$${2}_luck / 2]
eval echo "\$${1}向\$${2}投毒 \$${2}各属性减半"
echo "";sleep 1
eval ${1}_fight=\$${1}_max
random_fight=$(echo $RANDOM | awk '{if(str=(substr($0,2,2))) print str}')
eval ${1}_attack=\$[\$[\$${1}_fight + \$random_fight] \* \$[100 - \$\(echo \$${2}_speed\)] / 100]
eval ${2}_life=\$[\$${2}_life - \$${1}_attack]
eval echo -en " \$${1}\$${1}_life 发起攻击";echo -en "\t\t" ; eval echo -e " \$$2\$${2}_life 损失生命值 \$$1_attack"
echo ""
sleep 2
}
function fight() #############################################攻击控制主函数
{
random_luck=$(echo $RANDOM|awk '{if(str=(substr($0,2,2))) print str}')
if [ $[$(eval echo \$${2}_speed)+$random_luck] -ge 100 ];then #若被攻击者的敏捷+随机数 大于 100,直接判定攻击被闪避
eval echo -en " \$${1} 发起攻击";echo -en "\t\t" ; eval echo -e " \$$2 华丽的一个闪避" ; echo "" ;sleep 2
signfirst=$[(($signfirst+1)) % 2]
continue
fi
eval type_fight=\$[$(echo $RANDOM|awk '{if(str=(substr($0,2,2))) print str}') + \$${1}_luck]
if [ $type_fight -ge 100 ] && [ $[$type_fight % 2] -eq 0 ];then
attack2 $1 $2
elif [ $type_fight -ge 100 ] && [ $[$type_fight % 2] -eq 1 ];then
attack3 $1 $2
else
attack1 $1 $2
fi
#调整攻击优先标记
signfirst=$[(($signfirst+1)) % 2] #转换攻击者
#监视是否死人
if [ $(eval echo \$${2}_life) -le 0 ];then
eval echo "\$$2被打败了!"
break
fi
}
###############################################################################################################
while : #循环调用攻击控制函数
do
if [ $signfirst -eq 0 ];then
fight a1 a2
else
fight a2 a1
fi
done
- bones7456
- 帖子: 8495
- 注册时间: 2006-04-12 20:05
- 来自: 杭州
- 联系:
-
- 帖子: 1331
- 注册时间: 2006-07-09 12:22
- 来自: 南京