Created
July 19, 2014 17:54
-
-
Save DomWilliams0/b73f9b12a460dd7f4318 to your computer and use it in GitHub Desktop.
A simple rock-paper-scissors game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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