cris Cadinu
cris Cadinu

Reputation: 33

Making a Rock, Paper, Scissors game

I am making a Rock, Paper, Scissors game.

The code below works, but there are two critical things missing and I would really like to know how to implement this:

  1. In case of a tie, the players must be able to choose again. So the break statement must go but what is needed to go back to the while loop and play again?

  2. And the same goes for the end of a game. Players must be able to play again. I think it is the same code as the above.

The answer would be really helpful as it really helps me understand the while loop more.

def rps():

    play = True

    player1 = input('Player1: rock, paper of scissors? ')
    player2 = input('Player2: rock, paper of scissors? ')

    while play:
        if player1 == 'rock' and player2 == 'rock': # deze methode werkt niet om opnieuw weer te beginnen
            print ('Tie! Pick again')
            break
        elif player1 == 'rock' and player2 == 'paper':
            print('Player 2 won')
            break
        elif player1 == 'rock' and player2 == 'scissors':
            print ('Speler 1 won')
            break
        elif player1 == 'paper' and player2 == 'rock':
            print('Speler 1 won')
            break
        elif player1 == 'paper' and player2 == 'paper':
            print('Tie! Pick again')
            continue
        elif player1 == 'paper' and player2 == 'scissors':
            print('Speler 2 won')
            break
        elif player1 == 'scissors' and player2 == 'rock':
            print('Speler 2 won')
            break
        elif player1 == 'scissors' and player2 == 'paper':
            print('Speler 1 won')
            break
        elif player1 == 'scissors' and player2 == 'scissors':
            print('Tie! Pick again')

Upvotes: 1

Views: 107

Answers (2)

Arghya Saha
Arghya Saha

Reputation: 5713

This code should solve serve your purpose

def rps():

    play = True

    player1 = input('Player1: rock, paper of scissors? ')
    player2 = input('Player2: rock, paper of scissors? ')

    while play:
        if player1 == 'rock' and player2 == 'rock': # deze methode werkt niet om opnieuw weer te beginnen
            print ('Tie! Pick again')
        elif player1 == 'rock' and player2 == 'paper':
            print('Player 2 won')
            if input('Type c to continue playing, anything else to quit') == 'y':
                pass
            else:
                break
        elif player1 == 'rock' and player2 == 'scissors':
            print ('Speler 1 won')
            if input('Type c to continue playing, anything else to quit') == 'c':
                pass
            else:
                break
        elif player1 == 'paper' and player2 == 'rock':
            print('Speler 1 won')
            if input('Type c to continue playing, anything else to quit') == 'c':
                pass
            else:
                break
        elif player1 == 'paper' and player2 == 'paper':
            print('Tie! Pick again')

        elif player1 == 'paper' and player2 == 'scissors':
            print('Speler 2 won')
            if input('Type c to continue playing, anything else to quit') == 'c':
                pass
            else:
                break
        elif player1 == 'scissors' and player2 == 'rock':
            print('Speler 2 won')
            if input('Type c to continue playing, anything else to quit') == 'c':
                pass
            else:
                break
        elif player1 == 'scissors' and player2 == 'paper':
            print('Speler 1 won')
            if input('Type c to continue playing, anything else to quit') == 'c':
                pass
            else:
                break
        elif player1 == 'scissors' and player2 == 'scissors':
            print('Tie! Pick again')

        player1 = input('Player1: rock, paper of scissors? ')
        player2 = input('Player2: rock, paper of scissors? ')

The code can be optimised and simplified by doing away few if and else, but I kept this for your understanding and making minor tweak in your code.

Upvotes: 0

manglano
manglano

Reputation: 844

Wrap the gameplay loop in a second while loop. Do not break on ties. Input for the gameplay loop happens in the gameplay loop. Input for quit or play happens and is assessed before the gameplay loop.

while True:
    print("Welcome to Rock, Paper, Scissors.")
    gameplay = input("(P)lay or (Q)uit?")
        if (gameplay == 'Q'):
             break
        else:
             while True:
                player1 = input('Player1: rock, paper of scissors? ')
                player2 = input('Player2: rock, paper of scissors? ')
                {Gameplay Logic}

Upvotes: 1

Related Questions