Skip to content

Instantly share code, notes, and snippets.

@gnagel
Last active August 29, 2015 14:25
Show Gist options
  • Save gnagel/b43f50e92402ee8c6ff3 to your computer and use it in GitHub Desktop.
Save gnagel/b43f50e92402ee8c6ff3 to your computer and use it in GitHub Desktop.
CodeSkulptor is unable to save at this time :-(
#
# CodeSkulptor URL:
# http://www.codeskulptor.org/#user40_H6qP6dj1LU_0.py
#
# rock_spock_paper_lizard_scissors.py
# 2015-07-25
# github.com/gnagel
#
#
# Rock-paper-scissors is a hand game that is played by two people.
# The players count to three in unison and simultaneously throw one
# of three hand signals that correspond to rock, paper or scissors.
# The winner is determined by the rules:
# * Rock smashes scissors
# * Scissors cuts paper
# * Paper covers rock
#
#
# Mini-project development process
#
# 1. Build a helper function name_to_number(name) that converts the string name into a number between 0 and 4 as described above. This function should use a sequence of if/elif/else clauses. You can use conditions of the form name == 'paper', etc. to distinguish the cases. To make debugging your code easier, we suggest including a final else clause that catches cases when name does not match any of the five correct input strings and prints an appropriate error message. You can test your implementation of name_to_number() using this name_to_number testing template. (Also available in the Code Clinic tips thread).
#
# 2. Next, you should build a second helper function number_to_name(number) that converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final else clause that catches cases when number is not in the correct range. You can test your implementation of number_to_name() using this number_to_name testing template.
#
# 3. Implement the first part of the main function rpsls(player_choice). Print out a blank line (to separate consecutive games) followed by a line with an appropriate message describing the player's choice. Then compute the number player_number between 0 and 4 corresponding to the player's choice by calling the helper function name_to_number() using player_choice.
#
# 4. Implement the second part of rpsls() that generates the computer's guess and prints out an appropriate message for that guess. In particular, compute a random number comp_number between 0 and 4 that corresponds to the computer's guess using the function random.randrange(). We suggest experimenting with randrange in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range. Then compute the name comp_choice corresponding to the computer's number using the function number_to_name() and print an appropriate message with the computer's choice to the console.
#
# 5. Implement the last part of rpsls() that determines and prints out the winner. Specifically, compute the difference between comp_number and player_number taken modulo five. Then write an if/elif/else statement whose conditions test the various possible values of this difference and then prints an appropriate message concerning the winner. If you have trouble deriving the conditions for the clauses of this if/elif/else statement, we suggest reviewing the "RPSLS" video which describes a simple test for determine the winner of RPSLS.
#
import random
# Valid names to use in the game:
NAMES = ["rock", "Spock", "paper", "lizard", "scissors"];
# 1. Build a helper function name_to_number(name) that converts the string name into a number between 0 and 4 as described above.
# This function should use a sequence of if/elif/else clauses.
# You can use conditions of the form name == 'paper', etc. to distinguish the cases.
# To make debugging your code easier, we suggest including a final else clause that catches cases when name
# does not match any of the five correct input strings and prints an appropriate error message.
# You can test your implementation of name_to_number() using this name_to_number testing template.
# (Also available in the Code Clinic tips thread).
#
def name_to_number(name):
if name == "rock":
return 0
elif name == "Spock":
return 1
elif name == "paper":
return 2
elif name == "lizard":
return 3
elif name == "scissors":
return 4
else:
print('Error! Name does not match.')
# 2. Next, you should build a second helper function number_to_name(number) that converts a number in the range 0 to 4 into its corresponding name as a string.
# Again, we suggest including a final else clause that catches cases when number is not in the correct range.
# You can test your implementation of number_to_name() using this number_to_name testing template.
#
def number_to_name(number):
if number == 0:
return "rock"
elif number == 1:
return "Spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
elif number == 4:
return "scissors"
else:
print('Error! Number is not in the correct range.')
# 3. Implement the first part of the main function rpsls(player_choice).
# Print out a blank line (to separate consecutive games) followed by a line with an appropriate message describing the player's choice.
# Then compute the number player_number between 0 and 4 corresponding to the player's choice by calling the helper function name_to_number() using player_choice.
#
# 4. Implement the second part of rpsls() that generates the computer's guess and prints out an appropriate message for that guess.
# In particular, compute a random number comp_number between 0 and 4 that corresponds to the computer's guess using the function random.randrange().
# We suggest experimenting with randrange in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range.
# Then compute the name comp_choice corresponding to the computer's number using the function number_to_name() and print an appropriate message with the computer's choice to the console.
#
# 5. Implement the last part of rpsls() that determines and prints out the winner.
# Specifically, compute the difference between comp_number and player_number taken modulo five.
# Then write an if/elif/else statement whose conditions test the various possible values of this difference and then prints an appropriate message concerning the winner.
# If you have trouble deriving the conditions for the clauses of this if/elif/else statement, we suggest reviewing the "RPSLS" video which describes a simple test for determine the winner of RPSLS.
#
def rpsls(player_choice):
print "\n"
print "Player chooses: ", player_choice
# 3. Compute the number player_number using player_choice
player_number = name_to_number(player_choice);
# 4. Compute a random number comp_number [0, 4] that corresponds to the computer's guess using the function random.randrange().
comp_number = random.randrange(0,5);
print "Computer chooses: ", number_to_name(comp_number)
# 5. Compute the difference between comp_number and player_number taken modulo five.
difference = ( player_number - comp_number ) % 5;
# 5. Write an if/elif/else statement whose conditions test the various possible values of this difference and then prints an appropriate message concerning the winner.
if difference == 0:
print "Player and computer tie!"
elif difference >= 3:
print "Computer wins!"
elif difference <= 2:
print "Player wins!"
# Do it up!
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment