我在 Ubuntu 中编写了一个 bash 程序。该程序是“用计算机玩石头、剪刀、布”。我希望继续这个程序,直到出现“你赢了,再见”的消息。
有人可以建议我使用下面的代码来做到这一点吗?
#!/bin/bash
echo "Welcome to play Rock, Paper, Scissors"
echo "Choose Rock(0), Paper(1), Scissors(2)"
#read players choice
read PLAYER
choices=( 0 1 2 )
#randomize the computer choice
COMPUTER=${choices[$((RANDOM%3))]}
if [[ $PLAYER = 0 && $COMPUTER = 1 ]]
then echo "i have paper , i win";
elif [[ $PLAYER = 1 && $COMPUTER = 1 ]]
then echo "i have paper , we draw";
elif [[ $PLAYER = 2 && $COMPUTER = 1 ]]
then echo "i have paper , you win, bye bye";
elif [[ $PLAYER = 0 && $COMPUTER = 0 ]]
then echo "i have rock , we draw";
elif [[ $PLAYER = 1 && $COMPUTER = 0 ]]
then echo "i have rock , you win, bye bye";
elif [[ $PLAYER = 2 && $COMPUTER = 0 ]]
then echo "i have rock , i win";
elif [[ $PLAYER = 0 && $COMPUTER = 2 ]]
then echo "i have scissors, you win, bye bye";
elif [[$count = 0 && $PLAYER = 1 && $COMPUTER = 2 ]]
then echo "i have scissors, i win";
elif [[$count = 0 && $PLAYER = 2 && $COMPUTER = 2 ]]
then echo "i have scissors, we draw "
fi
答案1
将整个 while 过程放入一个while true
循环中,并break
在每个想要结束的位置退出。
还要注意,不需要数组choices
。
#!/bin/bash
echo "Welcome to play Rock, Paper, Scissors"
while true
do
echo "Choose Rock(0), Paper(1), Scissors(2)"
#read players choice
read PLAYER
#randomize the computer choice
COMPUTER=$((RANDOM%3))
if [[ $PLAYER = 0 && $COMPUTER = 1 ]]
then echo "i have paper , i win";
elif [[ $PLAYER = 1 && $COMPUTER = 1 ]]
then echo "i have paper , we draw";
elif [[ $PLAYER = 2 && $COMPUTER = 1 ]]
then echo "i have paper , you win, bye bye"; break
elif [[ $PLAYER = 0 && $COMPUTER = 0 ]]
then echo "i have rock , we draw";
elif [[ $PLAYER = 1 && $COMPUTER = 0 ]]
then echo "i have rock , you win, bye bye"; break
elif [[ $PLAYER = 2 && $COMPUTER = 0 ]]
then echo "i have rock , i win";
elif [[ $PLAYER = 0 && $COMPUTER = 2 ]]
then echo "i have scissors, you win, bye bye"; break
elif [[$count = 0 && $PLAYER = 1 && $COMPUTER = 2 ]]
then echo "i have scissors, i win";
elif [[$count = 0 && $PLAYER = 2 && $COMPUTER = 2 ]]
then echo "i have scissors, we draw "
fi
done
答案2
您可以简单地添加一个无限循环,使用while
循环,并根据玩家输入退出程序。例如:
#!/usr/env bash
echo "Welcome to play Rock, Paper, Scissors"
choices=( 0 1 2 )
while [ "$PLAYER" != "Q" ]; do
echo "Choose Rock(0), Paper(1), Scissors(2), or quit (Q)"
read PLAYER
#randomize the computer choice
COMPUTER=${choices[$((RANDOM%3))]}
if [[ "$PLAYER" = "0" && "$COMPUTER" = "1" ]]
then echo "i have paper , i win"
elif [[ "$PLAYER" = "1" && "$COMPUTER" = "1" ]]
then echo "i have paper , we draw"
elif [[ "$PLAYER" = "2" && "$COMPUTER" = "1" ]]
then echo "i have paper , you win, bye bye"
elif [[ "$PLAYER" = "0" && "$COMPUTER" = "0" ]]
then echo "i have rock , we draw"
elif [[ "$PLAYER" = "1" && "$COMPUTER" = "0" ]]
then echo "i have rock , you win, bye bye"
elif [[ "$PLAYER" = "2" && "$COMPUTER" = "0" ]]
then echo "i have rock , i win"
elif [[ "$PLAYER" = "0" && "$COMPUTER" = "2" ]]
then echo "i have scissors, you win, bye bye"
elif [[ "$PLAYER" = "1" && "$COMPUTER" = "2" && "$count" = "0" ]]
then echo "i have scissors, i win"
elif [[ "$PLAYER" = "2" && "$COMPUTER" = "2" && "$count" = "0" ]]
then echo "i have scissors, we draw "
fi
done