Project 1: The Game of Hog(CS61A)

(第一阶段)问题 5a(3 分)

实现该函数,该函数模拟了完整的 Hog 游戏。球员 交替轮流掷骰子,直到其中一名玩家达到分数。playgoal

您现在可以忽略 Feral Hogs 规则和论点; 您将在问题 5b 中实现它。feral_hogs

为了确定每回合掷出多少骰子,每个玩家使用他们的 各自的策略(玩家 0 使用,玩家 1 使用)。 策略是一种函数,给定玩家的分数和对手的分数 score,返回当前玩家想要掷的骰子数量 转。每个策略函数每回合只能调用一次。不用担心 关于实施策略的细节。您将在以下方面开发它们 第 3 阶段。strategy0strategy1

游戏结束后,返回双方玩家的最终总分,其中 玩家 0 得分第一,玩家 1 得分第二。play

以下是一些提示:

  • 你应该使用你已经写好的函数!您将需要使用所有三个参数进行调用。take_turn
  • 每回合只能呼叫一次。take_turn
  • 执行除野猪以外的所有特殊规则。
  • 您可以通过调用来获取其他玩家的号码(0 或 1) 提供的功能。other
  • 您可以暂时忽略该函数的参数。您将使用 它在项目的第 2 阶段。sayplay

在编写任何代码之前,请解锁测试以验证您对问题的理解。

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

问题 5b (1 pt)

现在,实施野猪规则。当被调用并且它的参数是 时,则应该强加此规则。如果是,则应忽略此规则。 (这样,在解决 5b 后,5a 的测试用例仍将通过。playferal_hogsTrueferal_hogsFalse

野猪规则转载如下:

  • 野猪。如果您掷出的骰子数量与您在上一回合获得的分数正好相差 2 分(绝对差异),则该回合将获得 3 分的额外积分。将第一回合前的回合视为得分 0。在计算上一回合的得分数时,不要考虑任何先前的野猪奖金或猪交换(下一个规则)。

    例子
    • 示例 1:

      • 两位球员都从 0 开始。(0, 0)
      • 玩家 0 掷 3 个骰子并获得 7 分。(7, 0)
      • 玩家 1 掷 1 个骰子并获得 4 分。(7, 4)
      • 玩家 0 掷 5 个骰子并获得 10 分。5 与 7 相差 2,因此玩家 0 获得 3 的加成。(20, 4)
      • 玩家 1 掷 2 个骰子并获得 8 分。2 是 2 与 4 相差 2,因此玩家 1 获得 3 的奖金。(20, 15)
      • 玩家 0 掷 8 个骰子并获得 20 分。8 是 2 与 10 相差 10,因此玩家 0 获得 3 的奖励。(43, 15)
      • 玩家 1 掷 6 个骰子并获得 1 分。6 与 8 相差 2,因此玩家 1 获得 3 的奖金。(43, 19)
    • 示例 2:

      • 两位球员都从 0 开始。(0, 0)
      • 玩家 0 掷 2 个骰子并获得 3 分。2 是 2 与 0 相差 2,因此玩家 0 获得 3 的奖励。(6, 0)

 解锁时,注意这里坑爹的strat1:

s0->仍然指向s1(s0只是形参)

(之前我总把s0代入s0,难受)

---------------------------------------------------------------------
Question 5b > Suite 1 > Case 1
(cases remaining: 103)>>> import hog
>>> always_one = hog.make_test_dice(1)
>>> always_two = hog.make_test_dice(2)
>>> always_three = hog.make_test_dice(3)
>>> always = hog.always_roll
>>> # example 1
>>> def strat0(s0, s1):
...     if s0 == 0: return 3
...     if s0 == 7: return 5
...     return 8
>>> def strat1(s0, s1):
...     if s0 == 0: return 1
...     if s0 == 4: return 2
...     return 6
>>> s0, s1 = hog.play(
...   strat0, strat1, score0=0, score1=0, goal=21,
...   dice=hog.make_test_dice(2, 2, 3, 4, 2, 2, 2, 2, 2, 3, 5, 2, 2, 2, 2, 2, 2, 2, 6, 1))
>>> s0

1. s0=43(野猪*2)

(计算注意野猪的加分算额外加分,不算计算野猪加分的上回合加分)

s1=15(野猪*1)

2.dice()每次调用后,下一次初始值为上一次调用结尾的后一位

 

代码:

    add0=0add1=0while score0 <goal and score1 < goal:if who==0:get0=strategy0(score0,score1)  # 当前回合的次数if feral_hogs:if abs(get0-add0)==2:score0+=3add0=take_turn(get0,score1,dice)  # 当前回合的加分score0+=add0if is_swap(score0,score1):score0,score1=score1,score0else:get1 = strategy1(score1, score0)    # 当前回合的次数if feral_hogs:if abs(get1-add1)==2:score1+=3add1= take_turn(get1,score0,dice)   # 当前回合的加分score1+= add1if 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

 pass情况:

OK! All cases for Question 5b unlocked.Backup... 100% complete
Backup past deadline by 1242 days, 56 minutes, and 10 seconds
Backup successful for user: mxylms1210@163.comOK is up to date
PS D:\python文件\hog> python3 ok -q 05b
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests---------------------------------------------------------------------
Test summary103 test cases passed! No cases failed.Backup... 100% complete
Backup past deadline by 1242 days, 56 minutes, and 26 seconds
Backup successful for user: mxylms1210@163.com

第一阶段,总算是完结啦:

Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Scoring tests---------------------------------------------------------------------
Question 0Passed: 0Failed: 1
[k..........] 0.0% passed---------------------------------------------------------------------
Question 1Passed: 3Failed: 0
[ooooooooook] 100.0% passed---------------------------------------------------------------------
Question 2Passed: 1Failed: 0
[ooooooooook] 100.0% passed---------------------------------------------------------------------
Question 3Passed: 2Failed: 0
[ooooooooook] 100.0% passed---------------------------------------------------------------------
Question 4Passed: 1Failed: 0
[ooooooooook] 100.0% passed---------------------------------------------------------------------
Question 5aPassed: 3Failed: 0
[ooooooooook] 100.0% passed---------------------------------------------------------------------
Question 5bPassed: 2Failed: 0
[ooooooooook] 100.0% passed---------------------------------------------------------------------
Question 6Passed: 0Failed: 2
[k..........] 0.0% passed---------------------------------------------------------------------
Question 7 > Suite 2 > Case 1>>> from hog import play, always_roll, announce_highest, both
>>> from dice import make_test_dice
>>> # this might not technically be a possible game for the current rules, this shouldn't be relevant
>>> f0 = announce_highest(1) # Only announce Player 1 score gains
>>> f1 = f0(12, 0)
TypeError: 'NoneType' object is not callable# Error: expected# but got
#     Traceback (most recent call last):
#       ...
#     TypeError: 'NoneType' object is not callableRun only this test case with "python3 ok -q 07 --suite 2 --case 1"
---------------------------------------------------------------------
Question 7Passed: 0Failed: 1
[k..........] 0.0% passed---------------------------------------------------------------------
Question 8 > Suite 3 > Case 1>>> from hog import *
>>> hundred_range = range(1, 100)
>>> hundred_dice = make_test_dice(*hundred_range)
>>> averaged_hundred_dice = make_averaged(hundred_dice, 5*len(hundred_range))
>>> correct_average = sum(range(1, 100)) / len(hundred_range)
>>> averaged_hundred_dice()
TypeError: 'NoneType' object is not callable# Error: expected
#     50.0
# but got
#     Traceback (most recent call last):
#       ...
#     TypeError: 'NoneType' object is not callableRun only this test case with "python3 ok -q 08 --suite 3 --case 1"
---------------------------------------------------------------------
Question 8Passed: 0Failed: 2
[k..........] 0.0% passed---------------------------------------------------------------------
Question 9Passed: 0Failed: 2
[k..........] 0.0% passed---------------------------------------------------------------------
Question 10 > Suite 2 > Case 1>>> from hog import *
>>> bacon_strategy(44, 47, 0, 4)
6# Error: expected
#     0
# but got
#     6Run only this test case with "python3 ok -q 10 --suite 2 --case 1"
---------------------------------------------------------------------
Question 10Passed: 0Failed: 2
[k..........] 0.0% passed---------------------------------------------------------------------
Question 11 > Suite 2 > Case 1>>> from hog import *
>>> swap_strategy(47, 64, 3, 4)
6# Error: expected
#     0
# but got
#     6Run only this test case with "python3 ok -q 11 --suite 2 --case 1"
---------------------------------------------------------------------
Question 11Passed: 0Failed: 2
[k..........] 0.0% passed---------------------------------------------------------------------
Question 12Passed: 0Failed: 0
[k..........] 0.0% passed---------------------------------------------------------------------
Point breakdownQuestion 0: 0.0/0Question 1: 2.0/2Question 2: 1.0/1Question 3: 2.0/2Question 4: 2.0/2Question 5a: 3.0/3Question 5b: 1.0/1Question 6: 0.0/2Question 7: 0.0/3Question 8: 0.0/2Question 9: 0.0/2Question 10: 0.0/1Question 11: 0.0/2Question 12: 0.0/0Score:Total: 11.0Backup... 100% complete
Backup past deadline by 1242 days, 1 hour, 23 minutes, and 40 seconds
Backup successful for user: mxylms1210@163.comOK is up to date

(第二阶段)问题6(2分)

更新您的play函数,以便在最后调用注释函数 每一个转身。调用注释函数的返回值为您提供 评论功能,以调用下一轮。

例如,say(score0, score1)应该在第一个 依次它的返回值(另一个注释函数)应该在最后调用 的第二个转折点。每一个连续的回合,调用返回的函数 通过调用前一回合的评论功能

解锁遇到的坑点:(line3)

轮到参数者第二加分时,得到13,此时对手为12(两者交换)

line3:12 13

---------------------------------------------------------------------
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)

