Example #1
0
 def test_take_turn(self):
     """take a turn using different opponent scores and dice"""
     FACE_VALUE = 4
     test_dice = make_test_dice(FACE_VALUE)  # always rolls same value
     # test the free bacon case
     self.assertEqual(10, take_turn(0, 29, dice=test_dice))
     self.assertEqual(8, take_turn(0, 75, dice=test_dice))
     self.assertEqual(1, take_turn(0, 0, dice=test_dice))
     # for rolls > 0 opponent score should be irrelevant
     self.assertEqual(FACE_VALUE, take_turn(1, 0, dice=test_dice))
     self.assertEqual(3 * FACE_VALUE, take_turn(3, 0, dice=test_dice))
     self.assertEqual(3 * FACE_VALUE, take_turn(3, 23, dice=test_dice))
     self.assertEqual(10 * FACE_VALUE, take_turn(10, 23, dice=test_dice))
     # this case always gets a score of 1
     test_dice = make_test_dice(1, 1, 1, 1, 1)
     self.assertEqual(1, take_turn(1, 10, dice=test_dice))
     self.assertEqual(1, take_turn(2, 10, dice=test_dice))
     self.assertEqual(1, take_turn(3, 10, dice=test_dice))
     self.assertEqual(1, take_turn(9, 10, dice=test_dice))
     self.assertEqual(1, take_turn(10, 10, dice=test_dice))
     # a test die with 2 values to verify student code uses it
     FACE1 = 2
     FACE2 = 5
     test_dice = make_test_dice(FACE1, FACE2)
     self.assertEqual(FACE1, take_turn(1, 10, dice=test_dice))
     self.assertEqual(FACE2, take_turn(1, 10, dice=test_dice))
     self.assertEqual(FACE1 + FACE2, take_turn(2, 10, dice=test_dice))
     self.assertEqual(2 * FACE1 + FACE2, take_turn(3, 10, dice=test_dice))
Example #2
0
def problem4(grades):
    """Test play using fixed dice."""
    always = hog.always_roll
    hog.four_sided = make_test_dice(1)
    hog.six_sided = make_test_dice(3)

    test_suite = [
        ((always(5), always(5)), (92, 106)),
        ((always(2), always(2)), (17, 102)),
        ((always(2), always(10)), (19, 120)),
        ((always(0), always(0)), (91, 103)),  # always roll 0
        ((always(0), always(2)), (106, 56))
    ]

    try:
        failure = False
        if check_func(hog.play, test_suite):
            failure = True
    finally:
        # Revert dice
        hog.four_sided = four_sided
        hog.six_sided = six_sided
    print('Note: Not all tests have been released for problem4.',
          'Submit your project to the actual autograder to get more results!',
          sep='\n',
          end='\n')
    return failure
Example #3
0
File: hog.py Project: sinistrum/Hog
def take_turn_test():
    """Test the roll_dice and take_turn functions using test dice."""
    print('-- Testing roll_dice with deterministic test dice --')
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(2, dice) == 10, 'First two rolls total 10'
    
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(3, dice) == 1, 'Third roll is a 1'
    
    dice = make_test_dice(1, 2, 3)
    assert roll_dice(3, dice) == 1, 'First roll is a 1'
    
    print('-- Testing take_turn --')
    dice = make_test_dice(4, 6, 1)
    assert take_turn(2, 0, dice) == 10, 'First two rolls total 10'
    
    dice = make_test_dice(4, 6, 1)
    assert take_turn(3, 20, dice) == 1, 'Third roll is a 1'
    
    assert take_turn(0, 34) == 4, 'Opponent score 10s digit is 3'
    assert take_turn(0, 71) == 8, 'Opponent score 10s digit is 7'
    assert take_turn(0,  7) == 1, 'Opponont score 10s digit is 0'
    
    '*** You may add more tests here if you wish ***'
    
    print('Tests for roll_dice and take_turn passed.')
def take_turn_test():
    """Test the roll_dice and take_turn functions using test dice."""
    print('-- Testing roll_dice with deterministic test dice --')
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(2, dice) == 10, 'First two rolls total 10'

    dice = make_test_dice(4, 6, 1)
    assert roll_dice(3, dice) == 1, 'Third roll is a 1'

    dice = make_test_dice(1, 2, 3)
    assert roll_dice(3, dice) == 1, 'First roll is a 1'

    print('-- Testing take_turn --')
    dice = make_test_dice(4, 6, 1)
    assert take_turn(2, 0, dice) == 10, 'First two rolls total 10'

    dice = make_test_dice(4, 6, 1)
    assert take_turn(3, 20, dice) == 1, 'Third roll is a 1'

    assert take_turn(0, 34) == 4, 'Opponent score 10s digit is 3'
    assert take_turn(0, 71) == 8, 'Opponent score 10s digit is 7'
    assert take_turn(0,  7) == 1, 'Opponont score 10s digit is 0'

    '*** You may add more tests here if you wish ***'

    print('Tests for roll_dice and take_turn passed.')
