为什么我的石头剪刀布游戏中的循环无法正常继续?

为什么我的石头剪刀布游戏中的循环无法正常继续?

我正在制作一个bo3石头剪刀布游戏。到目前为止,我已经让它在除领带之外的所有条件下都能工作。当出现平局时,它意味着只需从 I 中减去 1 即可“重制”该游戏。然而,它似乎不起作用,我不明白为什么。

这是我的代码:

#!/bin/bash

#rock beats scissors, scissors beats paper, paper beats rock, 
userwin=0
compwin=0

printf "Welcome to rock paper scissors. "

for i in {1..3}
do
    printf "Type 'r', 'p', or 's': "

    #get users selection
    read user

    #detect input and assign it to a num that will be used to check against computer.
    if [[ $user == [rR] || $user == "Rock" || $user == "rock" ]]
      then
        user="r"
    elif [[ $user == [sS] || $user == "scissors" || $user == "Scissors" ]]
      then
        user="s"
    elif [[ $user == [pP] || $user == "paper" || $user == "Paper" ]]
      then
        user="p"
    else
        printf "Not a valid submission, you entered: %s\n" "$user"
    fi

   #get random number between 1 and 9
   comp=$(shuf -i 1-9 -n 1)

   #detect what number it was and assign either r p s to it. 
   if ((1<=comp && comp<=3))
     then 
       comp="r"
       printf "Computer chooses rock\n"
   elif ((4<=comp && comp<=6))
     then
       comp="s"
       printf "Computer chooses scissors\n"
   elif ((6<=comp && comp<=9))
     then
       comp="p"
       printf "Computer chooses paper\n"
   else
       printf "not in range?"
   fi

   #find out who won
   if [[ $user == "$comp" ]]
     then
        i=$((i-1))
        printf "It's a tie, remake!\n"
   elif [[ $user == "r" && $comp == "p" ]]
     then
       printf "You lose!\n"
       compwin=$((compwin+1))
   elif [[ $user == "p" && $comp == "r" ]]
     then  
       printf "You win!\n"
       userwin=$((userwin+1))
   elif [[ $user == "s" && $comp == "r" ]]
     then  
       printf "You lose!\n"
       compwin=$((compwin+1))
   elif [[ $user == "r" && $comp == "s" ]]
     then  
       printf "You win!\n"
       userwin=$((userwin+1))
   elif [[ $user == "p" && $comp == "s" ]]
     then  
       printf "You lose!\n"
       compwin=$((compwin+1))
   elif [[ $user == "s" && $comp == "p" ]]
     then  
       printf "You win!\n"
   else
       printf "something is borked"
   fi

   if [[ $userwin == 2 ]]
     then
       printf "You win the bo3!\n"
       break
   elif [[ $compwin == 2 ]]
     then
       printf "You lose the bo3!\n"    
       break
   else
      reali=$((i+1))
      printf "\nGame %s start\n" "$reali"
   fi

done

这是一个失败游戏的例子:

noah: [~/myFiles/scripts] ./rps.sh
Welcome to rock paper scissors. Type 'r', 'p', or 's': r
Computer chooses rock
It's a tie, remake!

Game 1 start
Type 'r', 'p', or 's': r
Computer chooses paper
You lose!

Game 3 start
Type 'r', 'p', or 's': r
Computer chooses scissors
You win!

Game 4 start
noah: [~/myFiles/scripts]

似乎有些东西弄乱了我的 I 整数,但即使使用 bash -x 我也无法弄清楚为什么。有经验的人可以检查一下吗?

答案1

for您不能像这样修改循环内的变量bash。哦...可以,但是不会影响迭代。它不是像我们在 C 中习惯的那样计数循环。

最小的例子:

#!/bin/bash
for i in {1..3}
do
    echo $i
    i=$((i-1))
    echo $i
done

明显打印:

./b.sh 
1
0
2
1
3
2

您最好使用带条件的标准循环(如 Kusalananda 所建议的):

#!/bin/bash
i=0
while ((i++ < 3)); do
    echo $i
    # your code here
    #i=$((i-1))
    #echo $i
done

还保留下面​​的旧提案,但上面的提案显然更好。


如果您想修改运行量,最好使用无限循环并在内部处理自己的变量。像这样的东西应该可以完成工作(例如增量 2,否则它永远不会结束):

#!/bin/bash
i=1
while true; do
    if [[ $i > "3" ]]; then
        break
    fi
    echo $i
    i=$((i-1))
    echo $i

    i=$((i+2))
done

相关内容