cs61a 2020fall 学习记录hw01 hog
系统地学习berkeley cs61a中,对于学过算法和数据结构以及C++的人来说难度并不低,本次作业重点在于理解和灵活运用各模块,过程中遇到不解的地方也一一在此记录。
Phase 1 simulator
Phase 1 的作用是模拟的掷骰子过程,模块依次为
roll_dice,输入num_rolls(骰子数量),dice(掷的点数,具体看dice.py),输出total(掷的总点数)
free_bacon,take_turn,swine_align,pig_pass
"""CS 61A Presents The Game of Hog."""
from dice import six_sided, four_sided, make_test_dice
from ucb import main, trace, interact
GOAL_SCORE = 100 # The goal of Hog is to score 100 points.
FIRST_101_DIGITS_OF_PI = 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
######################
# Phase 1: Simulator #
######################
def roll_dice(num_rolls, dice=six_sided):
"""Simulate rolling the DICE exactly NUM_ROLLS > 0 times. Return the sum of
the 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
k,total,flag=0,0,0
while k<num_rolls:
number=dice()
total+=number
k+=1
if number==1:
flag=1
if flag==1:
return 1
elif flag==0:
return total
# END PROBLEM 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.'
pi = FIRST_101_DIGITS_OF_PI
# Trim pi to only (score + 1) digit(s)
# BEGIN PROBLEM 2
pi//=pow(10,(100-score))
# END PROBLEM 2
return pi % 10 + 3
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.
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
if num_rolls==0:
return free_bacon(opponent_score)
else:
return roll_dice(num_rolls,dice)
# END PROBLEM 3
def extra_turn(player_score, opponent_score):
"""Return whether the player gets an extra turn."""
return (pig_pass(player_score, opponent_score) or
swine_align(player_score, opponent_score))
def swine_align(player_score, opponent_score):
"""Return whether the player gets an extra turn due to Swine Align.
player_score: The total score of the current player.
opponent_score: The total score of the other player.
>>> swine_align(30, 45) # The GCD is 15.
True
>>> swine_align(35, 45) # The GCD is 5.
False
"""
# BEGIN PROBLEM 4a
if player_score==0 or opponent_score==0:
return False
k=min(player_score,opponent_score)
while k>=10:
if player_score%k==0 and opponent_score%k==0:
return True
else:
k-=1
return False
# END PROBLEM 4a
def pig_pass(player_score, opponent_score):
"""Return whether the player gets an extra turn due to Pig Pass.
player_score: The total score of the current player.
opponent_score: The total score of the other player.
>>> pig_pass(9, 12)
False
>>> pig_pass(10, 12)
True
>>> pig_pass(11, 12)
True
>>> pig_pass(12, 12)
False
>>> pig_pass(13, 12)
False
"""
# BEGIN PROBLEM 4b
if player_score<opponent_score and (opponent_score-player_score)<3:
return True
else:
return False
# END PROBLEM 4b
def other(who):
"""Return the other player, for a player WHO numbered 0 or 1.
>>> other(0)
1
>>> other(1)
0
"""
return 1 - who
def silence(score0, score1):
"""Announce nothing (see Phase 2)."""
return silence