有网站怎么做下载直链seo优化教程自学
问题 5a(3 分)
实现该函数,该函数模拟了完整的 Hog 游戏。球员 交替轮流掷骰子,直到其中一名玩家达到分数。play
goal
您现在可以忽略 Feral Hogs 规则和论点; 您将在问题 5b 中实现它。feral_hogs
为了确定每回合掷出多少骰子,每个玩家使用他们的 各自的策略(玩家 0 使用,玩家 1 使用)。 策略是一种函数,给定玩家的分数和对手的分数 score,返回当前玩家想要掷的骰子数量 转。每个策略函数每回合只能调用一次。不用担心 关于实施策略的细节。您将在以下方面开发它们 第 3 阶段。strategy0
strategy1
游戏结束后,返回双方玩家的最终总分,其中 玩家 0 得分第一,玩家 1 得分第二。play
以下是一些提示:
- 你应该使用你已经写好的函数!您将需要使用所有三个参数进行调用。
take_turn
- 每回合只能呼叫一次。
take_turn
- 执行除野猪以外的所有特殊规则。
- 您可以通过调用来获取其他玩家的号码(0 或 1) 提供的功能。
other
- 您可以暂时忽略该函数的参数。您将使用 它在项目的第 2 阶段。
say
play
在编写任何代码之前,请解锁测试以验证您对问题的理解。
python3 ok -q 05a -u
完成解锁后,开始实施解决方案。您可以通过以下方式检查您的正确性:
python3 ok -q 05a
def play(strategy0, strategy1, score0=0, score1=0, dice=six_sided,goal=GOAL_SCORE, say=silence, feral_hogs=True):"""Simulate a game and return the final scores of both players, with Player0's score first, and Player 1's score second.A strategy is a function that takes two total scores as arguments (thecurrent player's score, and the opponent's score), and returns a number ofdice that the current player will roll this turn.strategy0: The strategy function for Player 0, who plays first.strategy1: The strategy function for Player 1, who plays second.score0: Starting score for Player 0score1: Starting score for Player 1dice: A function of zero arguments that simulates a dice roll.goal: The game ends and someone wins when this score is reached.say: The commentary function to call at the end of the first turn.feral_hogs: A boolean indicating whether the feral hogs rule should be active."""who = 0 # Who is about to take a turn, 0 (first) or 1 (second)# BEGIN PROBLEM 5"*** YOUR CODE HERE ***"
实现逻辑:
1.循坏条件满足:两人分数均小于规定分数
2.两人逻辑分别写
3.以其中一人0为例:首先计算当前回合抛骰子的次数(strategy0)
计算当前得分(take_turn)
如果满足交换规律,就交换(is_swap)
4.当前回合结束,更换下一个(直至结束)
AC代码:
while score0 <goal and score1 < goal:if who==0:get=strategy0(score0,score1)score0+=take_turn(get,score1,dice)if is_swap(score0,score1):score0,score1=score1,score0else:get = strategy1(score1, score0)score1+=take_turn(get,score0,dice)if is_swap(score1,score0):score0,score1=score1,score0who=other(who)# END PROBLEM 5# (note that the indentation for the problem 6 prompt (***YOUR CODE HERE***) might be misleading)# BEGIN PROBLEM 6"*** YOUR CODE HERE ***"# END PROBLEM 6return score0, score1
AC情况(注意要解锁之后,才能测试):
Test summary3 test cases passed before encountering first failed test caseBackup... 100% complete
Backup past deadline by 1241 days, 23 hours, 51 minutes, and 24 seconds
Backup successful for user: mxylms1210@163.comOK is up to date
PS D:\python文件\hog> python3 ok -q 05a
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests---------------------------------------------------------------------
Test summary11 test cases passed! No cases failed.Backup... 100% complete
Backup past deadline by 1241 days, 23 hours, 52 minutes, and 25 seconds
Backup successful for user: mxylms1210@163.comOK is up to date