问题7(3分)

实现announce_highest函数,这是一个高阶函数, 返回一个注释函数。每当出现 某个玩家在一个回合中获得的点数比以往任何时候都多。例如,在一个示例中, announce_highest(1)及其返回值完全忽略参与人0, 打印有关参与人1的信息。为了计算增益,它必须比较 从最后一轮到这轮的得分,这是 由who参数指定。此函数还必须跟踪 这是迄今为止玩家获得的最高收益。

announce_highest宣布的方式是非常具体的,你的 实现应该匹配所提供的doctests。不要担心单数 而不是复数时宣布点收益;你应该简单地使用“点(s)”为 两个案子

Hint.提供给您的announce_lead_changes函数是一个示例, 如何使用评论功能跟踪信息。如果你被卡住了, 首先,请确保您了解announce_lead_changes如何工作。

注意:hog.py中both/announce_highest的文档测试可能描述 一个不可能按照规则发生的游戏这不应该是一个问题, 评论功能,因为它们不执行任何游戏规则。

Hint.如果你得到一个local variable [var] reference before assignment错误:

这是因为在Python中,通常不允许修改 父框架中定义的变量。而不是重新分配[var], 解释器认为你试图在当前的 frame.我们将在以后的讲座中学习如何解决这个问题, 这个问题不需要。

要解决这个问题,你有两个选择:

1)与其将[var]重新分配给它的新值,不如创建一个新变量, 保留新值。在以后的计算中使用这个新变量。

2)对于此问题,请通过不使用 所有的赋值语句而是将新值作为参数传递给 打电话给#1。

分析:由于形参无法直接赋值新值,

我们可以采用递归的方法,利用新函数重新赋值新的形参 

(anounce_highest()函数)

我们分为两个情况考虑:

一种是:当前分数-之前分数>最高增加数

                返回新函数的形参:

                        之前分数为:当前分数

                        最高增加数为:当前分数-之前分数

另一种是:当前分数-之前分数<最高增加数

                返回新函数的形参:

                        之前分数为:当前分数

                        最高增加数为:当前最高增加数

