Rock Paper Scissors Game in Python Code

Rock Paper Scissors Game in Python Code

 
Rock Paper Scissors Game in Python Code

Introduction:

 Python is one of the easiest languages ​​in the world to learn coding. Today, we will make a simple rock-paper-scissors game with python code.

My experience making this game:

I learned yesterday to make a great game of rock-paper-scissors with python code. So, now I am going to share my experience with you. We will make a fun and easy game of rock-paper-scissors with python code.

Requirements:

Initially, we need a Python IDLE or Pycharm IDE installed on your desktop or pc to generate code. If you use another IDE, you can use it again and if you want to install the public version of Pycharm IDE and use it for free, you can do it from  here.

We will use five variables, one list and three functions to make this rock-paper-scissors game with python code.

Rules:

In this project, we will create a ROCK-PAPER-SCISSORS game in which the player plays 'Rock', 'Paper' or 'Sisk' and the computer will try to hit the player according to the player's previous movement. The game is played between two people in total. The rules of the game are:

  • The rock strikes the scissors.
  • Scissors hit the paper.
  • The paper hits the rock.
In every move, whether a human player gets 1 point or a computer gets 1 point The game runs until a human player or computer reaches 10 points.

Player Installation Format:

The player must play his / her movement by entering the numbers 0, 1 and 2 when-
0 means ROCK.
1 means PAPER.
2 describes SCISSORS.

Algorithm:

In the algorithm, the computer must store player movement counts (i.e., counts 0, 1 and 2) with three different variables - count_rock, count_paper and count_scissors. The algorithm should determine the flow of the computer according to the following possibilities:

  • If the value of the count_rock variable is greater than the count_paper and count_scissors variables values, then the computer flow should be ROCK.
  • If the value of the count_paper variable is greater than the values ​​of the count_rock and count_scissors variables, the computer must be PAPER.
  • If the variable number of count_scissors is greater than the values ​​of count_rock and count_paper variables, then the computer flow should be SCISSORS.
  • In all other cases, the computer must play ROCK, PAPER and SCISSORS at random.

Game play:

In this game, you have to save both points - player and computer. The game must continue until the player or computer reaches 10 points. In each step, the output must have the following:

  • The player moves and the computer moves.
  • Player school and computer school.

Python Code:

# Import the random module in the next line.
import random
# Create the count_rock, 'count_paper and count_scissors variables and set their initial values equal to 0.
count_rock = count_paper = count_scissors = 0
def update_count(user_input):
  global count_paper , count_rock , count_scissors
  if user_input == 0:
    count_rock = count_rock + 1
  elif user_input == 1:
    count_paper = count_paper + 1
  elif user_input == 2:
    count_scissors = count_scissors + 1
def predict():
  if count_rock > count_paper and count_rock > count_scissors:
    pred = 0
  elif count_paper > count_rock and count_paper > count_scissors:
    pred = 1
  elif count_scissors > count_rock and count_scissors > count_paper:
    pred = 2
  else:
    import random
    pred = random.randint(0,2)
  return pred
 
global player_score , comp_score
player_score = comp_score = 0
def update_scores(user_input):
  global player_score, comp_score
  # Rock wins over scissors, scissors win over paper and paper wins over rock.
  pred = predict()
  if user_input == 0:
    if pred == 0:
      print("\nYou played ROCK,computer played ROCK.")
      print("\nComputer Score: ", comp_score, "\nYour Score: ", player_score)
    elif pred == 1:
      print("\nYou played ROCK,computer played PAPER.")
      comp_score += 1
      print("\nComputer Score: ", comp_score, "\nYour Score: ", player_score)
    else:
      print("\nYou played ROCK,computer played SCISSORS.")
      player_score += 1
      print("\nComputer Score: ", comp_score, "\nYour Score: ", player_score)
  elif user_input == 1:
    if pred == 1:
      print('\nyou played paper,computer played paper')
      print('\ncomputer score:', comp_score,'player score:', player_score)
    elif pred == 0:
      print('\nyou player paper,computer played rock')
      player_score += 1
      print('\ncomputer score:', comp_score,'player score:', player_score)
    elif pred == 2:
      print('\nyou played paper,computer played scissors')
      comp_score += 1
      print('\ncomputer score:', comp_score,'player score:', player_score)
  elif user_input == 2:
    if pred == 0:
      print('\nyou played scissors,computer played rock')
      comp_score += 1
      print('\ncomputer score:', comp_score,'player score:', player_score)
    elif pred == 1:
      print('\nyou played scissors,computer played paper')
      player_score += 1
      print('\ncomputer score:', comp_score,'player score:', player_score)
    elif pred == 2:
      print('\nyou played scissors,computer played scissors')
      print('\ncomputer score:', comp_score,'player score:', player_score)
# 1. Create a list: ['0', '1', '2'] and store it in the variable called valid_entries, i.e, valid_entries = ['0', '1', '2']
def gameplay():
  valid_entries = ['0','1','2']
 # 2. Create an infinite while loop. Inside the loop, create a variable called user_input to store the input taken by the player.
  while True:
    user_input = input('Enter 0 for ROCK, 1 for PAPER and 2 for SCISSORS:')
  # 3. Use the input() function to take input from a player.
  # Inside the input() function, write the Enter 0 for ROCK, 1 for PAPER and 2 for SCISSORS: statement to show it as a message to a player.
  # 4. Write another while loop to check whether the input provided by a player exists in the valid_entries list or not.
    while user_input not in valid_entries:
      print('Invalid Input!')
      user_input = input('Enter 0 for ROCK, 1 for PAPER and 2 for SCISSORS:')
    # 5. If the input provided by a player does not exist in the valid_entries list, then print Invalid Input! message.
    # In the next line, rewrite the user_input = input("Enter 0 for ROCK, 1 for PAPER and 2 for SCISSORS: ") statement.
  # 6. Now, outside the inner while loop, convert the user_input value to an integer value using the int() function.
    user_input = int(user_input)
  # 7. Call the update_scores() function with the user_input as an input to update the scores of the computer and the player.
    update_scores(user_input)
  # Rock wins over scissors, scissors win over paper and paper wins over rock.
  # 8. Call the update_counts() function with the user_input as an input to update the counts of the inputs provided by the player.
    update_count(user_input)
  # 9. Write an if statement to check if the score is 10 for any of the player.
    if player_score == 10:
      print('player wins the game')
      break
  # If the comp_score == 10, then print the Computer Won! message and break the loop
    elif comp_score == 10:
      print('better luck next time')
      break
gameplay()

Activities:

  • Now open your IDE and follow the steps below. We will now import a random module and create count_rock, count_paper, count_scissors variables and set their initial value to 0.
  • We will now create a function and name it update_counts (). It should take a user-input variable as input and update calculation variables based on the following concepts.
  • If the player is playing ROCK (or user_input == 0), then increase the value of the variable count_rock.
  • Alternatively if the player is playing PAPER (or user_input == 1), then increase the value of the variable count_paper.
  • Alternatively if the player is playing SCISSORS (or user_input == 2), then increase the number of variable count_scissors.
  • The update_counts () function should not return anything. In other words, it should not contain a return statement. It should only perform the actions described above. We will enter a global keyword here before count_rock, count_paper and count_scissors variables before increasing their values ​​to avoid an error while playing a game.
  • Now, we're going to build a predictive function.
  • Now, we will create two player_score variables and comp_score to display points scored by the player and the computer, respectively and set their initial value equal to 0.
  • Now, we're going to create update_scores functions.
  • Now, we will use the gameplay function and run the code.
  • After doing the gameplay function, we will now complete the existing code and play the game.

Click here to read more articles

Has this been helpful? Tell me at comment.

Post a Comment

0 Comments