From 8d68b79fb5a213c4848aa87ab0224804fac7db03 Mon Sep 17 00:00:00 2001 From: Amlan Mohanty <72063042+amlanmohanty1@users.noreply.github.com> Date: Thu, 1 Oct 2020 00:10:48 +0530 Subject: [PATCH] Added a game of "Rock, Paper and Scissors" using basic Python programming. --- Game_of_Rock,Paper,Scissors.ipynb | 130 ++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Game_of_Rock,Paper,Scissors.ipynb diff --git a/Game_of_Rock,Paper,Scissors.ipynb b/Game_of_Rock,Paper,Scissors.ipynb new file mode 100644 index 0000000..b31a55a --- /dev/null +++ b/Game_of_Rock,Paper,Scissors.ipynb @@ -0,0 +1,130 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Game_of_Rock,Paper,Scissors.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "code", + "metadata": { + "id": "hynDT8gLTPzO", + "outputId": "d69db102-db7c-47cb-a9d4-5d96d4ad42ed", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 311 + } + }, + "source": [ + "from random import choice\n", + "\n", + "rps = ('rock', 'paper', 'scissors')\n", + "rps_dict = {\n", + " 'rock': {'strong': 'scissors', 'weak': 'paper'},\n", + " 'paper': {'strong': 'rock', 'weak': 'scissors'},\n", + " 'scissors': {'strong': 'paper', 'weak': 'rock'}\n", + "}\n", + "\n", + "\n", + "def print_result(user_score, com_score, win, lose, tie):\n", + " print('Result:', end=' ')\n", + " if user_score > com_score:\n", + " print('You win!')\n", + " elif user_score < com_score:\n", + " print('You lose...')\n", + " else:\n", + " print('Draw.')\n", + " print('--------------------')\n", + " # Print scores\n", + " print(f'Your score: {user_score}')\n", + " print(f'Computer score: {com_score}')\n", + " print('--------------------')\n", + " # Print statistics\n", + " print(f'Win: {win}')\n", + " print(f'Lose: {lose}')\n", + " print(f'Tie: {tie}')\n", + "\n", + "\n", + "def play(rounds):\n", + " game_result = {\n", + " 'user_score': 0,\n", + " 'com_score': 0,\n", + " 'win': 0,\n", + " 'lose': 0, \n", + " 'tie': 0\n", + " }\n", + " while rounds > 0:\n", + " user_input = input(\n", + " 'Enter your choice (1: Rock, 2: Paper, 3: Scissors, 0: Quit): ')\n", + " # Check whether the input is in the options\n", + " if user_input in ('1', '2', '3'):\n", + " rounds -= 1\n", + " user_hand = rps[int(user_input) - 1]\n", + " com_hand = choice(rps)\n", + " # user_hand is strong against com_hand\n", + " if rps_dict[user_hand]['strong'] == com_hand:\n", + " game_result['user_score'] += 1\n", + " game_result['win'] += 1\n", + " result = 'You win!'\n", + " # user_hand is weak against com_hand\n", + " elif rps_dict[user_hand]['weak'] == com_hand:\n", + " game_result['com_score'] += 1\n", + " game_result['lose'] += 1\n", + " result = 'You lose...'\n", + " # Tie\n", + " else:\n", + " game_result['tie'] += 1\n", + " result = 'Tie.'\n", + " print(\n", + " f'You -> {user_hand}. Computer -> {com_hand}. {result}')\n", + " elif user_input == '0':\n", + " break\n", + " else:\n", + " print('Invalid input!')\n", + " print()\n", + " print_result(**game_result)\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " print('Welcome to Rock-Paper-Scissors Game!')\n", + " try:\n", + " rounds = int(input('How many rounds you want to play? '))\n", + " play(rounds)\n", + " except ValueError:\n", + " print('Please input a valid number!')" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Welcome to Rock-Paper-Scissors Game!\n", + "How many rounds you want to play? 2\n", + "Enter your choice (1: Rock, 2: Paper, 3: Scissors, 0: Quit): 1\n", + "You -> rock. Computer -> paper. You lose...\n", + "\n", + "Enter your choice (1: Rock, 2: Paper, 3: Scissors, 0: Quit): 2\n", + "You -> paper. Computer -> rock. You win!\n", + "\n", + "Result: Draw.\n", + "--------------------\n", + "Your score: 1\n", + "Computer score: 1\n", + "--------------------\n", + "Win: 1\n", + "Lose: 1\n", + "Tie: 0\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file