提示:本文内容来源于UCB CS61A课程,详情请点击CS 61A: Structure and Interpretation of Computer Programs

文章目录

  • 前言
  • Phase2 Commentary
    • Problem 06
    • Problem 07

前言

本文收录在文章CS61A 课程 Project1 The Game of Hog


提示:以下是本篇文章正文内容,下面代码除课程提供外均为本人自主完成,仅供参考

Phase2 Commentary

本阶段需要完成一个函数,该函数会在每位玩家每轮结束后对其作出注释,如

"22 points! That's the biggest gain yet for Player 1."

一个注释函数需要两个参数,玩家0的当前分数和玩家1的当前分数

其作用是打印分数中的任意一个或者两个,也可以打印其父环境中其他信息

由于分数的不同,每一轮的注释函数会有所差异,所以一个注释函数会返回下一轮将要调用的注释函数(注释函数的唯一副作用为打印信息)

课程提供了一个注释函数的样例say_scores,此函数单纯打印了两位玩家的分数,同时将自身作为返回值(作为下一轮的注释函数)

def say_scores(score0, score1):"""A commentary function that announces the score for each player."""print("Player 0 now has", score0, "and Player 1 now has", score1)return say_scores

课程还提供了announce_lead_changes函数,该函数是一个高阶函数,其返回值可以追踪领先玩家,使得每轮游戏都会调用不同的评论性函数

def announce_lead_changes(last_leader=None):"""Return a commentary function that announces lead changes.>>> f0 = announce_lead_changes()>>> f1 = f0(5, 0)Player 0 takes the lead by 5>>> f2 = f1(5, 12)Player 1 takes the lead by 7>>> f3 = f2(8, 12)>>> f4 = f3(8, 13)>>> f5 = f4(15, 13)Player 0 takes the lead by 2"""def say(score0, score1):if score0 > score1:leader = 0elif score1 > score0:leader = 1else:leader = Noneif leader != None and leader != last_leader:print('Player', leader, 'takes the lead by', abs(score0 - score1))return announce

除上述两个函数之外,还提供了一个函数both,该函数的两个参数f,g均为注释函数,该函数的返回值为一个新的注释函数,被返回的函数又会返回另一个注释函数,该函数按顺序依次调用fg返回的函数

函数如下

def both(f, g):"""Return a commentary function that says what f says, then what g says.>>> h0 = both(say_scores, announce_lead_changes())>>> h1 = h0(10, 0)Player 0 now has 10 and Player 1 now has 0Player 0 takes the lead by 10>>> h2 = h1(10, 6)Player 0 now has 10 and Player 1 now has 6>>> h3 = h2(6, 17)Player 0 now has 6 and Player 1 now has 17Player 1 takes the lead by 11    NOTE: the following game is not possible under the rules, it's justan example for the sake of the doctest"""def say(score0, score1):return both(f(score0, score1), g(score0, score1))return say

关于嵌套含数的调用请参照1.2.5 Evaluating Nested Expressions的内容

Problem 06

本节要求更新play函数的内容,使得注释函数可以在每轮游戏中被调用,而注释函数得返回值同时是下一轮游戏得注释函数

课程提供了提问脚本用于确保学生完全理解函数的要求

