๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿƒ‍โ™€๏ธ ํ™œ๋™/ํ”„๋กœ์ ํŠธ ์ž‘์—…์ผ์ง€

Hog(2): Playing the Game

  • Commentary functions
    • ๊ฒŒ์ž„ ์„ค๋ช…ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ
    • ์‚ฌ์šฉํ•  ์ธ์ˆ˜
      • score0, score1, ์ด์ „์˜ leading player, leader
    • ๋ฆฌํ„ด๊ฐ’
      • ํ˜„์žฌ leading player(์ ์ˆ˜๊ฐ€ ๋” ๋†’์€ ํ”Œ๋ ˆ์ด์–ด), printํ•  ๋ฉ”์‹œ์ง€ ๋˜๋Š” None
    • ๊ทธ๋ƒฅ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋‹ˆ๊นŒ ํ”„๋กœ๊ทธ๋žจ์ƒ์˜ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค์ง€๋Š” ์•Š์„ ๊ฒƒ

Problem 6: announce_lead_changes ๊ตฌํ˜„

  • lead๊ฐ€ ๋ฐ”๋€Œ๋Š”๊ฑธ ์ซ“์•„๊ฐ€๋Š” ์ฃผ์„ํ•จ์ˆ˜์ด๋‹ค.
  • leading player๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์ด ํ•จ์ˆ˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • leading player๊ฐ€ ์—†๊ฑฐ๋‚˜(== ์ฆ‰, ๋‘ ํ”Œ๋ ˆ์ด์–ด์˜ ์ ์ˆ˜๊ฐ€ ๋˜‘๊ฐ™๋‹ค๋ฉด) ์ด์ „ leading player๋กœ๋ถ€ํ„ฐ ์•„๋ฌด ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค๋ฉด ์ด ํ•จ์ˆ˜๋Š” None์„ message๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.
  • ์ด ํ•จ์ˆ˜์˜ ๋งˆ์ง€๋ง‰์— ๋‘ ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์•ผํ•œ๋‹ค.
    • ๋” ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ player ๋˜๋Š” None
    • printํ•  ๋ฉ”์„ธ์ง€ ๋˜๋Š” None
  • leading player์— ๋ณ€ํ™”๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ํ•จ์ˆ˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค.
    • ์ด ๋ฉ”์‹œ์ง€์˜ ํ˜•์‹์€ ๋งค์šฐ ๊ตฌ์ฒด์ ์ด๋ผ์„œ ๋‚ด ๊ตฌํ˜„์€ ์ œ๊ณต๋œ doctest(์˜ˆ์‹œ)์— ์žˆ๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์•„์•ผ ํ•œ๋‹ค.
def announce_lead_changes(score0, score1, last_leader=None):
    """A commentary function that announces when the leader has changed.

    >>> leader, message = announce_lead_changes(5, 0)
    >>> print(message)
    Player 0 takes the lead by 5
    >>> leader, message = announce_lead_changes(5, 12, leader)
    >>> print(message)
    Player 1 takes the lead by 7
    >>> leader, message = announce_lead_changes(8, 12, leader)
    >>> print(leader, message)
    1 None
    >>> leader, message = announce_lead_changes(8, 13, leader)
    >>> leader, message = announce_lead_changes(15, 13, leader)
    >>> print(message)
    Player 0 takes the lead by 2
    """
    # BEGIN PROBLEM 6
    differ = 0
    leader = None
    if score0 > score1:
        leader = 0
        differ = score0 - score1
        if last_leader != leader:
            message = f"Player 0 takes the lead by {differ}"
        else:
            message = None
    elif score1 > score0:
        leader = 1
        differ = score1 - score0
        if last_leader != leader:
            message = f"Player 1 takes the lead by {differ}"
        else:
            message = None
    else:
        message = None
    
    return leader, message
    # END PROBLEM 6

Problem 7: playํ•จ์ˆ˜ ์—…๋ฐ์ดํŠธ

  • ๊ฐ turn์ด ๋๋‚  ๋•Œ ๋งˆ๋‹ค commentํ•จ์ˆ˜์ธ say๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก play function ์—…๋ฐ์ดํŠธ
  • sayํ•จ์ˆ˜๊ฐ€ ๋ถˆ๋ฆด ๋•Œ ๋งˆ๋‹ค, ๋‘ ๊ฐ’์ด ๋ฆฌํ„ด๋œ๋‹ค.
    • leader์™€ message
      • leader๋Š” ๋‹ค์Œ ํ„ด์— sayํ•จ์ˆ˜์— 3๋ฒˆ์งธ ์ธ์ˆ˜๋กœ์„œ ๋„˜๊ฒจ์ ธ์•ผ ํ•จ
      • message๊ฐ€ None์ด ์•„๋‹ˆ๊ณ  empty string("")์ด ์•„๋‹ˆ๋ฉด printํ•ด์•ผ ํ•œ๋‹ค.
  • ์ฒซ๋ฒˆ์งธ ํ„ด์—์„œ, ์ œ๊ณต๋œ leader์„ sayํ•จ์ˆ˜์˜ ์„ธ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ์„œ ๋„˜๊ฒจ์ค€๋‹ค.
  • ๊ฐ๊ฐ ์—ฐ์†์ ์œผ๋กœ callํ•œ sayํ•จ์ˆ˜๋Š” ์ด์ „์— callํ•œ sayํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์— ์˜์กดํ•œ๋‹ค.

 

  • ๋‘๋ฒˆ์งธ ๋ฆฌํ„ด๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š”๊ฒŒ ๊ทœ์น™์ด๋‹ค.
  •  
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.

    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 0
    score1:     Starting score for Player 1
    dice:       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 every turn.
    """
    who = 0  # Who is about to take a turn, 0 (first) or 1 (second)
    leader = None  # To be used in problem 7
    # BEGIN PROBLEM 5
    while score0 < goal and score1 < goal:
        if who == 0:
            roll_turn = strategy0(score0, score1) #์ด๋ฒˆ ํ„ด์˜ roll์ˆ˜ ๋ฐ›๊ธฐ
            score_add = take_turn(roll_turn, score0, score1, dice, goal)
            score0 += score_add
            score0 += pigs_on_prime(score0,score1)
        else:
            roll_turn = strategy1(score1, score0)
            score_add = take_turn(roll_turn, score1, score0, dice, goal)
            score1 += score_add
            score1 += pigs_on_prime(score1,score0)
        who = next_player(who)
    # END PROBLEM 5

    # (note that the indentation for the problem 7 prompt (***YOUR CODE HERE***) might be misleading)
    # BEGIN PROBLEM 7
        leader, message = say(score0, score1, leader)
        if message != None and message != "":
            print(message)

    # END PROBLEM 7
    return score0, score1