Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have made a bash program in Ubuntu. The program is "rock, scissors paper with computer". I want to continue this program until the message "you win, bye bye".

Could anyone suggest a method for me to do this using the code below?

#!/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
share|improve this question

2 Answers 2

up vote 0 down vote accepted

Wrap the whole while thing in a while true loop and break out at each point where you want to finish.

Note also that there's no need for the choices array.

#!/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
share|improve this answer
    
i don't know 'while true~break'. can you teach its meaning to me? –  leebyeoungju Apr 30 at 11:15
    
while true will repeat the commands between do and done forever. But any break statement within the loop will break out and execute whatever commands come after the done. In this case there are no commands after the loop so the script exits. –  Lqueryvg Apr 30 at 12:06
    
i see thank you:) –  leebyeoungju Apr 30 at 12:42

You can simply add an infinite loop, using a while loop, and exit the program depending on player input. Ex:

#!/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
share|improve this answer
    
well, in my computer, it has an error on line 6. that said,"!= is unary operator expected" please fix it and reupload for me? –  leebyeoungju Apr 30 at 11:22
    
That's because PLAYER has not been set before being used, and so the while condition sees only != "Q" which is an error. A fix might be to set PLAYER to something before the loop starts. Also, I would always guard the variable with double quotes when using it in conditions, i.e. "$PLAYER" just in case it's not set. This should really be done in all of those if conditions; it's good coding practice. –  Lqueryvg Apr 30 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.