$ python3 ok -q 06 -u --local
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unlocking testsAt each "? ", type what you would expect the output to be.
Type exit() to quit---------------------------------------------------------------------
Question 6 > Suite 1 > Case 1
(cases remaining: 7)Q: What does a commentary function return?
Choose the number of the correct choice:
0) An integer representing the score.
1) None.
2) Another commentary function.
? 2
-- OK! -----------------------------------------------------------------------
Question 6 > Suite 2 > Case 1
(cases remaining: 6)>>> from hog import play, always_roll
>>> from dice import make_test_dice
>>> #
>>> def echo(s0, s1):
...     print(s0, s1)
...     return echo
>>> s0, s1 = play(always_roll(1), always_roll(1), dice=make_test_dice(3), goal=5, say=echo)
(line 1)? 3 0
(line 2)? 3 3
(line 3)? 9 3
-- OK! -----------------------------------------------------------------------
Question 6 > Suite 2 > Case 2
(cases remaining: 5)>>> from hog import play, always_roll
>>> from dice import make_test_dice
>>> #
>>> def count(n):
...     def say(s0, s1):
...         print(n, s0)
...         return count(n + 1)
...     return say
>>> s0, s1 = play(always_roll(1), always_roll(1), dice=make_test_dice(3), goal=10, say=count(1))
(line 1)? 1 3
(line 2)? 2 3
(line 3)? 3 9
(line 4)? 4 9
(line 5)? 5 15
-- OK! -----------------------------------------------------------------------
Question 6 > Suite 2 > Case 3
(cases remaining: 4)>>> from hog import play, always_roll
>>> from dice import make_test_dice
>>> #
>>> def echo(s0, s1):
...     print(s0, s1)
...     return echo
>>> strat0 = lambda score, opponent: 1 - opponent // 10
>>> strat1 = always_roll(3)
>>> s0, s1 = play(strat0, strat1, dice=make_test_dice(4, 2, 6), goal=15, say=echo)
(line 1)? 4 0
(line 2)? 4 12
(line 3)? 12 13
(line 4)? 12 25
-- OK! -----------------------------------------------------------------------
Question 6 > Suite 2 > Case 4
(cases remaining: 3)>>> from hog import play, always_roll
>>> from dice import make_test_dice
>>> #
>>> # Ensure that say is properly updated within the body of play.
>>> def total(s0, s1):
...     print(s0 + s1)
...     return echo
>>> def echo(s0, s1):
...     print(s0, s1)
...     return total
>>> s0, s1 = play(always_roll(1), always_roll(1), dice=make_test_dice(2, 3), goal=15, say=echo)
(line 1)? 2 0
(line 2)? 5
(line 3)? 4 3
(line 4)? 13
(line 5)? 6 9
(line 6)? 21
-- OK! -----------------------------------------------------------------------
Question 6 > Suite 3 > Case 1
(cases remaining: 2)>>> from hog import play, always_roll, both, announce_lead_changes, say_scores
>>> from dice import make_test_dice
>>> #
>>> def echo_0(s0, s1):
...     print('*', s0)
...     return echo_0
>>> def echo_1(s0, s1):
...     print('**', s1)
...     return echo_1
>>> s0, s1 = play(always_roll(1), always_roll(1), dice=make_test_dice(2), goal=3, say=both(echo_0, echo_1))
(line 1)? * 2
(line 2)? ** 0
(line 3)? * 2
(line 4)? ** 2
(line 5)? * 4
(line 6)? ** 2
-- OK! -----------------------------------------------------------------------
Question 6 > Suite 3 > Case 2
(cases remaining: 1)-- Already unlocked -----------------------------------------------------------------------
OK! All cases for Question 6 unlocked.

函数得实现只需要在if和else得语句块末尾分别添加以下表达式即可

say = say(score0, score1)

可以通过脚本得检测

$ python3 ok -q 06 --local
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests---------------------------------------------------------------------
Test summary7 test cases passed! No cases failed.

Problem 07

该部分要求实现announce_highest函数,该函数为一个返回值为注释函数得高阶函数
该注释函数只有在某个特定玩家在某一轮游戏中获得比以往更多的分数时才会打印信息

例如,announce_highest(1) 及其返回值完全忽略玩家0,只打印有关玩家1的信息。

要计算得分,必须目标玩家将上一回合的得分与本回合的得分进行比较(目标玩家由参数who指定)

除此之外,该函数须跟踪玩家到目前为止的最高得分

课程提供提问脚本以确保学生完全理解函数的需求

