Skip to content

Instantly share code, notes, and snippets.

@DomWilliams0
Created July 19, 2014 17:54
Show Gist options
  • Save DomWilliams0/b73f9b12a460dd7f4318 to your computer and use it in GitHub Desktop.
Save DomWilliams0/b73f9b12a460dd7f4318 to your computer and use it in GitHub Desktop.
A simple rock-paper-scissors game
import random
from time import sleep
moves = ["rock", "paper", "scissors"]
choices = [x[0] for x in moves]
WIN, LOSS, DRAW = "WIN", "LOSS", "DRAW"
player_score = 0
computer_score = 0
def end_game():
print("You: %d | Computer: %d" % (player_score, computer_score))
exit()
def player_move():
while True:
m = input("[r]ock, [p]aper, [s]cissors or [q]uit >").lower()
if m in choices:
return moves[choices.index(m)]
if m in 'q':
end_game()
print("Invalid input!")
def determine_winner(computer_choice, player_choice):
computer = moves.index(computer_choice)
player = moves.index(player_choice)
if computer == player:
return DRAW
if abs(computer - player) == 1:
return WIN if player > computer else LOSS
return WIN if computer == choices[0] else LOSS
if __name__ == '__main__':
moves = list(map(str.upper, moves))
while True:
player = player_move()
computer = random.choice(moves)
result = determine_winner(computer, player)
width = 8
print("[%02d]%s -> %s" % (player_score, "You".center(width), player))
print("[%02d]%s -> %s" % (computer_score, "Computer".center(width), computer))
print("=> %s\n---" % result)
if result == WIN:
player_score += 1
elif result == LOSS:
computer_score += 1
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment