Last active
November 25, 2019 15:07
-
-
Save Crocmagnon/0af5fd211183f9d7ee37f6e13b227584 to your computer and use it in GitHub Desktop.
A game of rock papers scissors lizard spock
This file contains hidden or 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 argparse | |
| import random | |
| ROCK = "rock" | |
| PAPER = "paper" | |
| SCISSORS = "scissors" | |
| LIZARD = "lizard" | |
| SPOCK = "spock" | |
| QUIT = "quit" | |
| COMPUTER_CHOICES = [ROCK, PAPER, SCISSORS, LIZARD, SPOCK] | |
| USER_CHOICES = [ROCK, PAPER, SCISSORS, LIZARD, SPOCK, QUIT] | |
| WINS_OVER = { | |
| ROCK: [SCISSORS, LIZARD], | |
| PAPER: [ROCK, SPOCK], | |
| SCISSORS: [PAPER, LIZARD], | |
| LIZARD: [SPOCK, PAPER], | |
| SPOCK: [SCISSORS, ROCK], | |
| } | |
| def pick(): | |
| return random.choice(COMPUTER_CHOICES) | |
| def print_scores(user_score, computer_score, draws, header="Scores"): | |
| print("{}:".format(header)) | |
| print("You - Computer - draws") | |
| print("{user} - {computer} - {draws}\n".format(user=user_score, computer=computer_score, draws=draws)) | |
| def main(args): | |
| user_score = 0 | |
| computer_score = 0 | |
| draws = 0 | |
| while True: | |
| print(", ".join(USER_CHOICES)) | |
| user_choice = None | |
| while not user_choice: | |
| try: | |
| user_choice = input("What is your choice ?\n> ") | |
| if user_choice not in USER_CHOICES: | |
| raise ValueError | |
| except (TypeError, ValueError): | |
| print("Answer not valid.") | |
| user_choice = None | |
| pass | |
| if user_choice == QUIT: | |
| print("\nThanks for playing!") | |
| print_scores(user_score, computer_score, draws, header="Final scores") | |
| return | |
| if args.godmode: | |
| computer_choice = WINS_OVER[user_choice][0] | |
| else: | |
| computer_choice = pick() | |
| print("Your choice : {}".format(user_choice)) | |
| print("Computer choice : {}".format(computer_choice)) | |
| if user_choice in WINS_OVER[computer_choice]: | |
| print("The computer won.") | |
| computer_score += 1 | |
| elif computer_choice in WINS_OVER[user_choice]: | |
| print("You won.") | |
| user_score += 1 | |
| else: | |
| print("It's a draw.") | |
| draws += 1 | |
| print_scores(user_score, computer_score, draws) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--godmode", action="store_true", help="You will always win.") | |
| args = parser.parse_args() | |
| main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment