我正在开发一个简单的随机数猜测游戏,但我遇到了一些困难。核心游戏正在运行,但我想要更多。例如,如果您距离随机数在 10 以内(更高或更低),您会收到一条不同的消息。
#!/bin/bash
name=Jack
num=$(( 1+$RANDOM%101 ))
echo $num
target=$num
goes=0
until [[ $guess -eq $target ]]
do
goes=$(( $goes+1 ))
echo "Guess $goes. Enter a number between 0 and 100"
read guess
if [[ $((number - $guess)) -gt 10 ]]
then
echo "Too low"
elif [[ $((guess - $number)) -gt 10 ]]
then
echo "Too high"
elif [[ $(($number - $guess)) -le 10 ]]
then
echo "You are within ten"
elif [[ $(($guess - $low)) -le 10 ]] ||
then
echo "You are within ten"
fi
done
echo "Well done $name, it took $goes goes to guess $num"
任何帮助将不胜感激
答案1
更改您的第一个语句并将最后一部分添加为您的elif
语句:
if [[ $((number - $guess)) -gt 10 ]]
then
echo "Too low"
elif [[ $((guess - $number)) -gt 10 ]]
then
echo "Too high"
elif [[ $(($number - $guess)) -le 10 ]]
then
echo "You are within ten but too low"
elif [[ $(($guess - $number)) -le 10 ]]
then
echo "You are within ten but too high"
fi
答案2
#!/bin/bash
number=$(( 1 + (RANDOM%100) )) # random number [1,100]
while read -p 'Guess: '; do
[[ $REPLY == *[![:digit:]]* ]] && continue
if [[ REPLY -eq number ]]; then
echo Correct
break
elif [[ REPLY -gt number ]]; then
too=large
delta=$(( REPLY - number ))
else
too=small
delta=$(( number - REPLY ))
fi
if [[ delta -le 10 ]]; then
close=' (but close)'
else
close=
fi
printf 'No, too %s%s\n' "$too" "$close"
done
实现简单的输入验证(忽略包含非数字的输入),计算给定的猜测是否太大或太小,以及猜测与正确答案之间的差异为delta
。使用干原则,根据猜测是否太大或太小以及错误的大小,将变量too
和设置close
为适当的字符串,最后在让用户重试之前打印一条消息。
游戏通过正确的猜测、或者用户按下Ctrl+D以表示输入结束、或者Ctrl+C简单地中断脚本来终止。
玩上述游戏的脚本。该脚本假定上述脚本guessinggame
在当前目录中可用。
#!/bin/bash
coproc bash guessinggame
make_guess () {
printf 'Guessing %d\n' "$1"
printf '%s\n' "$1" >&${COPROC[1]}
}
guess=50 # initial guess
change=25 # next guess this much higher/lower
make_guess "$guess"
tries=1
while read; do
printf 'Game says "%s"\n' "$REPLY"
if [[ $REPLY == 'Correct' ]]; then
break
fi
if [[ $REPLY == *close* ]] && [[ change -gt 10 ]]; then
# Close call, do not change too much in next guess!
change=10
fi
if [[ $REPLY == *small* ]]; then
guess=$(( guess + change ))
else
guess=$(( guess - change ))
fi
change=$(( change / 2 ))
[[ change -eq 0 ]] && change=1
make_guess "$guess"
tries=$(( tries + 1 ))
done <&${COPROC[0]}
printf 'Guessed correctly after %d tries\n' "$tries"
运行示例:
$ bash guesser
Guessing 50
Game says "No, too large"
Guessing 25
Game says "No, too large (but close)"
Guessing 15
Game says "No, too small (but close)"
Guessing 20
Game says "No, too small (but close)"
Guessing 22
Game says "Correct"
Guessed correctly after 5 tries
答案3
数学就是解决方案:) ..请检查以下代码:
#!/bin/bash
name=Jack
number=$(( 1+$RANDOM%101 ))
echo $number
target=$number
goes=0
until [[ $guess -eq $target ]]
do
goes=$(( $goes+1 ))
echo "Guess $goes. Enter a number between 0 and 100"
read guess
if [[ $((number - guess)) -eq 0 ]]; then break; fi
if [[ $((number - guess)) -gt 10 ]]
then
echo "Too low"
elif [[ $((guess - number)) -gt 10 ]]
then
echo "Too high"
elif [[ $((number - guess)) -le 10 ]]
then
if [[ $((number - guess)) -lt 0 ]]
then
echo "You are within ten on the higher side"
else
echo "You are within ten on the lower side"
fi
fi
done
echo "Well done $name, it took $goes goes to guess $number"