pass代码:


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 bumble(player1,player2):if who==1:current1=player2else:current1=player1if (current1-last_score) > running_high:print(str(current1-last_score)+" point(s)! That's the biggest gain yet for Player "+str(who))return announce_highest(who,current1,current1-last_score)return announce_highest(who,current1,running_high)return bumble

 pass情况:

OK is up to date
PS D:\python文件\hog> python3 ok -q 07
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests---------------------------------------------------------------------
Test summary5 test cases passed! No cases failed.Backup... 100% complete
Backup past deadline by 1242 days, 4 hours, 44 minutes, and 54 seconds
Backup successful for user: mxylms1210@163.comOK is up to date

问题9(2分)

实现max_scoring_num_rolls函数,该函数运行一个实验, 确定给出最大平均值的卷数(从1到10) 一个转身得分。 您的实现应该使用make_averaged和 roll_dice.

如果两个掷骰数相等,则返回 更低的数字。例如,如果3和6都达到了最高平均分, 返回3.

在编写任何代码之前,解锁测试以验证您对问题的理解。

python3 ok -q 09 -u

完成解锁后,开始实施解决方案。您可以通过以下方式检查您的正确性:

python3 ok -q 09

要在随机骰子上运行此实验,请使用 run_experiments选项:

python3 hog.py -r

运行实验对于本项目的剩余部分,您可以更改 run_experiments1average_win_rate1if False:1if True:1always_roll(8)1always_roll(6)1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1undefined1#1 通过调用 undefined,你可以评估各种猪策略。比如说, 将第一个undefined更改为undefined,以评估 undefined对比undefined的基线策略。

某些实验可能需要一分钟才能运行。你总是可以减少 调用make_averaged中的试验次数以加快实验速度。

分析:此题函数需要返回最小的达到最大平均值的骰子个数

利用roll_dice()实现多个骰子同时使用(但返回的是int)

利用make_average()实现计算平均值

        形参:骰子的函数,和抛骰子的次数

        (注意:需要把roll_dice利用lambda表达式转换为函数)

pass代码: 

def max_scoring_num_rolls(dice=six_sided, trials_count=1000):"""Return the number of dice (1 to 10) that gives the highest average turnscore by calling roll_dice with the provided DICE over TRIALS_COUNT times.Assume that the dice always return positive outcomes.>>> dice = make_test_dice(1, 6)>>> max_scoring_num_rolls(dice)1"""# BEGIN PROBLEM 9"*** YOUR CODE HERE ***"aver={}max1=-1.0for i in range(1,11):av=make_averaged(lambda :roll_dice(i,dice),trials_count)aver[i-1]=av()if max1 < aver[i-1]:max1=aver[i-1]for i in range(0,10):if max1== aver[i]:return i+1

pass情况:

PS D:\python文件\hog> python3 ok -q 09
=====================================================================
Assignment: Project 1: Hog
OK, version v1.18.1
=====================================================================~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests---------------------------------------------------------------------
Test summary8 test cases passed! No cases failed.Backup... 100% complete

最后的final策略,就之后再实现吧(毕竟没有学分...hahaha)

以下为完整代码:

"""CS 61A Presents The Game of Hog."""from dice import six_sided, four_sided, make_test_dice
from ucb import main, trace, interactGOAL_SCORE = 100  # The goal of Hog is to score 100 points.######################
# Phase 1: Simulator #
######################def roll_dice(num_rolls, dice=six_sided):"""Simulate rolling the DICE exactly NUM_ROLLS > 0 times. Return the sum ofthe outcomes unless any of the outcomes is 1. In that case, return 1.num_rolls:  The number of dice rolls that will be made.dice:       A function that simulates a single dice roll outcome."""# These assert statements ensure that num_rolls is a positive integer.assert type(num_rolls) == int, 'num_rolls must be an integer.'assert num_rolls > 0, 'Must roll at least once.'# BEGIN PROBLEM 1"*** YOUR CODE HERE ***"ret = 0pig_out =Falsefor _ in range(num_rolls):score = dice()if score == 1:pig_out =Truecontinueret += scorereturn 1 if pig_out else ret# END PROBLEM 1def free_bacon(score):"""Return the points scored from rolling 0 dice (Free Bacon).score:  The opponent's current score."""assert score < 100, 'The game should be over.'# BEGIN PROBLEM 2"*** YOUR CODE HERE ***"return 10 - score%10+ int(score / 10)# END PROBLEM 2def take_turn(num_rolls, opponent_score, dice=six_sided):"""Simulate a turn rolling NUM_ROLLS dice, which may be 0 (Free Bacon).Return the points scored for the turn by the current player.num_rolls:       The number of dice rolls that will be made.opponent_score:  The total score of the opponent.dice:            A function that simulates a single dice roll outcome."""# Leave these assert statements here; they help check for errors.assert type(num_rolls) == int, 'num_rolls must be an integer.'assert num_rolls >= 0, 'Cannot roll a negative number of dice in take_turn.'assert num_rolls <= 10, 'Cannot roll more than 10 dice.'assert opponent_score < 100, 'The game should be over.'# BEGIN PROBLEM 3"*** YOUR CODE HERE ***"if num_rolls>0:return roll_dice(num_rolls,dice)else:return free_bacon(opponent_score)# END PROBLEM 3def is_swap(player_score, opponent_score):"""Return whether the two scores should be swapped"""# BEGIN PROBLEM 4"*** YOUR CODE HERE ***"if abs(player_score%10-opponent_score%10)==(opponent_score//10)%10:return Trueelse:return False# END PROBLEM 4def other(who):"""Return the other player, for a player WHO numbered 0 or 1.>>> other(0)1>>> other(1)0"""return 1 - whodef silence(score0, score1):"""Announce nothing (see Phase 2)."""return silencedef 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 ***"add0=0add1=0while score0 <goal and score1 < goal:if who==0:get0=strategy0(score0,score1)  # 当前回合的次数if feral_hogs:if abs(get0-add0)==2:score0+=3add0=take_turn(get0,score1,dice)  # 当前回合的加分score0+=add0if is_swap(score0,score1):score0,score1=score1,score0say = say(score0, score1)else:get1 = strategy1(score1, score0)    # 当前回合的次数if feral_hogs:if abs(get1-add1)==2:score1+=3add1= take_turn(get1,score0,dice)   # 当前回合的加分score1+= add1if is_swap(score1,score0):score0,score1=score1,score0say = say(score0, score1)who=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#######################
# Phase 2: Commentary #
#######################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_scoresdef 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_lead_changes(leader)return saydef both(f, g):"""Return a commentary function that says what f says, then what g says.NOTE: the following game is not possible under the rules, it's justan example for the sake of the doctest>>> 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"""def say(score0, score1):return both(f(score0, score1), g(score0, score1))return saydef 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 bumble(player1,player2):if who==1:current1=player2else:current1=player1if (current1-last_score) > running_high:print(str(current1-last_score)+" point(s)! That's the biggest gain yet for Player "+str(who))return announce_highest(who,current1,current1-last_score)return announce_highest(who,current1,running_high)return bumble# END PROBLEM 7#######################
# Phase 3: Strategies #
#######################def always_roll(n):"""Return a strategy that always rolls N dice.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.>>> strategy = always_roll(5)>>> strategy(0, 0)5>>> strategy(99, 99)5"""def strategy(score, opponent_score):return nreturn strategydef make_averaged(original_function, trials_count=1000):"""Return a function that returns the average value of ORIGINAL_FUNCTION when called.To implement this function, you will have to use *args syntax, a new Pythonfeature introduced in this project.  See the project description.>>> dice = make_test_dice(4, 2, 5, 1)>>> averaged_dice = make_averaged(dice, 1000)>>> averaged_dice()3.0"""# BEGIN PROBLEM 8"*** YOUR CODE HERE ***"def average1():sum=0.0for i in range(1,trials_count+1):sum+=original_function()return sum/trials_countreturn average1# END PROBLEM 8def max_scoring_num_rolls(dice=six_sided, trials_count=1000):"""Return the number of dice (1 to 10) that gives the highest average turnscore by calling roll_dice with the provided DICE over TRIALS_COUNT times.Assume that the dice always return positive outcomes.>>> dice = make_test_dice(1, 6)>>> max_scoring_num_rolls(dice)1"""# BEGIN PROBLEM 9"*** YOUR CODE HERE ***"aver={}max1=-1.0for i in range(1,11):av=make_averaged(lambda :roll_dice(i,dice),trials_count)aver[i-1]=av()if max1 < aver[i-1]:max1=aver[i-1]for i in range(0,10):if max1== aver[i]:return i+1# END PROBLEM 9def winner(strategy0, strategy1):"""Return 0 if strategy0 wins against strategy1, and 1 otherwise."""score0, score1 = play(strategy0, strategy1)if score0 > score1:return 0else:return 1def average_win_rate(strategy, baseline=always_roll(6)):"""Return the average win rate of STRATEGY against BASELINE. Averages thewinrate when starting the game as player 0 and as player 1."""win_rate_as_player_0 = 1 - make_averaged(winner)(strategy, baseline)win_rate_as_player_1 = make_averaged(winner)(baseline, strategy)return (win_rate_as_player_0 + win_rate_as_player_1) / 2def run_experiments():"""Run a series of strategy experiments and report results."""if True:  # Change to False when done finding max_scoring_num_rollssix_sided_max = max_scoring_num_rolls(six_sided)print('Max scoring num rolls for six-sided dice:', six_sided_max)if False:  # Change to True to test always_roll(8)print('always_roll(8) win rate:', average_win_rate(always_roll(8)))if False:  # Change to True to test bacon_strategyprint('bacon_strategy win rate:', average_win_rate(bacon_strategy))if False:  # Change to True to test swap_strategyprint('swap_strategy win rate:', average_win_rate(swap_strategy))if False:  # Change to True to test final_strategyprint('final_strategy win rate:', average_win_rate(final_strategy))"*** You may add additional experiments as you wish ***"def bacon_strategy(score, opponent_score, cutoff=8, num_rolls=6):"""This strategy rolls 0 dice if that gives at least CUTOFF points, androlls NUM_ROLLS otherwise."""# BEGIN PROBLEM 10if free_bacon(opponent_score)>=cutoff:return 0else:return num_rolls# END PROBLEM 10def swap_strategy(score, opponent_score, cutoff=8, num_rolls=6):"""This strategy rolls 0 dice when it triggers a beneficial swap. It alsorolls 0 dice if it gives at least CUTOFF points and does not trigger anon-beneficial swap. Otherwise, it rolls NUM_ROLLS."""# BEGIN PROBLEM 11bacon=free_bacon(opponent_score)if is_swap(bacon+score,opponent_score):if bacon+score>opponent_score:return num_rollselse:return 0if bacon >= cutoff:return 0else:return num_rolls# END PROBLEM 11def final_strategy(score, opponent_score):"""Write a brief description of your final strategy.*** YOUR DESCRIPTION HERE ***"""# BEGIN PROBLEM 12return 6  # Replace this statement# END PROBLEM 12##########################
# Command Line Interface #
########################### NOTE: Functions in this section do not need to be changed. They use features
# of Python not yet covered in the course.@main
def run(*args):"""Read in the command-line argument and calls corresponding functions.This function uses Python syntax/techniques not yet covered in this course."""import argparseparser = argparse.ArgumentParser(description="Play Hog")parser.add_argument('--run_experiments', '-r', action='store_true',help='Runs strategy experiments')args = parser.parse_args()if args.run_experiments:run_experiments()

