Coders Packet

Stone Paper Scissors game in Python

By Faizan Ahmad

This game is developed so that users can play the famous 'Stone Paper Scissors' game even if they are alone. They do not need any other person but just a computer to play this game.

Modules being used:-

1.'import speech_recognition' :-It performs speech recognition, with support for several engines and APIs, online and offline. To install this module you just need to copy and paste the below code in terminal or command prompt.

pip install SpeechRecognition

2.'import pyttsx3' :- It is a text-to-speech conversion library in Python and it also work offline. Installation syntax is given below:-

pip install pyttsx3

3.‘import random’ :- The pseudo-random variables are generated by the random module, which is a built-in module. It can be used to do things like generate a random number, pick random elements from a list, shuffle elements randomly, and so on. It is already installed in Python, so no need to pip install anything.

import random as rdm
import speech_recognition as sr     #Speech recognition library
import pyttsx3                      #Text to speech library

 

Objects being made:-

  1. ‘listener’ :- Listener object of Recognizer() class of speech recognition module is made to recognize the voice input of the user.
  2. ‘engine’ :- This object of init() of pyttsx3 module is made to use further in the program to convert text into speech.
listener = sr.Recognizer()
engine = pyttsx3.init()

 

Functions in the code:-

1.‘def talk(text)’ :- This function will take the text as a parameter and let the system speak the text passed. After this runAndWait() function is invoked to block while processing all currently queued commands.

def talk(text):
    print(text)
    engine.say(text)
    engine.runAndWait()

 

2.‘def main()’ :-

  • Many variables have been created for various purposes like for calculating scores and rounds and so on.
  • A list has been made with three elements for computer to pick at random.
  • While loop is being used to provide the user with the option to play multiple rounds.
  • Three conditional expressions with four nested conditional expressions in each of them have been used to achieve the comparison between user’s and computer’s choice to declare a winner in each round.
  • Another pair of conditional expressions is used to handle the situation arises when user presses wrong option(apart from 's' , 'p' , 'c').
  • Scores and results will be evaluated as per the comparison result in these conditional expressions.
  • After each round, scores will be showed till that round and then system will ask the user whether he/she wants to play again or not.
  • If he/she wants to play again then it will ask the user for his/her choice(stone, paper, scissors). It will continue until user does not want to play anymore.
  • System will calculate the final score and will announce the winner as per the result at the end.
def main():    
    comp=0
    ch='y'
    rnd=0
    user=0
    ll=['stone','paper','scissors']
    while ch=='y' or ch=='Y':
        temp=0
        sys_choice=rdm.choice(ll)
        talk("Please enter your choice:")
        user_choice=(input())
        rnd=rnd+1
if sys_choice=='stone': if user_choice=='s' or user_choice=='S': talk('This round is tied!') flag='stone' pass elif user_choice=='p' or user_choice=='P': user=user+1 flag='paper' talk('You won this round!') elif user_choice=='c' or user_choice=='C': talk('Computer won this round!') comp=comp+1 flag='scissors' else: talk("You have pressed wrong option!!!") temp=1 rnd=rnd-1 elif sys_choice=='paper': if user_choice=='s' or user_choice=='S': talk('Computer won this round!') comp=comp+1 flag='stone' elif user_choice=='p' or user_choice=='P': talk('This round is tied!') flag='paper' pass elif user_choice=='c' or user_choice=='C': talk('You won this round!') user=user+1 flag='scissors' else: talk("You typed wrong option!!!") temp=1 rnd=rnd-1 elif sys_choice=='scissors': if user_choice=='s' or user_choice=='S': talk('You won this round!') user=user+1 flag='stone' elif user_choice=='p' or user_choice=='P': talk('Computer won this round!') comp=comp+1 flag='paper' elif user_choice=='c' or user_choice=='C': talk('This round is tied!') flag='scissors' pass else: talk("You typed wrong option!!!") temp=1 rnd=rnd-1 if temp==0: talk("You chose: "+flag) talk("Computer chose: "+sys_choice) talk("Score of user by round "+str(rnd)+' is: '+str(user)+' point') talk("Score of computer by round "+str(rnd)+' is: '+str(comp)+' point') elif temp==1: pass talk("Enter 'y' to play again:") ch=input() talk("Final score is being calculated:") talk("Final score of yours: "+str(user)+' point') talk("Final score of computer: "+str(comp)+' point') if user>comp: talk("Congratulations!!! You have won the game!")

 

Interesting feature:-

System will speak out everything whatever will be printed on the screen, for better experience and enjoyment.

 

Assembling the codes to understand the flow of the program better:-

import random as rdm
import speech_recognition as sr   
import pyttsx3                      

listener = sr.Recognizer()
engine = pyttsx3.init()

def talk(text):
    print(text)
    engine.say(text)
    engine.runAndWait()

def main():    
    comp=0
    ch='y'
    rnd=0
    user=0
    comp=0
    ll=['stone','paper','scissor']
    talk("Welcome to the 'Stone Paper Scissor' game.")
    talk("Press 's' for stone \nPress 'p' for paper \nPress 'c' for Scissor")
    while ch=='y' or ch=='Y':
        temp=0
        sys_choice=rdm.choice(ll)
        talk("Please enter your choice:")
        user_choice=(input())
        rnd=rnd+1
        if sys_choice=='stone':
            if user_choice=='s' or user_choice=='S':
                talk('This round is tied!')
                flag='stone'
                pass
            elif user_choice=='p' or user_choice=='P':
                user=user+1
                flag='paper'
                talk('You won this round!')
            elif user_choice=='c' or user_choice=='C':
                talk('Computer won this round!')
                comp=comp+1
                flag='scissor'
            else:
                talk("You have pressed wrong option!!!")
                temp=1
                rnd=rnd-1

        elif sys_choice=='paper':
            if user_choice=='s' or user_choice=='S':
                talk('Computer won this round!')
                comp=comp+1
                flag='stone'
            elif user_choice=='p' or user_choice=='P':
                talk('This round is tied!')
                flag='paper'
                pass
            elif user_choice=='c' or user_choice=='C':
                talk('You won this round!')
                user=user+1
                flag='scissor'
            else:
                talk("You typed wrong option!!!")
                temp=1
                rnd=rnd-1

        elif sys_choice=='scissor':
            if user_choice=='s' or user_choice=='S':
                talk('You won this round!')
                user=user+1
                flag='stone'
            elif user_choice=='p' or user_choice=='P':
                talk('Computer won this round!')
                comp=comp+1
                flag='paper'
            elif user_choice=='c' or user_choice=='C':
                talk('This round is tied!')
                flag='scissor'
                pass
            else:
                talk("You typed wrong option!!!")
                temp=1
                rnd=rnd-1
           
        if temp==0:
            talk("You chose: "+flag)
            talk("Computer chose: "+sys_choice)
            talk("Score of user by round "+str(rnd)+' is: '+str(user)+' point')
            talk("Score of computer by round "+str(rnd)+' is: '+str(comp)+' point')
        elif temp==1:
            pass
        talk("Enter 'y' to play again:")
        ch=input()
    talk("Final score is being calculated:") 
    talk("Final score of user: "+str(user)+' point')
    talk("Final score of computer: "+str(comp)+' point')
    if user>comp:
        talk("Congratulations!!! You have won the game!")

 

Hope you will enjoy this game!!!

    

Download project

Reviews Report

Submitted by Faizan Ahmad (fpawn123)

Download packets of source code on Coders Packet