Skip to content

Instantly share code, notes, and snippets.

@novasush
Last active March 6, 2020 15:42
Show Gist options
  • Save novasush/dfc2552bc2e60a532877cb98434d99e2 to your computer and use it in GitHub Desktop.
Save novasush/dfc2552bc2e60a532877cb98434d99e2 to your computer and use it in GitHub Desktop.
A simple python based rock paper scissor game
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from random import randint"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import clear_output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"randint(1,3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_rps(player_input):\n",
" rps = {\n",
" 1 : \"R\",\n",
" 2 : \"P\",\n",
" 3 : \"S\"\n",
" }\n",
" return rps[player_input]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_score(player1_input, player2_input):\n",
" if (player1_input == \"R\" and player2_input == \"S\") or (player1_input == \"S\" and player2_input == \"R\"):\n",
" return \"R\"\n",
" elif (player1_input == \"P\" and player2_input == \"S\") or (player1_input == \"S\" and player2_input == \"P\"):\n",
" return \"S\"\n",
" elif (player1_input == \"S\" and player2_input == \"R\") or (player1_input == \"R\" and player2_input == \"P\"):\n",
" return \"P\"\n",
" else:\n",
" return \"Draw\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_winner(player1_score , player2_score):\n",
" if player1_score > player2_score:\n",
" return \"Player 1 wins\"\n",
" elif player1_score < player2_score:\n",
" return \"Player 2 wins\"\n",
" else:\n",
" return \"It's a Draw\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"player1_score = player2_score = 0\n",
"winner = \"\"\n",
"for attempt in range(1,4): #5 attempts\n",
" player1_input = input('Please enter \"r\" for rock, \"p\" for paper or \"s\" for scissor: ')\n",
" player1_input = player1_input.upper()\n",
" player2_input = get_rps(randint(1,3))\n",
" score = get_score(player1_input , player2_input)\n",
" if score == player1_input:\n",
" player1_score += 1\n",
" print(\"Player 1 gets point\")\n",
" elif score == player2_input:\n",
" player2_score += 1\n",
" print(\"Player 2 gets point\")\n",
" print()\n",
" clear_output()\n",
" print(\"Player 1 input is {} \\nPlayer 2 input is {}\".format(player1_input,player2_input))\n",
" print()\n",
" print(\"Player 1 point: {} \\nPlayer 2 point: {}\".format(player1_score,player2_score))\n",
" print()\n",
" \n",
"print(\"Result: {}\".format(get_winner(player1_score,player2_score)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment