Rock-Paper-Scissors is a popular game played across the world. In this article, the game is implemented using a simple AI and a command line interface in Python programming language.
Environment
The code is built and executed using the following tools,
- Operating System - Windows 10
- Interpreter - Python 3.10
- Editor - Visual Studio Code
The game play is very simple, two players pick one of rock, paper or scissors. If they both choose the same value, then the round is drawn. Otherwise, the winner is decided based on the following logic,
- Rock beats Scissors
- Scissors beat Paper
- Paper beats Rock
To simulate the game play, the player (or the user) plays against an AI which chooses an option before the player decides. The winner is decided after the player chooses an option, the scores are updated as per the winner and gets displayed at the end of each round. To simulate many rounds, the code is run in an infinite loop until the user wishes to exit the game. The code is as follows,
import random validInput = ['R', 'P', 'S'] rps = { 'R': {'name': 'Rock', 'beats': 'S'}, 'P': {'name': 'Paper', 'beats': 'R'}, 'S': {'name': 'Scissors', 'beats': 'P'} } score = {'ai': 0, 'user': 0} random.seed() while True: aiOption = random.choice(validInput) userOption = input("Rock [R]/Paper [P]/Scissors [S]/Exit [E]: ").upper() if userOption == 'E': break if userOption not in validInput: print('error: Invalid input') continue print(f"User - {rps[userOption]['name']}, AI - {rps[aiOption]['name']}") if rps[userOption]['beats'] == aiOption: print(f"{rps[userOption]['name']} beat {rps[aiOption]['name']}") score['user'] += 1 elif rps[aiOption]['beats'] == userOption: print(f"{rps[aiOption]['name']} beat {rps[userOption]['name']}") score['ai'] += 1 else: print('Draw') print(f"Score: User - {score['user']}, AI - {score['ai']}")
The list of valid inputs are maintained in an array named validInput. The game logic is enclosed in a dictionary of dictionaries named rps and scores of both AI and player are kept in another dictionary named score.
The code starts with initializing a seed for the random library and jumps right into the infinite loop. For each iteration, the AI picks a random value from the list of valid inputs and the code waits for the player to enter an input. The input is validated later, and an invalid input will put back the user to enter an option again. The game then decides the winner or draw based on the input from the player and the AI, increases score if there is a clear winner and displays the score at the end of each round. The command line output is as follows,
> python.exe rps.py Rock [R]/Paper [P]/Scissors [S]/Exit [E]: r User - Rock, AI - Rock Draw Score: User - 0, AI - 0 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: p User - Paper, AI - Scissors Scissors beat Paper Score: User - 0, AI - 1 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: s User - Scissors, AI - Paper Scissors beat Paper Score: User - 1, AI - 1 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: r User - Rock, AI - Paper Paper beat Rock Score: User - 1, AI - 2 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: p User - Paper, AI - Rock Paper beat Rock Score: User - 2, AI - 2 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: s User - Scissors, AI - Scissors Draw Score: User - 2, AI - 2 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: r User - Rock, AI - Rock Draw Score: User - 2, AI - 2 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: p User - Paper, AI - Paper Draw Score: User - 2, AI - 2 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: s User - Scissors, AI - Rock Rock beat Scissors Score: User - 2, AI - 3 Rock [R]/Paper [P]/Scissors [S]/Exit [E]: e
Thanks for reading. Please leave suggestions/comments (if any).
Comments
Post a Comment