3

私のコードはここにあります。ただし、2番目について心配する必要はありません。代わりに、プレーヤー2とまったく同じです。

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

pt=1

fi
fi

if [ $p1s > 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
echo "ok going back to the games directory then"
sleep 1
bash games.sh

fi

done

while [ $pt == 2 ]; do
echo "roll or stay"
read choice
if [ $choice == r ]; then

die=$(($RANDOM%6+1))

elif [ $die -eq 1 ]; then
p1s=$(echo "$p2s-$count" |bc)
echo "You rolled a 1. Your score is $p2s"
echo "$p1 turn now."
sleep 1
count=0
pt=2

elif [ $die > 1 ]; then
p1s=$(echo "$p2s+$die" |bc)
count=$(echo "$count+$die" |bc)
echo "You rolled a $die. Your score is $p2s"
pt=1

else

if [ $choice == s ]; then
echo "Okay $p1 your score is $p2s"
echo "$p1 turn now"
sleep 1
count=0
pt=2

else

pt=2

fi
fi

if [ $p2s > 99 ]; then
echo "$p2 won. $p1 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
echo "ok going back to the games directory then"
sleep 1
bash games.sh

fi

done

私が実行した後に何が起こるかはこれです

Player 1 name?
name1
Player 2 name?
name2
Okay name1 and name2. name1 will go first
roll or stay
r
name1 won. name2 lost
would you like to play again?

それは転がることになっています、そしてあなたが乱数を得るならば、そのラウンドのスコアは削除されて次のプレーヤーに行きます。2-6をロールすると、そのラウンドのスコアに追加されます。滞在する場合は、スコアを保存し、1人のプレーヤーが100ポイントを獲得するまで、各ラウンドのスコアが合計されます。

4

1 に答える 1

3

数値関係をテストするために使用しているテスト比較演算子が間違っています。例えば:

elif [ $die > 1 ]; then---->である必要がありますelif [ $die -gt 1 ]; then

if [ $p2s > 99 ]; then---->である必要がありますif [ $p2s -gt 99 ]; then

等々。

数値のテスト比較演算子のリストは次のとおりです。

  • ==---> -eq(等しい)
  • !=---> -ne(等しくない)
  • >---> -gt(より大きい)
  • <---> -lt(未満)
  • >=---> -ge(以上)
  • <=---> -le(以下)
于 2012-11-08T23:55:11.740 に答える