pass情况:

---------------------------------------------------------------------
Point breakdownQuestion 0: 0.0/0Question 1: 2.0/2Question 2: 1.0/1Question 3: 2.0/2Question 4: 2.0/2Question 5a: 3.0/3Question 5b: 1.0/1Question 6: 2.0/2Question 7: 3.0/3Question 8: 0.0/2Question 9: 2.0/2Question 10: 1.0/1Question 11: 2.0/2Question 12: 0.0/0Score:Total: 21.0Backup... 0.0% completeOK is up to date

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/193976.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

树莓派多串口通信

树莓派多串口通信 串口配置串口通信函数分析串口通信示例代码 参考博文1&#xff1a;树莓派 4 UART 多串口配置通信参考博文2&#xff1a;树莓派wiringPi库详解关于树莓派相关其他环境配置可参考&#xff1a;快速上手树莓派关于wiringPi库初始化与IO口开发可参考&#xff1a;树…

调优--学习笔记

1&#xff0c;Presto调优 数据存储格式 1&#xff09;合理设置分区 与Hive类似&#xff0c;Presto会根据元信息读取分区数据&#xff0c;合理的分区能减少Presto数据读取量&#xff0c;提升查询性能。 2&#xff09;使用列式存储 Presto对ORC文件读取做了特定优化&#xff0c…

Qt OpenCV 学习(一):环境搭建

对应版本 Qt 5.15.2OpenCV 3.4.9MinGW 8.1.0 32-bit 1. OpenCV 下载 确保安装 Qt 时勾选了 MinGW 编译器 本文使用 MinGW 编译好的 OpenCV 库&#xff0c;无需自行编译 确保下载的 MinGW 和上述安装 Qt 时勾选的 MinGW 编译器位数一致&#xff0c;此处均为 x86/32-bit下载地址…

《微信小程序开发从入门到实战》学习四十

4.2 云开发JSON数据库 4.2.11 更新数据 使用数据库API更新数据有两种方法&#xff1a;一.将记录局部更新的update方法&#xff1b;二.以替换的方式更新记录的set方法 update方法可以局部更新一个记录或一个集合的多个记录&#xff0c;更新时只有指定字段更新&#xff0c;其他…

智能诊疗体验:整合AI技术的互联网医院小程序开发

在科技化的趋势下&#xff0c;互联网医院小程序的开发变得愈发重要&#xff0c;尤其是通过整合人工智能&#xff08;AI&#xff09;技术&#xff0c;进一步提升了就医的效率。 一、引言 互联网医院小程序其开发目标是提高医疗服务的效率&#xff0c;同时也也提升了用户的就医…

23种设计模式之C++实践(一)

23种设计模式之C++实践 1. 简介2. 基础知识3. 设计模式(一)创建型模式1. 单例模式——确保对象的唯一性1.2 饿汉式单例模式1.3 懒汉式单例模式比较IoDH单例模式总结2. 简单工厂模式——集中式工厂的实现简单工厂模式总结3. 工厂方法模式——多态工厂的实现工厂方法模式总结4.…

【像素画板】游戏地图编辑器-uniapp项目开发流程详解

嘿&#xff0c;用过像素画板没有哦&#xff0c;相信喜欢绘画的小朋友会对它感兴趣呢&#xff0c;用来绘制像素画非常好看&#xff0c;有没有发现&#xff0c;它是可以用来绘制游戏地图的&#xff0c;是不是很好奇&#xff0c;来一起看看吧。 像素画板&#xff0c;也叫像素画的绘…

c语言-归并排序

目录 1、归并排序基本思想 2、归并排序的实现&#xff08;递归法&#xff09; 2.1 代码实现递归法归并排序 3、归并排序的实现&#xff08;非递归法&#xff09; 3.1 修正边界问题 3.2 代码实现非递归法归并排序 结语&#xff1a; 前言&#xff1a; 归并排序是一种把数…

Python---格式化输出与%百分号----涉及转义符 \ 反斜杠的使用

相关链接Python--格式化输出中的转义符号----\t 制表符&#xff08;空格的&#xff09;和\n&#xff08;换行的&#xff09;_唯元素的博客-CSDN博客 Python---字符串&#xff08;用单、双引号、 三单/双引号定义。反斜杠 \ 转义&#xff0c;单在双内/双在单内 &#xff09;-CS…

力扣 --- 最后一个单词的长度

题目描述&#xff1a; 给你一个字符串 s&#xff0c;由若干单词组成&#xff0c;单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。 单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。 示例 1&#xff1a; 输入&#xff1a;s "Hello World&…

