- 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์ message
- ์ฒซ๋ฒ์งธ ํด์์, ์ ๊ณต๋ 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
'๐โโ๏ธ ํ๋ > ํ๋ก์ ํธ ์์ ์ผ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Project: Ants vs SomeBees(2) (0) | 2022.07.26 |
---|---|
Project: Ants vs SomeBees(1) (0) | 2022.07.26 |
Project 2: CS 61A Autocorrected Typing Software(1) (0) | 2022.07.11 |
Phase 3: Strategies of the Game (0) | 2022.07.07 |
Project 1: The Game of Hog(1) (0) | 2022.07.07 |