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