运维02:Linux

Linux安装 VMWare安装&#xff1a;夸克网盘分享&#xff08;提取码&#xff1a;refg&#xff09; CentOS安装&#xff1a;Index of /centos/7.9.2009/isos/x86_64/ Xshell安装&#xff1a;百度网盘 请输入提取码&#xff08;提取码&#xff1a;juau&#xff09; 环境准备 1、…

在Windows 11中,把iPhone照片和视频导出来又快又简单,无需第三方软件

如果你想将照片和视频从iPhone传输到Windows 11 PC&#xff0c;最快、最简单的方法是插入手机并执行自动导入。以下是操作方法。 如何将照片和视频从iPhone导入Windows 如果你用USB数据线将iPhone插入Windows PC&#xff0c;Windows 11可以像标准数码相机一样连接到它&#x…

react之封装有无Token(路由权限控制)的高阶组件

TOC 前景 有些路由页面内的内容信息比较敏感&#xff0c;如果用户没有经过登录获取到有效Token&#xff0c;是没有权限跳转的&#xff0c;根据Token的有 无控制当前路由是否可以跳转就是路由的权限控制 技术方案 实现步骤 1.在 components 目录中&#xff0c;创建 AuthRoute/in…

solidity实现ERC721代币标准发布NFT

文章目录 1、非同质化货币&#xff08;NFT&#xff09;- 维基百科2、IERC1653、IERC7214、IERC721Receiver5、IERC721Metadata6、ERC7217、ERC721 NFT 的实现8、编译部署 1、非同质化货币&#xff08;NFT&#xff09;- 维基百科 非同质化代币&#xff08;英语&#xff1a;Non-F…

Elasticsearch:什么是大语言模型(LLM)?

大语言模型定义 大语言模型 (LLM) 是一种深度学习算法&#xff0c;可以执行各种自然语言处理 (natural language processing - NLP) 任务。 大型语言模型使用 Transformer 模型&#xff0c;并使用大量数据集进行训练 —— 因此规模很大。 这使他们能够识别、翻译、预测或生成文…

时间复杂度为O (nlogn)的排序算法

归并排序 归并排序遵循分治的思想&#xff1a;将原问题分解为几个规模较小但类似于原问题的子问题&#xff0c;递归地求解这些子问题&#xff0c;然后合并这些子问题的解来建立原问题的解&#xff0c;归并排序的步骤如下&#xff1a; 划分&#xff1a;分解待排序的 n 个元素的…

【c】求一组数据的最大值和第二大的值

我们可以创建数组&#xff0c;利用冒泡排序法把数组进行排序&#xff0c;但是当元素过多时候循环可能过多导致循环超限 所以我们可以换种其他方法&#xff0c;代码附上 #include<stdio.h> int main() {int n,i;puts("输入这组数据的个数");scanf("%d&qu…

进行主从复制时出现的异常FATAL CONFIG FILE ERROR (Redis 6.2.6)Reading the configuration file

错误如下所示&#xff1a; FATAL CONFIG FILE ERROR (Redis 6.2.6) Reading the configuration file, at line 1 >>> include/myredis/redis.conf Bad directive or wrong number of arguments出现错误的原因是.conf文件中命令之间缺少空格&#xff0c;如下所示&…

QML中常见布局方法

目录 引言常见方法锚定&#xff08;anchors&#xff09;定位器Row、ColumnGridFlow 布局管理器RowLayout、ColumnLayoutGridLayoutStackLayout 总结 引言 UI界面由诸多元素构成&#xff0c;如Label、Button、Input等等&#xff0c;各种元素需要按照一定规律进行排布才能提高界…

Prime 2.0

信息收集 # Nmap 7.94 scan initiated Thu Nov 23 20:09:06 2023 as: nmap -sn -oN live.nmap 192.168.182.0/24 Nmap scan report for 192.168.182.1 Host is up (0.00018s latency). MAC Address: 00:50:56:C0:00:08 (VMware) Nmap scan report for 192.168.182.2 Host is u…