Example #5
0
def problem2(grades):
    """Test take_turn."""
    test_suite1 = [((2,  0, make_test_dice(4, 6, 1)), 10),
                   ((3, 20, make_test_dice(4, 6, 1)),  1),
                   ((2,  0, make_test_dice(6)),       12),
                   ((0, 34),                           5),# Free bacon
                   ((0, 71),                           8),
                   ((0,  7),                           8)]
    test_suite2 = [((0, 99),                          10),
                   ((0,  0),                           1),
                   ((0,  50),                          6)]
    if check_func(hog.take_turn, test_suite1):
        return True
    if check_func(hog.take_turn, test_suite2):
        return True
Example #6
0
def problem2(grades):
    """Test take_turn."""
    test_suite1 = [
        ((2, 0, make_test_dice(4, 6, 1)), 10),
        ((3, 20, make_test_dice(4, 6, 1)), 1),
        ((2, 0, make_test_dice(6)), 12),
        ((0, 34), 5),  # Free bacon
        ((0, 71), 8),
        ((0, 7), 8)
    ]
    test_suite2 = [((0, 99), 10), ((0, 0), 1), ((0, 50), 6)]
    if check_func(hog.take_turn, test_suite1):
        return True
    if check_func(hog.take_turn, test_suite2):
        return True
Example #7
0
 def test_roll_dice_with_one(self):
     """Test roll_dice() when a 1 appears on at least one die"""
     test_dice = make_test_dice(1, 5, 1, 3, 1, 2, 3, 4, 5, 6)
     self.assertEqual(1, roll_dice(1, dice=test_dice))
     self.assertEqual(1, roll_dice(3, dice=test_dice))
     self.assertEqual(1, roll_dice(5, dice=test_dice))
     self.assertEqual(1, roll_dice(10, dice=test_dice))
Example #8
0
 def test_roll_dice(self):
     """roll_dice() should return sum of several 'rolls' of dice"""
     test_dice = make_test_dice(6, 5, 2, 3, 4, 5, 6, 6, 6, 6)
     self.assertEqual(6, roll_dice(1, dice=test_dice))
     self.assertEqual(7, roll_dice(2, dice=test_dice))
     self.assertEqual(12, roll_dice(3, dice=test_dice))
     self.assertEqual(24, roll_dice(4, dice=test_dice))
     self.assertEqual(49, roll_dice(10, dice=test_dice))
Example #9
0
def take_turn_test():
    # Test the roll_dice and take_turn functions using test dice.
    
    print('Testing roll_dice function with deterministic test dice...')
    
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(2, dice) == 10, 'First two rolls total 10'
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(3, dice) == 1, 'Third roll is a 1'
    dice = make_test_dice(1, 2, 3)
    assert roll_dice(3, dice) == 1, 'First roll is a 1'
    print('Testing Turn Taking Functions..')
    dice = make_test_dice(4, 6, 1)
    assert take_turn(2, 0, dice) == 10, 'First two rolls total 10'
    dice = make_test_dice(4, 6, 1)
    assert take_turn(3, 20, dice) == 1, 'Third roll is a 1'
    assert take_turn(0, 34) == 4, 'Opponent score 10s digit is 3'
    assert take_turn(0, 71) == 8, 'Opponent score 10s digit is 7'
    assert take_turn(0,  7) == 1, 'Opponont score 10s digit is 0'
    print('Tests for roll_dice and take_turn passed.')
Example #10
0
def problem5(grades):
    """Test make_averaged."""
    # hundred_dice cycle from 1 to 99 repeatedly
    hundred_range = range(1, 100)
    hundred_dice = make_test_dice(*hundred_range)
    averaged_hundred_dice = test_eval(hog.make_averaged,
                                      (hundred_dice, 5 * len(hundred_range)))
    correct_average = sum(range(1, 100)) / len(hundred_range)

    test_suite = [((), correct_average)] * 2

    if check_doctest('make_averaged', hog):
        return True
    if check_func(averaged_hundred_dice, test_suite):
        return True
Example #11
0
def problem5(grades):
    """Test make_averaged."""
    # hundred_dice cycle from 1 to 99 repeatedly
    hundred_range = range(1, 100)
    hundred_dice = make_test_dice(*hundred_range)
    averaged_hundred_dice = test_eval(hog.make_averaged,
                                      (hundred_dice, 5 * len(hundred_range)))
    correct_average = sum(range(1, 100)) / len(hundred_range)

    test_suite = [((), correct_average)] * 2

    if check_doctest('make_averaged', hog):
        return True
    if check_func(averaged_hundred_dice, test_suite):
        return True
Example #12
0
def problem4(grades):
    """Test play using fixed dice."""
    always = hog.always_roll
    hog.four_sided = make_test_dice(1)
    hog.six_sided = make_test_dice(3)

    test_suite = [((always(5),  always(5)), (92, 106)),
                  ((always(2),  always(2)), (17, 102)),
                  ((always(2), always(10)), (19, 120)),
                  ((always(0),  always(0)), (91, 103)), # always roll 0
                  ((always(0),  always(2)), (106, 56))]

    try:
        failure = False
        if check_func(hog.play, test_suite):
            failure = True
    finally:
        # Revert dice
        hog.four_sided = four_sided
        hog.six_sided = six_sided
    print('Note: Not all tests have been released for problem4.',
          'Submit your project to the actual autograder to get more results!',
          sep='\n', end='\n')
    return failure
Example #13
0
def take_turn_test():
    """Test the roll_dice and take_turn functions using test dice."""
    print('-- Testing roll_dice with deterministic test dice --')
    
    print('-test1-')
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(2, dice) == 10, 'First two rolls total 10'
    
    print('-test2-')
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(3, dice) == 1, 'Third roll is a 1'
    
    print('-test3-')
    dice = make_test_dice(1, 2, 3)
    assert roll_dice(3, dice) == 1, 'First roll is a 1'

    print('-- Testing take_turn --')

    print('-test1-')
    dice = make_test_dice(4, 6, 1)
    assert take_turn(2, 0, dice) == 10, 'First two rolls total 10'
    
    print('-test2-')
    dice = make_test_dice(4, 6, 1)
    assert take_turn(3, 20, dice) == 1, 'Third roll is a 1'

 
    print('-- Testing Free Bacon rule --')
    assert take_turn(0, 34) == 4, 'Opponent score 10s digit is 3'
    assert take_turn(0, 71) == 8, 'Opponent score 10s digit is 7'
    assert take_turn(0,  7) == 1, 'Opponent score 10s digit is 0'

    print('-- Testing Touchdown rule --')
    dice = make_test_dice(6)
    assert take_turn(1, 0, dice) == 7, 'Original score was 6'
    assert take_turn(2, 0, dice) == 14, 'Original score was 12'
    assert take_turn(0, 50, dice) == 7, 'Original score was 6'

    print('-- Testing 49ers rule --')
    dice = make_test_dice(1)
    assert roll_dice(3, dice, ones_lose=False) == 3, '49ers rule in effect'
    print('test2', take_turn(10, 0, dice, ones_lose=False))
    assert take_turn(10, 0, dice, ones_lose=False) == 10, '49ers rule in effect'
    assert take_turn(6, 0, dice, ones_lose=False) == 7, '49ers and Touchdown rule'

    '*** You may add more tests here if you wish ***'

    print('Tests for roll_dice and take_turn passed.')
Example #14
0
def problem1(grades):
    """Test roll_dice."""
    counted_dice = make_test_dice(4, 1, 2)
    test_suite1 = [((2, make_test_dice(4, 6, 1)), 10),
                   ((3, make_test_dice(4, 6, 1)), 1),
                   ((3, make_test_dice(1, 2, 3)), 1), ((3, counted_dice), 1),
                   ((1, counted_dice), 4)]
    test_suite2 = [((5, make_test_dice(4, 2, 3, 3, 4, 1)), 16),
                   ((2, make_test_dice(1)), 1)]

    if check_func(hog.roll_dice, test_suite1):
        return True
    if check_func(hog.roll_dice, test_suite2):
        return True
Example #15
0
def problem1(grades):
    """Test roll_dice."""
    counted_dice = make_test_dice(4, 1, 2)
    test_suite1 = [((2, make_test_dice(4, 6, 1)),           10),
                   ((3, make_test_dice(4, 6, 1)),            1),
                   ((3, make_test_dice(1, 2, 3)),            1),
                   ((3, counted_dice),                       1),
                   ((1, counted_dice),                       2)]
    test_suite2 = [((5, make_test_dice(4, 2, 3, 3, 4, 1)),  16),
                   ((2, make_test_dice(1)),                  1)]

    if check_func(hog.roll_dice, test_suite1):
        return True
    if check_func(hog.roll_dice, test_suite2):
        return True
Example #16
0
def take_turn_test():
    """Test the roll_dice and take_turn functions using test dice."""
    print('-- Testing roll_dice --')
    dice = make_test_dice(4, 6, 1)
    assert roll_dice(2, dice) == 10, 'First two rolls total 10'

    dice = make_test_dice(4, 6, 1)
    assert roll_dice(3, dice) == 1, 'Third roll is a 1'

    dice = make_test_dice(1, 2, 3)
    assert roll_dice(3, dice) == 1, 'First roll is a 1'

    print('-- Testing take_turn --')
    dice = make_test_dice(4, 6, 1)
    assert take_turn(2, 0, dice) == 10, 'First two rolls total 10'

    dice = make_test_dice(4, 6, 1)
    assert (take_turn(3, 20, dice) == 1), 'Third roll is a 1'

    print('---- Testing Free Bacon rule ----')
    assert take_turn(0, 34) == 4, 'Opponent score 10s digit is 3'
    assert take_turn(0, 71) == 8, 'Opponent score 10s digit is 7'
    assert take_turn(0,  7) == 1, 'Opponont score 10s digit is 0'

    print('---- Testing Touchdown rule ----')
    dice = make_test_dice(6)
    assert take_turn(2, 0, dice) == 14, 'Original score was 12'
    assert take_turn(3, 0, dice) == 21, 'Original score was 18'

    print('---- Testing Hogtimus Prime rule ----')
    dice = make_test_dice(5, 6, 5, 2)
    assert take_turn(0, 42, dice) == 7, 'Opponent score 10s digit is 4'
    assert take_turn(2, 0, dice) == 13, 'Original score was 11'
    assert take_turn(0, 52, dice) == 11, 'Opponent score 10s digit is 5'
    assert take_turn(2, 0, dice) == 11, 'Original score was 7'

    print('Tests for roll_dice and take_turn passed.')

    
    '*** You may add more tests here if you wish ***'
Example #17
0
 def test_take_turn2(self):
     val = take_turn(3, 0, make_test_dice(4, 6, 1))
     self.assertTrue(val == 1)
Example #18
0
 def test_roll_dice2(self):
     val = roll_dice(2, make_test_dice(4, 2, 1, 3))
     self.assertTrue(val == 6)
Example #19
0
from hog import play, always_roll, both, announce_lead_changes, say_scores
from dice import make_test_dice

dice = make_test_dice(1, 2, 3, 3)
always = always_roll

strat0 = strat1 = always(3)

s0, s1 = play(strat0, strat1, dice=dice, goal=8, say=both(say_scores, announce_lead_changes()))
Example #20
0
 def test_roll_dice3(self):
     val = roll_dice(3, make_test_dice(4, 2, 1, 3))
     self.assertTrue(val == 1)
Example #21
0
    >>> other(0)
    1
    >>> other(1)
    0
    """
    return 1 - player


def silence(score0, score1):
    """Announce nothing (see Phase 2)."""
    return silence


strat1 = lambda x, y: x // 10 - 2
strat0 = lambda x, y: y % 10
always_seven = make_test_dice(7)


def play(strategy0,
         strategy1,
         score0=0,
         score1=0,
         dice=six_sided,
         goal=GOAL_SCORE,
         say=silence):
    """Simulate a game and return the final scores of both players, with Player
    0's score first, and Player 1's score second.

    A strategy is a function that takes two total scores as arguments (the
    current player's score, and the opponent's score), and returns a number of
    dice that the current player will roll this turn.
Example #22
0
        if score0 < goal and score1 < goal:
            score1 += take_turn(strategy1(score1, score0), score0, dice)
            if is_swap(score0, score1):
                score0, score1 = score1, score0
            say = say(score0, score1)
        '''note that I feel this second "if" statement is necessary because I need to
        check whether the game has ended yet before performing player1's move'''
    # END PROBLEM 5 DONE!

    # (note that the indentation for the problem 6 prompt (***YOUR CODE HERE***) might be misleading)
    # BEGIN PROBLEM 6
    # END PROBLEM 6 DONE!
    return score0, score1


dice_7 = make_test_dice(7)
strat0 = lambda score, opp: opp % 10
strat1 = lambda score, opp: score // 10 - 2
s0, s1 = play(strat0, strat1, score0=61, score1=80, dice=dice_7)
print(80 // 10 - 2)

#######################
# 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_scores
Example #23
0
 def test_roll_dice1(self):
     val = roll_dice(1, make_test_dice(4, 2, 1, 3))
     self.assertTrue(val == 4)
Example #24
0
    return score0, score1

#'''
def always_roll(n):
    """Return a strategy that always rolls N dice.

    A strategy is a function that takes two total scores as arguments
    (the current player's score, and the opponent's score), and returns a
    number of dice 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 n
    return strategy


import doctest
if __name__ == "__main__":
    #Docktests always fail without this because the origional fair dice overide it
    four_sided = make_test_dice(1)
    six_sided = make_test_dice(3)
    doctest.testmod(verbose=False)

    #fix the dice from the doctest problem
    from dice import four_sided, six_sided
Example #25
0
    """Read in the command-line argument and calls corresponding functions.

    This function uses Python syntax/techniques not yet covered in this course.
    """
    import argparse
    parser = 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()


def total(s0, s1):
    print(s0 + s1)
    return echo


def echo(s0, s1):
    print(s0, s1)
    return total


if __name__ == '__main__':
    dice = make_test_dice(3, 1, 5, 6)
    averaged_roll_dice = make_averaged(roll_dice, 1000)
    print(averaged_roll_dice(2, dice))
Example #26
0
        print(score0)
        if score0 < 100:
            score1 += take_turn(strategy1(score1, score0), score0, dice)
            print(score1)
    # 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 6
    return score0, score1


strat0 = lambda self, opponent: opponent - opponent + 5
strat1 = lambda self, opponent: self - self + 3

always_3 = make_test_dice(3)

print(always_3())
print(play(strat0, strat1, 91, 10, always_3))

#######################
# 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_scores

Example #27
0
    pig_out = False
    while num_rolls > 0:
        one_roll = dice()
        if one_roll == 1:
            while num_rolls > 1:
                dice()
                num_rolls -= 1
            return 1
        else:
            sum += one_roll
        num_rolls -= 1
    return sum
    # END PROBLEM 1


roll_dice(2, make_test_dice(4, 6, 1))


def 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
    cubed = score**3
    points = 0
    subtract = True
    while cubed > 0:
        next_digit = cubed % 10
        if subtract == False:
Example #28
0
 def test_take_turn8(self):
     val = take_turn(2, 0, make_test_dice(6))
     self.assertTrue(val == 12)
Example #29
0
 def test_make_averaged(self):
     dice = make_test_dice(1, 6)
     val=  max_scoring_num_rolls(dice)
     self.assertEqual(val,1)
Example #30
0
import hog
always_three = hog.make_test_dice(3)
always_seven = hog.make_test_dice(7)

# Use strategies
# We recommend working this out turn-by-turn on a piece of paper (use Python for difficult calculations).
strat0 = lambda score, opponent: opponent % 10
strat1 = lambda score, opponent: max((score // 10) - 4, 0)
s0, s1 = hog.play(strat0, strat1, score0=71, score1=80, dice=always_seven)
print(s0,s1)
"""
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)



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(5), goal=10, say=count(1))



def echo(s0, s1):
    print(s0, s1)
    return echo
Example #31
0
 def test_make_averaged(self):
     dice = make_test_dice(4, 2, 5, 1)
     averaged_dice = make_averaged(dice, 1000)
     val = averaged_dice()
     self.assertEqual(val,3.0)
Example #32
0
def 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. Also
    implements the Hogtimus Prime and When Pigs Fly rules.

    num_rolls:       The number of dice rolls that will be made.
    opponent_score:  The total score of the opponent.
    dice:            A function of no args that returns an integer 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 2
    if num_rolls == 0:
        return free_bacon(opponent_score)
    else:
        uncapped_score = roll_dice(num_rolls, dice)
        if hogtimus_prime(uncapped_score):
            uncapped_score = hogtimus_prime(uncapped_score)
            print(uncapped_score)
        elif uncapped_score > (25 - num_rolls):
            return (25 - num_rolls)
    return uncapped_score


print(take_turn(1, 0, make_test_dice(3)))

# END PROBLEM 2