在我的代码中滚动你只需写入r然后按回车但似乎没有读取它并转到重新启动while循环的else。让它滚动的唯一方法是输入除r以外的东西而不是(standard_in)1:解析错误。
#!/bin/bash
#this is a game that is two player and it is a race to get to
#100 before the other player
echo "Player 1 name?"
read p1
echo "Player 2 name?"
read p2
echo "Okay $p1 and $p2. $p1 will go first"
p1s=0
p2s=0
pt=1
while [ $pt -eq 1 ]; do
echo "roll or stay"
read choice
if [ $choice == r ]; then
die=$(($RANDOM%6+1))
elif [ $die -eq 1 ]; then
p1s=$(echo "$p1s-$count" |bc)
echo "You rolled a 1. Your score is $p1s"
echo "$p2 turn now."
sleep 1
count=0
pt=2
elif [ $die -gt 1 ]; then
p1s=$(echo "$p1s+$die" |bc)
count=$(echo "$count+$die" |bc)
echo "You rolled a $die. Your score is $p1s"
pt=1
else
if [ $choice == s ]; then
echo "Okay $p1 your score is $p1s"
echo "$p2 turn now"
sleep 1
count=0
pt=2
else
if [ $p1s -gt 99 ]; then
echo "$p1 won. $p2 lost"
echo "would you like to play again?"
read again
elif [ $again yes ]; then
echo "Okay one second."
sleep 1
clear
bash num.sh
elif [ $again == no ]; then
exit
else
pt=1
fi
fi
fi
done
不要担心他们不是第二名球员的第二名。我把它删除了,因为它只适用于玩家二的相同代码。
示例输入/输出
Player 1 name?
guy1
Player 2 name?
guy2
Okay guy1 and guy2. guy1 will go first
roll or stay
r
roll or stay
rr
(standard_in) 1: parse error
You rolled a 5. Your score is 5
roll or stay
roll
(standard_in) 1: parse error
You rolled a 5. Your score is 10
roll or stay
s
(standard_in) 1: parse error
You rolled a 5. Your score is 15
roll or stay
答案 0 :(得分:4)
好的,关于你的代码的一些评论。
*
)而不是“r”会发生什么?你的if
陈述会做出奇妙而神秘的事情。if
的单个方括号,将字符串等效性与单个等号(=
)进行比较,而不是双倍。如果你想要数字相等,你有-eq
。虽然您可能希望查看bash的扩展测试,但使用双方括号。 (有关详细信息,请查看手册页。)bc
。所以......所有人都说,这是你的代码段,重写了一下。
while [ "$pt" -eq 1 ]; do
read -p "Roll or stay (r/s)? " choice
if [ "$choice" = r ]; then
die=$(($RANDOM%6+1))
elif [ "$die" -eq 1 ]; then
p1s=$((p1s - count))
echo "You rolled a 1. Your score is $p1s"
echo "$p2 turn now."
sleep 1
count=0
pt=2
elif [ $die -gt 1 ]; then
p1s=$((p1s + die))
count=$((count + die))
echo "You rolled a $die. Your score is $p1s"
pt=1
else
请注意,我没有声明您的程序逻辑是否合理。
到底是什么num.sh
?这很重要吗?
答案 1 :(得分:0)
在查看正确缩进的代码(请参阅ghoti的重写代码段)后,我看到顶级if-block是
if [ $choice == r ]; then
die=... #roll the die
elif [ $die -eq 1 ]...
elif [ $die -gt 1 ]...
else
do something...
fi
问题是,如果[ $choice == r ]
为真,您将滚动骰子并跳过剩余的elif
- else
条目。所以你将进行下一次迭代而不做任何事情(滚动模具除外)
解决此问题的一种方法是将$choice
和$die
检查为单独的if
块,即
if [ $choice == r ]; then
#roll the die
else
#break or something...
fi
if [ $die -eq 1 ]; then
#do something
elif the rest of $die-related checks