Mini-project # 1 - Rock-paper-scissors-lizard-Spock
2013-10-20 14:04
621 查看
An Introduction to Interactive Programming in Python 这周的mini
project作业,花了我很长时间,本来想用github的,可是不会用,暂时在这里留个备份
Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously &ldqup;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
Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details).
Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties more unlikely.
Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS's popularity is that it has been featured
in 3 episodes of the TV series "The Big Bang Theory". The Wikipedia entry for RPSLS gives the complete
description of the details of the game.
In our first mini-project, we will build a Python function
takes as input the string
or
of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule that we will next describe.
While Rock-paper-scissor-lizard-Spock has a set of ten rules that logically determine who wins a round of RPSLS, coding up these rules would require a large number (5x5=25) of
in your mini-project code. A simpler method for determining the winner is to assign each of the five choices a number:
0 — rock
1 — Spock
2 — paper
3 — lizard
4 — scissors
In this expanded list, each choice wins against the preceding two choices and loses against the following two choices. In all of the mini-projects for this class, I will provide a walk through of the steps involved in building your project to aid its development.
A template for your mini-project is available here. Please work from this template.
Build a helper function
converts the string
4 as described above. This function should use a sequence of
You can use conditions of the form
etc. to distinguish the cases.To make debugging your code easier, we suggest including a final
that catches cases when
five correct input strings and prints an appropriate error message.
Next, you should build a second helper function
converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final
that catches cases when
range.
Build the first part of the main function
converts
0 and 4 using the helper function
Build the second part of
generates a random number
4 using the function
with
deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range.
Build the last part of
determines and prints out the winner. This test is actually very simple if you use the remainder operation (
Python) to the difference between
If this is not immediately obvious to you, I would suggest reviewing the "More operations" and "RPSLS" videos on remainders and modular arithmetic as well as experimenting with the remainder operator
a separate CodeSkulptor window to understand its behavior.
Using the helper function
you should produce four
line, print out the player's choice, print out the computer's choice and print out the winner.
This will be the only mini-project in the class that is not an interactive game. Since we have not yet learned enough to allow you to play the game interactively, you will simply call your
repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you
are testing, feel free to modify those calls, but make sure they are restored when you hand in your mini-project, as your peer assessors will expect them to be there.
The output of running your program should have the following form:
Note that, for this initial mini-project, we will focus only on testing whether your implementation of
correctly on valid input.
Your peers will assess your mini-project according to the rubric given below. To guide you in determining whether your project satisfies each item in the rubric, please consult the video that demonstrates our implementation of "Rock-paper-scissors-lizard-Spock".
Small deviations from the textual output of our implementation are fine. You should avoid large deviations (such as using the Python function
input your guesses). Whether moderate deviations satisfy an item of the grading rubric is at your peers' discretion during their assessment.
Here is a break down of the scoring:
2 pts — A valid CodeSkulptor URL was submitted. Give no credit if solution code was pasted into the submission field. Give 1 pt if an invalid CodeSkulptor URL was submitted.
2 pts — Program implements the function
the helper function
code. Give partial credit of 1 pt if only the function
plausible code.
1 pt — Running program does not throw an error.
1 pt — Program prints blank lines between games.
2 pts — Program prints
a string of the form
Give 1 pt if program prints out number instead of string.
2 pts — Program prints
a string of the form
Give 1 pt if program prints out number instead of string.
1 pt — Computer's guesses vary between five calls to
each run of the program.
1 pt — Computer's guesses vary between runs of the program.
3 pts — Program prints either
3 pts — Program chooses correct winner according to RPSLS rules. Please manually examine 5 cases for correctness. If all five cases are correct, award 3 pts; four cases correct award 2 pts; one to three cases correct award 1 pt; no cases correct
award 0 pts.
project作业,花了我很长时间,本来想用github的,可是不会用,暂时在这里留个备份
Mini-project description — Rock-paper-scissors-lizard-Spock
Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously &ldqup;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
Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details).
Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties more unlikely.
Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS's popularity is that it has been featured
in 3 episodes of the TV series "The Big Bang Theory". The Wikipedia entry for RPSLS gives the complete
description of the details of the game.
In our first mini-project, we will build a Python function
rpsls(name)that
takes as input the string
name, which is one of
"rock",
"paper",
"scissors",
"lizard",
or
"Spock". The function then simulates playing a round
of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule that we will next describe.
While Rock-paper-scissor-lizard-Spock has a set of ten rules that logically determine who wins a round of RPSLS, coding up these rules would require a large number (5x5=25) of
if/
elif/
elseclauses
in your mini-project code. A simpler method for determining the winner is to assign each of the five choices a number:
0 — rock
1 — Spock
2 — paper
3 — lizard
4 — scissors
In this expanded list, each choice wins against the preceding two choices and loses against the following two choices. In all of the mini-projects for this class, I will provide a walk through of the steps involved in building your project to aid its development.
A template for your mini-project is available here. Please work from this template.
Mini-project development process
Build a helper function name_to_number(name)that
converts the string
nameinto a number between 0 and
4 as described above. This function should use a sequence of
if/
elif/
elseclauses.
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
elseclause
that catches cases when
namedoes not match any of the
five correct input strings and prints an appropriate error message.
Next, you should build a second helper function
number_to_name(num)that
converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final
elseclause
that catches cases when
numberis not in the correct
range.
Build the first part of the main function
rpsls(name)that
converts
nameinto the number
player_numberbetween
0 and 4 using the helper function
name_to_number.
Build the second part of
rpsls(name)that
generates a random number
comp_numberbetween 0 and
4 using the function
random.randrange(). I suggest experimenting
with
randrangein 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.
Build the last part of
rpsls(name)that
determines and prints out the winner. This test is actually very simple if you use the remainder operation (
%in
Python) to the difference between
comp_numberand
player_number.
If this is not immediately obvious to you, I would suggest reviewing the "More operations" and "RPSLS" videos on remainders and modular arithmetic as well as experimenting with the remainder operator
%in
a separate CodeSkulptor window to understand its behavior.
Using the helper function
number_to_name,
you should produce four
line, print out the player's choice, print out the computer's choice and print out the winner.
This will be the only mini-project in the class that is not an interactive game. Since we have not yet learned enough to allow you to play the game interactively, you will simply call your
rpslsfunction
repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you
are testing, feel free to modify those calls, but make sure they are restored when you hand in your mini-project, as your peer assessors will expect them to be there.
The output of running your program should have the following form:
Player chooses rock Computer chooses scissors Player wins! Player chooses Spock Computer chooses lizard Computer wins! Player chooses paper Computer chooses lizard Computer wins! Player chooses lizard Computer chooses scissors Computer wins! Player chooses scissors Computer chooses Spock Computer wins!
Note that, for this initial mini-project, we will focus only on testing whether your implementation of
rpsls()works
correctly on valid input.
Grading rubric — 18 pts total (scaled to 100 pts)
Your peers will assess your mini-project according to the rubric given below. To guide you in determining whether your project satisfies each item in the rubric, please consult the video that demonstrates our implementation of "Rock-paper-scissors-lizard-Spock".Small deviations from the textual output of our implementation are fine. You should avoid large deviations (such as using the Python function
inputto
input your guesses). Whether moderate deviations satisfy an item of the grading rubric is at your peers' discretion during their assessment.
Here is a break down of the scoring:
2 pts — A valid CodeSkulptor URL was submitted. Give no credit if solution code was pasted into the submission field. Give 1 pt if an invalid CodeSkulptor URL was submitted.
2 pts — Program implements the function
rpsls()and
the helper function
name_to_number()with plausible
code. Give partial credit of 1 pt if only the function
rpsls()has
plausible code.
1 pt — Running program does not throw an error.
1 pt — Program prints blank lines between games.
2 pts — Program prints
"Player chooses player_guess"where
player_guessis
a string of the form
"rock",
"paper",
"scissors",
"lizard"or
"Spock".
Give 1 pt if program prints out number instead of string.
2 pts — Program prints
"Computer chooses computer_guess"where
computer_guessis
a string of the form
"rock", "paper", "scissors", "lizard"or
"Spock".
Give 1 pt if program prints out number instead of string.
1 pt — Computer's guesses vary between five calls to
rpsls()in
each run of the program.
1 pt — Computer's guesses vary between runs of the program.
3 pts — Program prints either
"Player and computer tie!",
"Player wins!"or
"Computer wins!"to report outcome. (1 pt for each message.)
3 pts — Program chooses correct winner according to RPSLS rules. Please manually examine 5 cases for correctness. If all five cases are correct, award 3 pts; four cases correct award 2 pts; one to three cases correct award 1 pt; no cases correct
award 0 pts.
# Rock-paper-scissors-lizard-Spock template # The key idea of this program is to equate the strings # "rock", "paper", "scissors", "lizard", "Spock" to numbers # as follows: # # 0 - rock # 1 - Spock # 2 - paper # 3 - lizard # 4 - scissors # helper functions import random def number_to_name(number): # fill in your code below # convert number to a name using if/elif/else # don't forget to return the result! 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 "Oops, Number is not in the correct range." def name_to_number(name): # fill in your code below # convert name to number using if/elif/else # don't forget to return the result! 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 "Oops, Name does not match." def rpsls(name): # fill in your code below # convert name to player_number using name_to_number player_number = name_to_number(name) # compute random guess for comp_number using random.randrange() comp_number = random.randrange(0, 5) # compute difference of player_number and comp_number modulo five a = ( player_number - comp_number ) % 5 # use if/elif/else to determine winner if a == 0: results = "Player and Computer tie!" elif a == 1 or a == 2: results = "Player wins!" elif a == 3 or a == 4: results = "Computer wins!" # convert comp_number to name using number_to_name comp_name = number_to_name (comp_number) # print results print "Player chooses " + str(name) print "Computer chooses " + str(comp_name) print results print "" # test your code rpsls("rock") rpsls("Spock") rpsls("paper") rpsls("lizard") rpsls("scissors") # always remember to check your completed program against the grading rubric
相关文章推荐
- 莱斯大学Python课程Mini-project # 1- Rock-paper-scissors-lizard-Spock
- Interactive Python:Mini-project # 1 - Rock-paper-scissors-lizard-Spock
- Mini-project # 1 - Rock-paper-scissors-lizard-Spock
- Mini-project # 1 - Rock-paper-scissors-lizard-Spock
- Mini-project # 1 - Rock-paper-scissors-lizard-Spock
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock
- [Interactive Python] Rock-paper-scissors-lizard-Spock 游戏 Mini-project #1
- Mini-project description 1 - Rock-paper-scissors-lizard-Spock
- Python Mini Project - Rock Paper Scissors Lizard Spock
- 作业week1 'Rock-paper-scissors-lizard-Spock'
- Python 8 - Rock-paper-scissors-lizard-spock
- 1 paper-rock-scissors-lizard-spock
- (Rock, Paper, Scissors, Lizard and Spock)sheldon谢耳朵独创的剪刀,布,石头,蜥蜴,史波克 用代码实现
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
- Rock-paper-scissors-lizard-Spock
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 1 Rock-paper-scissors
- [Mini]HTML5-游戏设计-Rock,Paper and Scissors Game
- IIPP迷你项目(一)“Rock-paper-scissor-lizard-Spock”
- IIPP迷你项目(一)“Rock-paper-scissor-lizard-Spock”