Rock Paper Scissor Game in python

 


Introduction


Rock paper scissors game that involves three players. The aim of the game is to win by having the highest total score from all your moves. In this article we will learn how to code a rock paper scissors game in python and how to implement it as an application on your computer or mobile phone.


Game Theory


Game theory is a mathematical model of conflict and cooperation between rational decision-makers. It provides a set of tools for analyzing strategic situations, such as the prisoner's dilemma, fighting over limited resources and bargaining with strangers.


Game theory was developed by John von Neumann and Oskar Morgenstern in 1944, after they had studied games under their professor Albert Lewisohn at Princeton University.


Create the class to represent a player in the game.


You will use the following imports to create a class for each player:


import random


import math


Add a function to calculate the payo of each player.


Here is a function that will calculate the payo of each player.


```python def get_payo(player1, player2):


return round(sum(player1*player2)/2)


Define the function that deals with winning/losing the game.


The function is the most important part of the game. It should be defined in a way that it is easy to understand, use and maintain.


Now we have got our classes ready so let's start coding now!


Now we have got our classes ready so let's start coding now!


Let's begin by creating the game board and the class for it.


STEP 1: Create a new file named "GameBoard" in your code editor of choice (I'm using PyCharm). You can also name it anything you want, but I'll be referring to this file as GameBoard throughout my tutorial because that way it's clear what I'm talking about when referring to all parts of my program.


STEP 2: Open up GameBoard.py and add the following code:


This article has given you all information about programming rock paper scissors game in python.


Now you know everything about rock paper scissors game and python programming.


If you want to learn more about the topic, see this article: Rock Paper Scissors Game in Python.


Conclusion


In this article, we will discuss how to create a simple game called rock paper scissor. We will also learn about game theory and how this can help us create better games.


Algorithm

Import the random module

Initialize the global variables for choices as a tuple of r, p, s, and computer score and player score as 0.

Create a function for getting the player’s choice.

Inside the function get the input from the user.

Check if the input is valid, If yes then return the answer to the function call. If no, then return the function call so that it will again ask for input from the user.

Create a function to return the computer’s choice as input from the random. choice function.

Create a game function inside which we will check who scores the mount for the current game.

Call the player’s choice function and save the answer in a variable.

Call the computer choice function and save it in another variable.

Now, check if both the answers are the same, if so then print its a tie and no one gets a point.

Following are the scenarios where the player gets a point,

Player choice- “r” computer choice- “s”

Player choice- “s” computer choice- “p”

Player choice- “p” computer choice- “r”

In all the other conditions computer scores a point.

Now, print both the points to the user.

Now, in our main part of the program, we need to write a welcome message.

Call the game function 4 times for running the game for 4 rounds.

Ask the user if the user wants to continue/ reset and continue/quit.

If the user wants to continue repeat step 7.

If the user wants to reset and continue re-declare the variables for player score and computer score to zero and repeat step 7

If the player wants to quit you can print thank you for playing and exit the game.


Code


import random #importing the random module
 
#initializing the variables
choices = ('r', 'p', 's')
cPoints = 0
pPoints = 0
 
def player():
   
  '''This function is used to get players choice.'''
   
  global choices #declaring the variable choices to be global
  symbol = input("Choose either rock(r), paper(p), or scissors(s). ").lower()
   
   
  if symbol not in choices:
    print("You did not enter a valid option. ")
    return player() #using recursion to get a valid input
  else:
    return symbol#if valid input return the answer
 
def computer():
 
  '''This function is used to get computers choice'''\
   
  return random.choice(choices)
 
def game():
 
  '''This function is used to play a single round of game'''
   
  global cPoints, pPoints #declaring cPoints and pPoints to be global
  pChoice = player()#getting the players choice
  cChoice = computer()#getting the computers choice
  print("The computer has chosen", cChoice)
  print("You chose", pChoice)
  if pChoice == cChoice: #checking for tie
    print("Its a tie! No one gets a point. ")
  elif (pChoice == "r" and cChoice == "s") or (pChoice == "s" and cChoice == "p") or (pChoice == "p" and cChoice == "r"): #checking if player scores a point
     
    print("You won! ")
    pPoints += 1 #increment the players point by 1
  else: #checking if computer scores a point
    print("Aww. I won. ")
    cPoints += 1 #increment the computer point by 1
  print()
 
print("Welcome to Rock Paper and Scissors game!!!")
while True: #creating an infinite loop
  for i in range(5):#loop that runs 5 times
    game() #calling the game for every single round
  print("Good job!\nYour score is:", pPoints, "\nMy score is", cPoints) #displaying the result after 5 rounds
  print()
  again = int(input("Press 1 to continue\nPress2 to reset and continue\npress 3 to exit "))
  if again==1: #this is executed if the user wants to play another 5 rounds
    continue
  elif again==2: #this is executed if the user wants to reset the scores and play another 5 rounds
    pPoints=0
    cPoints=0
    continue
  else: #this is executed if the user wants to quit.
    break
     
print("Ok! Bye!")

Post a Comment

0 Comments