Rock-Paper-Scissors Game in Python

Introduction

This project is a Python-based implementation of the classic Rock-Paper-Scissors game. It is designed for two players and allows for multiple rounds of play. The game determines the winner of each round and ultimately declares an overall winner or a tie.

Prerequisites

  • Basic understanding of Python programming.
  • Familiarity with conditional statements and loops in Python.
  • Python environment (like IDLE, Jupyter Notebook, or any IDE supporting Python).

Learning Objectives

  • Implementing game logic using Python.
  • Understanding the use of functions and global variables.
  • Handling user inputs and providing appropriate responses.

Installation Guide

No additional installation is required if Python is already installed. This game can be run in any Python environment.

Project Steps

  1. Initialization: Set up global variables and the list of valid choices.
  2. User Input: Prompt players to input their choices in each round.
  3. Game Logic: Implement functions to determine the outcome of each round.

Final Outcome: After all rounds, determine and display the overall winner.

Code

				
					def tie():
    global p1c, p2c
    if p1 == p2:
        print("Match Tie")
        p1c += 1
        p2c += 1

def p1Wins():
    global p1c
    if p1 == 'rock' and p2 == 'scissor': 
        print("Player 1 wins")
        p1c += 1 
    elif p1 == 'paper' and p2 == 'rock' : 
        print("Player 1 wins")
        p1c += 1
    elif p1 == 'scissor' and p2 == 'paper' : 
        print("Player 1 wins")
        p1c += 1

def p2Wins():
    global p2c
    if p2 == 'rock' and p1 == 'scissor': 
        print("Player 2 wins")
        p2c += 1
    elif p2 == 'paper' and p1 == 'rock' : 
        print("Player 2 wins")
        p2c += 1
    elif p2 == 'scissor' and p1 == 'paper' : 
        print("Player 2 wins")
        p2c += 1

def whoWins():
    global p1c, p2c
    if p1c == p2c : print("Final Match Tie")
    elif p1c > p2c : print("Finally Player 1 wins")
    elif p1c < p2c : print("Finally Player 2 wins")

p1c = 0
p2c = 0
signs = ['rock', 'paper', 'scissor']
for i in range(int(input("How many times you want to play: "))):
    p1 = input("Player 1 - Choose Rock/Paper/Scissor : ").lower()
    p2 = input("Player 2 -Choose Rock/Paper/Scissor : ").lower()
    if p1 in signs and p2 in signs:
        tie()
        p1Wins()
        p2Wins()
    else: print('Invalid input')
whoWins()

				
			

Code

				
					def tie():
    global p1c, p2c
    if p1 == p2:
        print("Match Tie")
        p1c += 1
        p2c += 1

def p1Wins():
    global p1c
    if p1 == 'rock' and p2 == 'scissor': 
        print("Player 1 wins")
        p1c += 1 
    elif p1 == 'paper' and p2 == 'rock' : 
        print("Player 1 wins")
        p1c += 1
    elif p1 == 'scissor' and p2 == 'paper' : 
        print("Player 1 wins")
        p1c += 1

def p2Wins():
    global p2c
    if p2 == 'rock' and p1 == 'scissor': 
        print("Player 2 wins")
        p2c += 1
    elif p2 == 'paper' and p1 == 'rock' : 
        print("Player 2 wins")
        p2c += 1
    elif p2 == 'scissor' and p1 == 'paper' : 
        print("Player 2 wins")
        p2c += 1

def whoWins():
    global p1c, p2c
    if p1c == p2c : print("Final Match Tie")
    elif p1c > p2c : print("Finally Player 1 wins")
    elif p1c < p2c : print("Finally Player 2 wins")

p1c = 0
p2c = 0
signs = ['rock', 'paper', 'scissor']
for i in range(int(input("How many times you want to play: "))):
    p1 = input("Player 1 - Choose Rock/Paper/Scissor : ").lower()
    p2 = input("Player 2 -Choose Rock/Paper/Scissor : ").lower()
    if p1 in signs and p2 in signs:
        tie()
        p1Wins()
        p2Wins()
    else: print('Invalid input')
whoWins()

				
			

Pseudo Code explaining this Python Project

				
					Initialize p1c, p2c to 0
Define signs as ['rock', 'paper', 'scissor']

For each round (user-defined number):
    Get Player 1's choice
    Get Player 2's choice
    If both choices are valid:
        Call tie()
        Call p1Wins()
        Call p2Wins()
    Else:
        Print 'Invalid input'

Call whoWins() to determine the final winner

				
			

Pseudo Code explaining this Python Project

				
					Initialize p1c, p2c to 0
Define signs as ['rock', 'paper', 'scissor']

For each round (user-defined number):
    Get Player 1's choice
    Get Player 2's choice
    If both choices are valid:
        Call tie()
        Call p1Wins()
        Call p2Wins()
    Else:
        Print 'Invalid input'

Call whoWins() to determine the final winner

				
			

Code Explanation

Global Variables: p1c, p2c for keeping score, signs for valid choices.

Function Description

  • tie()
    • Purpose: Checks if the current round is a tie (both players chose the same sign).
    • Global Variables Used: p1c, p2c
    • Logic: If p1 (Player 1’s choice) is equal to p2 (Player 2’s choice), it’s a tie. Increments both p1c and p2c by 1.
  • p1Wins()
    • Purpose: Determines if Player 1 wins the current round.
    • Global Variables Used: p1c
    • Logic: Checks if Player 1’s choice beats Player 2’s choice based on the game rules. If Player 1 wins, increments p1c by 1.
  • p2Wins()
    • Purpose: Determines if Player 2 wins the current round.
    • Global Variables Used: p2c
    • Logic: Checks if Player 2’s choice beats Player 1’s choice based on the game rules. If Player 2 wins, increments p2c by 1.
  • whoWins()
    • Purpose: Determines the overall winner after all rounds or declares a tie.
    • Global Variables Used: p1c, p2c

Logic: Compares p1c and p2c to declare the final winner or a tie.

Main Game Loop

  • Variables Initialization: p1c and p2c are initialized to 0. signs is a list containing ‘rock’, ‘paper’, and ‘scissor’.
  • Gameplay: The game runs for a user-defined number of rounds. In each round:
    • Players input their choices.
    • The program checks for valid inputs.
    • Calls tie(), p1Wins(), and p2Wins() to determine the round’s outcome.
    • After all rounds, whoWins() is called to declare the final result.

Challenges and Solutions

  • Challenge: Managing the game state across multiple rounds.
  • Solution: Use of global variables to track scores throughout the game.

Testing

  • Test the game with various inputs to ensure all possible game scenarios are handled correctly.
  • Verify that the score is accurately updated after each round.

Additional Resources

FAQs

  • Can the game be extended to more than two players?
    • Currently, the game is designed for two players, but it can be modified for more players with additional logic.
  • How can I reset the game?
    • Restart the Python script to reset the game scores and start anew.

Flow Chart

Project by Nimisha Agrawal, Team edSlash.