''' Name: Catie Welsh Date: 3/20/2015 Class: CS142 Pledge: I have neither given nor received unauthorized aid on this program. Description: This program lets you play one round of the game Rock-Paper-Scissors-Lizard-Spock. Input: Player names and their gestures/throws. Output: Status of the game and who wins. ''' # Holds the names of the gestures you can throw. # OK to make this a global constant because this never changes. gestures = ["Rock", "Paper", "Scissors", "Lizard", "Spock"] # This matrix (aka 2D list or 2D array) stores the winner and loser for # each pair of gestures. Gestures are indexed by the "gestures" array above. # If gesture x beats gesture y, then winloss[x][y] = 1 and winloss[y][x] = 0. # If gesture x ties gesture y, then winloss[x][y] = 2. # For example, winloss[0][1] = 0, which means Rock (gesture 0) loses to # Paper (gesture 1). Conversely, winloss[1][0] = 1. winloss = [[2, 0, 1, 1, 0], [1, 2, 0, 0, 1], [0, 1, 2, 1, 0], [0, 1, 0, 2, 1], [1, 0, 1, 0, 2]] def main(): print_greeting() name1 = input("Please enter Player 1's name: ") name2 = input("Please enter Player 2's name: ") # throw1 and throw2 hold the numbers of the gestures throw1 = input_throw(name1, name2) throw2 = input_throw(name2, name1) # winner is 1 if player 1 wins, 0 if player 1 loses, 2 for a tie. winner = calc_winner(throw1, throw2) while winner == 2: print("It's a tie; both players will throw again.") throw1 = input_throw(name1, name2) throw2 = input_throw(name2, name1) winner = calc_winner(throw1, throw2) if winner == 1: print(gestures[throw1], "defeats", gestures[throw2] + ".") print(name1, "defeats", name2 + ".") else: print(gestures[throw2], "defeats", gestures[throw1] + ".") print(name2, "defeats", name1 + ".") # Prints a greeting. # Parameters: None # Returns: None def print_greeting(): print("Welcome to Rock-Paper-Scissors-Lizard-Spock!") # Prints a list of all gestures. # Parameters: None # Returns: None def print_gestures(): print("Choices are:") print("(0) Rock, (1) Paper, (2) Scissors, (3) Lizard, (4) Spock") # Lets the user type in a number between 0 and 5. # Parameters: None # Returns: the number typed. def input_throw(thrower, opponent): print("It is", thrower + "'s turn.") print_gestures() print("No peeking,", opponent + ".") the_throw = int(input(thrower + ", what is your throw? ")) while the_throw < 0 or the_throw >= len(gestures): print("Invalid choice; try again.") the_throw = int(input(thrower + ", what is your throw? ")) print(thrower, "throws", gestures[the_throw] + ".") return the_throw # Calculates the winner of one round of the game. # Parameters: None # Returns: the winner as an integer: 1 if player 1 wins, # 0 if player 1 loses, 2 if they tie. def calc_winner(throw1, throw2): return winloss[throw1][throw2] main()