$ python3 ok -q 07 -u
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unlocking testsAt each "? ", type what you would expect the output to be.
Type exit() to quit---------------------------------------------------------------------
Question 7 > Suite 1 > Case 1
(cases remaining: 5)Q: What does announce_highest return?
Choose the number of the correct choice:
0) A string containing the largest point increase for thecurrent player.
1) The current largest point increase between bothplayers.
2) A commentary function that prints information about thebiggest point increase for the current player.
? 2
-- OK! -----------------------------------------------------------------------
Question 7 > Suite 1 > Case 2
(cases remaining: 4)Q: When does the commentary function returned by announce_highest
print something out?
Choose the number of the correct choice:
0) After each turn.
1) When the current player, given by the parameter `who`,earns the biggest point increase yet between bothplayers in the game.
2) When the current player, given by the parameter `who`,earns their biggest point increase yet in the game.
? 2
-- OK! -----------------------------------------------------------------------
Question 7 > Suite 1 > Case 3
(cases remaining: 3)Q: What does the parameter last_score represent?
Choose the number of the correct choice:
0) The current player's score before this turn.
1) The opponent's score before this turn.
2) The last highest gain for the current player.
? 0
-- OK! -----------------------------------------------------------------------
OK! All cases for Question 7 unlocked.

函数的实现如下:

def announce_highest(who, last_score=0, running_high=0):"""Return a commentary function that announces when WHO's scoreincreases by more than ever before in the game.NOTE: the following game is not possible under the rules, it's justan example for the sake of the doctest>>> f0 = announce_highest(1) # Only announce Player 1 score gains>>> f1 = f0(12, 0)>>> f2 = f1(12, 11)11 point(s)! That's the biggest gain yet for Player 1>>> f3 = f2(20, 11)>>> f4 = f3(13, 20)>>> f5 = f4(20, 35)15 point(s)! That's the biggest gain yet for Player 1>>> f6 = f5(20, 47) # Player 1 gets 12 points; not enough for a new high>>> f7 = f6(21, 47)>>> f8 = f7(21, 77)30 point(s)! That's the biggest gain yet for Player 1>>> f9 = f8(77, 22) # Swap!>>> f10 = f9(33, 77) # Swap!55 point(s)! That's the biggest gain yet for Player 1"""assert who == 0 or who == 1, 'The who argument should indicate a player.'# BEGIN PROBLEM 7"*** YOUR CODE HERE ***"def track_highest(score0, score1, highest=running_high):if who == 0:this_score = score0else:this_score = score1gain = this_score - last_scoreif gain > highest:print(gain, "point(s)! That's the biggest gain yet for Player", who)highest = gainreturn announce_highest(who, this_score, highest)return track_highest# END PROBLEM 7

该函数的实现满足评分脚本要求

$ python3 ok -q 07 --local
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests---------------------------------------------------------------------
Test summary5 test cases passed! No cases failed.

