2

Rock, Paper, Scissors, Saw Game in Python

Project requires students to create a design for a text-based “Rock, Paper, Scissors, and Saw”
game and then implement it in Python.

Scope

The software to be created will be a stand-alone PC representation of the hand game called
“Rock, Paper, Scissors, and Saw”, implemented with procedural approach using Python. It will
have two human players and a computer acting as the third player.

General Description

When user(s) starts the program, he will be prompted for the names of the two human players.
These values will be used for all prompts and statistics in the program.
Then there will be a menu with the following selections: “1. Play game”, “2. Show game rules”,
“3. Show statistics”, and “4. Exit” in that order. Rules selection will provide rules of the game,
statistics will show the accumulative wins, loses, and ties per each player (both rounds and
games), and exit will close the program.
When play game is selected, each player will be able to select his weapon (rock, paper, scissors,
or saw) and computer will make its choice. Each of the players will be playing against the
computer. A game will consist of 3 rounds. After each round either a player or computer will be
the winner of the round and after 3 rounds, again either computer or player will be the winner of
the game. User(s) will be able to run multiple games and their wins/loses/ties will be displayed in
the statistics option along with the overall human winner.

CODE

import random

games = [[0,0],[0,0],[0,0],[0,0]]
rounds = [[0,0],[0,0],[0,0],[0,0]]

def check_name(name):
if not name:
return False
elif len(name) < 5 or len(name) > 20:
return False
else:
return True
def player1_name():
while True:
name = input("What is the name of the first player?")
if check_name(name):
return name
else:
print("Name must contain letters(5-20)")
def player2_name(p1):
while True:
name = input("What is the name of the second player?")
if check_name(name):
if name.lower() == p1.lower():
print("Second player name must be different from first player")
else:
return name
else:
print("Name must contain letters(5-20)")
def menu():
while True:
print("1. Play game")
print("2. Show game rules")
print("3. Show statistics")
print("4. Exit")
ch = input("Enter your choice : ")
if ch.isdigit() and int(ch) > 0 and int(ch) <= 4:
return int(ch)
else:
print("choice must be from 1 - 5")
def rules():
print('''[R4.1] When game starts, each Player gets prompted for his selection of Rock, Paper, Scissors,
and Saw. The prompt must use the player’s name.
[R4.2] Computer makes its random selection to be used against each player (same selection
will be used against both human players)
Software Requirements Specification Page iii
<RPS Game>
[R4.3] For each player, program displays that player’s selection, computer’s selection and who
won that round (computer or player)
[R4.4] Next round repeats the prompting and announcement of the round winners. This is
repeated for a total of 3 rounds.
[R4.5] Once all 3 rounds are completed winners of the game are announced for each player.
Just like rounds, each player is playing against the computer and so the winner of the game is
either the player or the computer.
[R4.6] The winner of the game is one who won most rounds. The program must account for
ties. For example, if a player won 2 rounds and lost 1 round, then he won the game against the
computer. If the player won 1 round, lost 1 round, and tied 1 round then he tied the game
against the computer.
[R4.7] After game winners are announced, user is able to go back to menu where they can
again select one of the options.
[R4.8] Users can play as many games as they want''')
def valid_weapon(w):
if w not in ['rk', 'pr', 'sc', 'sw']:
return False
return True

def check_winner(w1,w2):
if w1 == w2:
return -1
if w1=='rk':
if w2 in ['sc','sw']:
return 1
return 2
if w1 == 'sc':
if w2 in ['rk','sw']:
return 2
else:
return 1
if w1 == 'pr':
if w2 in ['sc','sw']:
return 2
else:
return 1
if w1 == 'sw':
if w2 in ['sc','pr']:
return 1
else:
return 2
def play(p1,p2):
round = 0
gwinner1 = 0
gwinner2 = 0
while round != 3:
print(f"ROUND {round+1}\n***************")
print("Enter 'rk' for Rock, 'pr' for Paper, 'sc' for Scissors, 'sw' for Saw")
w1 = input(f"{p1}'s weapon? : ")
if valid_weapon(w1):
while True:
w2 = input(f"{p2}'s weapon? : ")
if valid_weapon(w2):
break
else:
print("invalid choice!!!")
continue
cw = random.choice(['rk', 'pr', 'sc', 'sw'])
#print(cw)
winner1 = check_winner(w1,cw)
rounds[0][0] += 1
rounds[0][1] += 1
if winner1 == -1:
rounds[3][0] += 1
print(f"{p1} ties in round {round + 1} against computer")
elif winner1 == 1:
rounds[1][0] += 1
gwinner1 += 1
print(f"{p1} wins in round {round+1} against computer")
else:
rounds[2][0] += 1
print(f"{p1} lose in round {round + 1} against computer")
winner2 = check_winner(w2,cw)
if winner2 == -1:
rounds[3][1] += 1
print(f"{p2} ties in round {round + 1} against computer")
elif winner2 == 1:
rounds[1][1] += 1
gwinner2 += 1
print(f"{p2} wins in round {round + 1} against computer")
else:
rounds[2][1] += 1
print(f"{p2} lose in round {round + 1} against computer")
round += 1


else:
print("invalid choice!!!")
continue
games[0][0] += 1
games[0][1] += 1
print("\n******Game Result*****")
if gwinner1 == gwinner2:
games[3][0] += 1
games[3][1] += 1
print("Game ties")
if gwinner1 < gwinner2:
games[1][1] += 1
games[2][0] += 1
print(f"{p2} wins")
if gwinner1 > gwinner2:
games[1][0] += 1
games[2][1] += 1
print(f"{p1} wins")
input("Enter to continue...")


def stats(p1,p2):
print("\n******STATS******\n")
print(f"{p1} STATS")
print(f"Total Games = ",games[0][0])
print(f"Games Won = ",games[1][0])
print(f"Games Lost = ",games[2][0])
print(f"Games Ties = ",games[3][0])
print(f"Total Rounds = ",rounds[0][0])
print(f"Rounds Won = ",rounds[1][0])
print(f"Rounds Lost = ",rounds[2][0])
print(f"Rounds Ties = ",rounds[3][0])
print()
print(f"{p2} STATS")
print(f"Total Games = ", games[0][1])
print(f"Games Won = ", games[1][1])
print(f"Games Lost = ", games[2][1])
print(f"Games Ties = ", games[3][1])
print(f"Total Rounds = ", rounds[0][1])
print(f"Rounds Won = ", rounds[1][1])
print(f"Rounds Lost = ", rounds[2][1])
print(f"Rounds Ties = ", rounds[3][1])

def main():
p1 = player1_name()
p2 = player2_name(p1)
while True:
ch = menu()
if ch == 1:
play(p1,p2)
elif ch == 2:
rules()
elif ch == 3:
stats(p1,p2)
elif ch == 4:
print("Good Bye!")
break

if __name__ == '__main__':
main()

Demo

keep visiting : shaadcoding

shaad

2 Comments

  1. certainly like your web site however you have to take a
    look at the spelling on several of your posts.
    A number of them are rife with spelling issues and I in finding it very bothersome to inform the reality then again I will
    definitely come again again.

Leave a Reply

Your email address will not be published. Required fields are marked *