ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[2021 Spring] CS61A Project 1: The Game of Hog (Phase 3)

2021-06-15 23:03:04  阅读:1630  来源: 互联网

标签:Hog num dice Spring CS61A score PROBLEM strategy rolls


项目说明: https://inst.eecs.berkeley.edu/~cs61a/sp21/proj/hog/#phase-3-strategies
Phase1: https://www.cnblogs.com/ikventure/p/14815119.html
Phase2: https://www.cnblogs.com/ikventure/p/14885436.html

目录

Phase 3: Strategies

In the third phase, you will experiment with ways to improve upon the basic strategy of always rolling a fixed number of dice. First, you need to develop some tools to evaluate strategies.

Problem 8


unlock时,注意Sow Sad规则。

def 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 Python
    feature introduced in this project.  See the project description.

    >>> dice = make_test_dice(4, 2, 5, 1)
    >>> averaged_dice = make_averaged(roll_dice, 1000)
    >>> averaged_dice(1, dice)
    3.0
    """
    # BEGIN PROBLEM 8
    "*** YOUR CODE HERE ***"
    def f(*args):
        result = 0
        for i in range(trials_count):
            result += original_function(*args)
        return result / trials_count
    return f
    # END PROBLEM 8

Problem 9


使用字典avgs存放每轮均值和num_rolls。

def max_scoring_num_rolls(dice=six_sided, trials_count=1000):
    """Return the number of dice (1 to 10) that gives the highest average turn score
    by calling roll_dice with the provided DICE a total of 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 ***"
    avg = 0
    avgs = {}
    for i in range(1, 11):
        avg = make_averaged(roll_dice, trials_count)(i, dice)
        if avg not in avgs:
            avgs[avg] = i
        else:
            if i < avgs[avg]:
                avgs[avg] = i
    return avgs[max(avgs)]
    # END PROBLEM 9

Problem 10


用之前定义的piggypoints,大于cutoff就return 0,否则返回num_rolls。

def piggypoints_strategy(score, opponent_score, cutoff=8, num_rolls=6):
    """This strategy rolls 0 dice if that gives at least CUTOFF points, and
    rolls NUM_ROLLS otherwise.
    """
    # BEGIN PROBLEM 10
    return 0 if piggy_points(opponent_score) >= cutoff else num_rolls  # Replace this statement
    # END PROBLEM 10

Problem 11


如果0 dice后触发再来一轮,0 dice;如果达到cutoff不再来一轮也选择0 dice;否则,num_rolls。注意使用piggypoints_strategy和more_boar比较方便。

def more_boar_strategy(score, opponent_score, cutoff=8, num_rolls=6):
    """This strategy rolls 0 dice when it triggers an extra turn. It also
    rolls 0 dice if it gives at least CUTOFF points and does not give an extra turn.
    Otherwise, it rolls NUM_ROLLS.
    """
    # BEGIN PROBLEM 11
    return 0 if more_boar(score + piggy_points(opponent_score), opponent_score) \
        else piggypoints_strategy(score, opponent_score, cutoff, num_rolls)
        # Replace this statement
    # END PROBLEM 11

Optional: Problem 12


设计策略让获胜概率更高(由于连接不上服务器,只能测试代码完成度,不能测试胜率)。

def final_strategy(score, opponent_score):
    """Write a brief description of your final strategy.

    *** YOUR DESCRIPTION HERE ***
    """
    # BEGIN PROBLEM 12
    if score <= 80:
        return more_boar_strategy(score, opponent_score, cutoff=8, num_rolls=6)
    elif score <= 90 and score > opponent_score:
        return piggypoints_strategy(score, opponent_score, cutoff=7, num_rolls=1)
    else:
        return 2
    # Replace this statement
    # END PROBLEM 12

project 完成


标签:Hog,num,dice,Spring,CS61A,score,PROBLEM,strategy,rolls
来源: https://www.cnblogs.com/ikventure/p/14887555.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有