CS61A 课程项目 Hog, Phase2: Commontary相关推荐

  1. 项目背景怎么描述_课程游戏背景下幼儿户外活动的组织和实施 ——记岱山县课程项目实施组活动...

    课程游戏背景下 幼儿户外活动的组织与实施 --记岱山县课程项目实施组活动 为了深入推进园本化课程实施的实践与研究,加强项目组幼儿园课程的建设与实施,提升项目组幼儿园课程质量.11月23日,县课程项目实 ...

  2. 麻省理工开放官方课程项目!

    2022 年起,Blended Learning 麻省理工学院官方课程项目面向国际在职人士开放课程席位. 参与项目,学习者可获得 MIT 或哈佛商学院官方证书.得到名校导师亲笔推荐信.收获全球顶尖科技 ...

  3. 计算机基础项目任务教学重构,面向计算思维培养的中职课程项目式重构研究

    摘要: 21世纪以来,随着信息技术的飞速发展,有专家指出,计算思维应该是继实验思维和理论思维之后人们应该具备的第三种思维;我国2017年发布的高中信息技术课程标准中明确提出了计算思维的概念,并将其作为 ...

  4. 计算机专业课程项目教学教学设计,高职旅游管理专业计算机课程项目化教学设计...

    <高职旅游管理专业计算机课程项目化教学设计>由会员分享,可在线阅读,更多相关<高职旅游管理专业计算机课程项目化教学设计(3页珍藏版)>请在装配图网上搜索. 1.高职旅游管理专业 ...

  5. 2019新版《黑马web前端课程+项目实践课程》

    黑马web前端课程+项目实践课程 黑马web前端课程+项目实践课程 黑马web前端课程+项目实践课程 下载地址:百度网盘

  6. 相关旅游专业的计算机课程,高职旅游管理专业计算机课程项目化教学设计论文...

    高职旅游管理专业计算机课程项目化教学设计论文 近年来,各高职院校不断深化计算机课程的教学改革,但目前仍然存在着诸多问题,例如学生学习兴趣不大.教学手段单一.教学与实践分离.无法真正服务服务需求和岗位需 ...

  7. 系统设计与分析课程项目个人小结

    文章目录 系统设计与分析课程项目个人小结 简短的课程学习自我总结 管理 分析 设计 开发 PSP2.1 表格 最得意/或有价值/或有苦劳的工作清单 最得意 最有价值 最有劳苦 个人GIT总结 Dash ...

  8. 视频教程-PMP@第六版项目管理视频课程项目整体管理-项目管理

    PMP@第六版项目管理视频课程项目整体管理 项目管理师.信息系统项目管理师.项目经理.美国项目管理专业人士.网络规划设计师.美国现代项目管理知识体系培训师,全国第一批网络规划设计师,并进入全国前50名 ...

  9. python架构师培训课程_Python从零到架构师课程 六大阶段Python高级课程+项目实战 尚学堂全方位Python课程...

    Python从零到架构师课程  六大阶段Python高级课程+项目实战 尚学堂全方位Python课程 3.JPG (31.16 KB, 下载次数: 1) 2019-9-17 23:36 上传 2.JP ...

最新文章

  1. 前端性能优化之gzip
  2. 视频动作识别--Two-Stream Convolutional Networks for Action Recognition in Videos
  3. java代码读取dbsequence的值_MongoDB自增序列实现 - Java多线程同步 synchronized 用法
  4. Linux下怎么创建和进入带有空格的文件夹
  5. [bzoj2243][SDOI2011]染色
  6. 注意.NET Core进行请求转发问题
  7. python c4.5完整代码_python实现c4.5/Id3自我练习
  8. Mock服务设计与实现:MySQL驱动字节码修改增强
  9. POJ1077 Eight —— 反向BFS
  10. 【栈与队列】剑指offer:两个栈模拟队列
  11. win10计算机用户文件夹改名字怎么改,win10如何改成自己想要的文件夹用户名
  12. socket 关于同一条TCP链接数据包到达顺序的问题
  13. 【应用案例】CANape支持基于模型的ECU开发
  14. ***抓鸡和上传方法
  15. 苹果手机专用计算机,使用苹果手机,发现iphone连不上wifi怎么办?连不上wifi解决方法...
  16. Java深入理解深拷贝和浅拷贝区别
  17. 课堂笔记 Numpy酒鬼漫步
  18. 第04课:组件和商品详情
  19. 一个UWP 框架开发的哔哩哔哩非官方应用
  20. 【XSY2733】Disembrangle DP

热门文章

  1. Oracle VM VirtualBox 不可用
  2. N 沟道 MOS管松木ME50N06A-G(替代)新洁能NCE6030K/NCE6020AK方案
  3. SSO单点登录(集成SSO认证服务)
  4. error launching idea maxjavastacktracedepth=-1
  5. 第三次全国土地调查业务培训考试试题分析
  6. Codeforces Round #579 (Div. 3)--Boxers(贪心,排序)
  7. linux关闭磁盘缓存,在linux上禁用apache2的所有磁盘缓存
  8. 除甲醛产品哪个好?除甲醛产品十大品牌第一名
  9. 从零开始的Arduino单片机开发(1):初识Arduino
  10. PyTorch:优化神经网络训练的17种方法