Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
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 "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 less likely.
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/else clauses
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 (if rock and scissors are thought of as being adjacent using modular arithmetic).
In all of the mini-projects for this class, we 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 stringnameinto a number between 0 and
4 as described above. This function should use a sequence ofif/elif/elseclauses.
You can use conditions of the formname == 'paper',
etc. to distinguish the cases. To make debugging your code easier, we suggest including a finalelseclause
that catches cases whennamedoes not match any of the
five correct input strings and prints an appropriate error message. You can test your implementation ofname_to_number()using
this name_to_number testing template. (Also available in the Code Clinic tips thread). - 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 finalelseclause
that catches cases whennumberis not in the correct
range. You can test your implementation ofnumber_to_name()using
this number_to_name testing template. - 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 numberplayer_numberbetween
0 and 4 corresponding to the player's choice by calling the helper functionname_to_number()usingplayer_choice. - 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 numbercomp_numberbetween
0 and 4 that corresponds to the computer's guess using the functionrandom.randrange().
We suggest experimenting withrandrangein 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 namecomp_choicecorresponding
to the computer's number using the functionnumber_to_name()and
print an appropriate message with the computer's choice to the console. - Implement the last part of
rpsls()that
determines and prints out the winner. Specifically, compute the difference betweencomp_numberandplayer_numbertaken
modulo five. Then write anif/elif/elsestatement 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 thisif/elif/elsestatement,
we suggest reviewing the "RPSLS" video which describes a simple test for determine the winner of RPSLS.
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 rpsls function
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 input to
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 functionname_to_number()with plausible
code. Give partial credit of 1 pt if only the functionrpsls()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_choice"whereplayer_choiceis
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 comp_choice"wherecomp_choiceis
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"Computerto report outcome. (1 pt for each message.)
wins!"
- 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.
<span style="font-size:18px;"># 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
import random
# helper functions def name_to_number(name):
# delete the following pass statement and fill in your code below
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 'wrong number' # convert name to number using if/elif/else
# don't forget to return the result! def number_to_name(number):
# delete the following pass statement and fill in your code below
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 'wrong number' # convert number to a name using if/elif/else
# don't forget to return the result! def rpsls(player_choice):
# delete the following pass statement and fill in your code below # print a blank line to separate consecutive games
# print out the message for the player's choice
print 'Player chooses ',player_choice
# convert the player's choice to player_number using the function name_to_number()
player_number = name_to_number(player_choice)
# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(0,4)
# convert comp_number to comp_choice using the function number_to_name()
comp_choice = number_to_name(comp_number)
# print out the message for computer's choice
print 'Computer chooses', comp_choice
# compute difference of comp_number and player_number modulo five
minus_number = comp_number - player_number
result_of_modulo = minus_number % 5
# use if/elif/else to determine winner, print winner message
if result_of_modulo ==0:
print 'Player and computer tie!'
elif result_of_modulo > 2:
print 'Player wins!'
else:
print 'Computer wins!' # test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors") # always remember to check your completed program against the grading rubric
</span>
代码链接:CodeSkulptor
Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"的更多相关文章
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
Mini-project #4 - "Pong" In this project, we will build a version of Pong, one of the firs ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习
Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...
- 【python】An Introduction to Interactive Programming in Python(week two)
This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习
#Practice Exercises for Logic and Conditionals # Solve each of the practice exercises below. # 1.Wri ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习
# Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...
- An Introduction to Interactive Programming in Python
这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...
- Quiz 6b Question 8————An Introduction to Interactive Programming in Python
Question 8 We can use loops to simulate natural processes over time. Write a program that calcula ...
- Quiz 6b Question 7————An Introduction to Interactive Programming in Python
Question 7 Convert the following English description into code. Initialize n to be 1000. Initiali ...
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...
随机推荐
- [C语言练习]学生学籍管理系统
/** * @copyright 2012 Chunhui Wang * * wangchunhui@wangchunhui.cn * * 学生学籍管理系统(12.06) */ #include &l ...
- Java的序列化
1.为啥需要序列化 在Java编程时,一个类被实例化以后,Java虚拟机使得对象处理生存状态,但是当虚拟机关闭后,对象就不复存在了,所以一个对象的生存期不会超过JVM的工作时间,那么如何才能让对象持续 ...
- android数据库持久化框架
android数据库持久化框架
- perl lwp get uft-8和gbk
gbk编码: jrhmpt01:/root/lwp# cat x2.pl use LWP::UserAgent; use DBI; $user="root"; $passwd='R ...
- 【HDU】病毒侵袭持续中(AC自己主动机+map)
一開始一直WA,之后发现这道题不止一组输入,改成多组输入之后就过了. 利用map把每一个字符串映射到它相应的结点上即可了. 11909467 2014-10-19 11:54:00 Accepted ...
- javascript的函数相关属性和方法
作为一名前端初学者,应该坚持每天去学习,去总结 ,去复习,去接触更新鲜的事物.但是这段时间很浮躁,虽说也是在一直学习,自己能吸收的少之又少.今日在这突然冒出来,实感惭愧. 1.函数名.name 获得函 ...
- CSS3滤镜
今天在办公室亲眼目睹了同事使用CSS3滤镜为一张漂亮的照片轮廓加上了阴影,瞬间亮瞎了我的的双眼,见笑了. 所以也迅速尝试使用CSS3滤镜让最新出炉的MUI LOGO也性感一把,试图来愉悦一下大家的双眼 ...
- sql server中关于批处理与脚本的简单介绍
1.批处理 批处理指的是包含一条或多条T-SQL语句的语句组,这组语句从应用程序一次性地发送到SQL Server服务器执行.SQL Server服务器将批处理语句编译成一个可执行单元(即执行计划), ...
- SSIS:捕获修改了的数据
获取修改了的数据一般有三种方式: 1.使用一个datetime列 缺点:是并不是每个表都会有个‘修改日期’字段来让你判断行是否修改过 使用实例可以参考我之前的文章:SSIS: 使用最大ID和最大日期来 ...
- unicode编码相互转换加密解密
需求:把字符串转换成unicode编码加密. 也可以把unicode编码解密并分析出汉字字母数字字符各多少个. unicode编码 \u 后面是一个16进制编码,必要时需要进行转换. 看源码: 0 & ...