Пример #1
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    "*** YOUR CODE HERE ***"
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    player1, player2 = 0, 0
    while player1 < goal and player2 < goal:    
        if who==0:                  # first player
            if strategy == make_die_specific_strategy or (player1+player2)%7 == 0:
                player1 += take_turn(strategy(player1,player2), dice=make_fair_die(4), who='Player1', comments=False)
            else:
                player1 += take_turn(strategy(player1,player2), dice=make_fair_die(), who='Player1', comments=False)
            #print('Player1 current points: ', player1)
        else:                       #second player
            if strategy == make_die_specific_strategy or (player1+player2)%7 == 0:
                player2 += take_turn(opponent_strategy(player1,player2), dice=make_fair_die(4), who='Player2', comments=False)
            else:
                player2 += take_turn(opponent_strategy(player1,player2), dice=make_fair_die(), who='Player2', comments=False)
            #print('Player2 current points: ', player2)

        who = other(who)                  #switch turn
    return who
Пример #2
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    "*** YOUR CODE HERE ***"
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    dice, player1, player2 = 0, 0, 0
    
    while player1 < goal and player2 < goal:
        if who == 0:
            if (player1+player2)%7 == 0:
                player1 += take_turn(strategy(player1,player2), dice=make_fair_die(4), who='Player1', comments=True)
            else:
                player1 += take_turn(strategy(player1,player2), dice=make_fair_die(), who='Player1', comments=True)
            #print('Player1 current points: ', player1)
        else:                       #second player
            if (player1+player2)%7 == 0:
                player2 += take_turn(opponent_strategy(player2,player1), dice=make_fair_die(4), who='Player2', comments=True)
            else:
                player2 += take_turn(opponent_strategy(player2,player1), dice=make_fair_die(), who='Player2', comments=True)
            #print('Player2 current points: ', player2)
        who = other(who)

    if player1 > player2:
        who = 0
    else:
        who = 1
    return who
Пример #3
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    p1_total = 0
    p2_total = 0
    winner = 0
    dice = make_fair_die()
    while p1_total < goal and p2_total < goal:
        if (p1_total+p2_total)%7 == 0:
            dice = make_fair_die(4)
        else:
            dice = make_fair_die()
        if who == 0:
            p1_total+=take_turn(strategy(p1_total, p2_total), dice, "Player 1", False)
        else:
            p2_total+=take_turn(opponent_strategy(p1_total, p2_total), dice, "Player 2", False)
        who = other(who)
    if p1_total > 100:
        winner = 0
    else:
        winner = 1
    return winner
Пример #4
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    score1, score2 = 0, 0
    plan1 = strategy(score1, score2)
    plan2 = opponent_strategy(score1, score2)
    win = False
    while win == False:
        if who == 0:
            if score1 <= 100:
                dice=make_fair_die()
                score_for_turn = take_turn(plan1, dice, 'player 1', False)
                score1 = score1 + score_for_turn
                #print('player 1 score: ', score1)
                if score1 >= 100:
                    win = True
                    who = other(who)
            who = other(who)
        else:
            if score2 <= 100:
                dice=make_fair_die()
                score_for_turn = take_turn(plan2, dice, 'player 2', False)
                score2 = score2 + score_for_turn
                #print('player 2 score: ', score2)
                if score2 >= 100:
                    win = True
                    who = other(who)
            who = other(who)
    return who
Пример #5
0
Файл: pig.py Проект: willguo/pig
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    "*** YOUR CODE HERE ***"

    score1, score2 = 0, 0
    temp_dice = make_fair_die()
    temp_score1, temp_score2 = 0, 0
    
    while score1 < 100 and score2 < 100:
        
        if ((score1 + score2) % 7 == 0):
            temp_dice = make_fair_die(4)
        temp_score1 = take_turn(strategy(score1, score2), temp_dice, 'Player One')
        score1 = score1 + temp_score1
        temp_dice = make_fair_die()

        if score1 >= 100:
            break

        if ((score1 + score2) % 7 == 0):
            temp_dice = make_fair_die(4)
        temp_score2 = take_turn(opponent_strategy(score1, score2), temp_dice, 'Player Two')
        score2 = score2 + temp_score2
        temp_dice = make_fair_die()
        #print(score1, score2)
        
    if score1 >= 100:
        return who
    if score2 >= 100:
        return other(who)
Пример #6
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=False):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"
    turn_total = 0
    over = False
    while over == False:
        dice_result = dice()
        action = plan(turn_total)
        if action == roll:
            score, turn_total, over = action(turn_total, dice_result)
            if score == 1:
                score_for_turn = 1
            if comments == True:
                commentate(action, dice_result, score_for_turn, turn_total,
                           over, who)
        else:
            score_for_turn = turn_total
            score, turn_total, over = action(turn_total, dice_result)
            if comments == True:
                commentate(action, dice_result, score_for_turn, turn_total,
                           over, who)
    return score_for_turn
Пример #7
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.

    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
	
	输入 strategy  对手strategy
	输出 

    """
    who = 1  # Which player is about to take a turn, 0 (first) or 1 (second)
    "*** YOUR CODE HERE ***"
    score_total = [0, 0]
    player_stgy = [strategy, opponent_strategy]
    while score_total[who] < goal:
        who = other(who)
        if who:
            playername = 'Second Player'
        else:
            playername = 'First Player'
        if (((score_total[who] % 7) == 0)
                and ((score_total[other(who)] % 7) == 0)):
            score_total[who] += take_turn(plan = player_stgy[who](score_total[who],\
            score_total[other(who)]), dice = make_fair_die(sides=4), who = playername)
        else:
            score_total[who] += take_turn(plan = player_stgy[who](score_total[who],\
            score_total[other(who)]), who = playername)
        # print(playername, "'s score_total is", score_total[who])

    return who
Пример #8
0
def take_turn(plan, dice=make_fair_die(), who='Paul Hilfinger',
              comments=False):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"
    turn_total = 0
    turn_is_over = False
    while not turn_is_over:
        action = plan(turn_total)
        outcome = dice()
        score_for_turn, turn_total, turn_is_over = action(turn_total, outcome)
        if comments:
            commentate(action, outcome, score_for_turn, turn_total,
                       turn_is_over, who)
    return score_for_turn
Пример #9
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=False): 
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn
    end_turn = False
    turn_total = 0
    while end_turn == False:
        outcome = dice()
        decision = plan(turn_total)
        result = decision(turn_total, outcome)
        end_turn = result[2]
        turn_total = result[1]
        if end_turn:
            turn_total = result[0]
        if comments:
            print(commentate(decision, outcome, score_for_turn,
                         turn_total, end_turn, who))
    
    score_for_turn = turn_total
    return score_for_turn
Пример #10
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=False):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"
    turn_total = 0
    over = False
    while over == False:
        dice_result = dice()
        action = plan(turn_total)
        if action == roll:
            score, turn_total, over = action(turn_total, dice_result)
            if score == 1:
                score_for_turn = 1
            if comments == True:
                commentate(action, dice_result, score_for_turn, turn_total, over, who)
        else:
            score_for_turn = turn_total
            score, turn_total, over = action(turn_total, dice_result)
            if comments == True:
                commentate(action, dice_result, score_for_turn, turn_total, over, who)
    return score_for_turn
Пример #11
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=False):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.

    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    """
		输入 plan,骰子函数,人名,是否注释
		输出 回合总分
    """

    score_for_turn = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"

    isended = False
    while (isended == False):
        dice_res = dice()
        results = plan(score_for_turn)(score_for_turn, dice_res)
        if results[2] == False:
            score_for_turn = results[1]
        else:
            score_for_turn = results[0]
            isended = True
        if comments == True:
            commentate(plan(score_for_turn), dice_res, score_for_turn,
                       score_for_turn, not isended, who)
    return score_for_turn
Пример #12
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=True):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn (after holding)
    "*** YOUR CODE HERE ***"
    turn_over=False # Is the turn over or not?
    turn_value=0 # The number of points that the player has (before holding)
    dice_outcome=0 # The outcome of the dice
    while turn_over==False:
        dice_outcome=dice()
        func1=plan(turn_value)
        score_for_turn,turn_value,turn_over = func1(turn_value,dice_outcome)
        if comments==True:
            commentate(func1,dice_outcome,score_for_turn,turn_value,turn_over,who='Someone')
    return score_for_turn
Пример #13
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    "*** YOUR CODE HERE ***"
    
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    player1_score=0 #Total score for player 1
    player2_score=0 #Total score for player 2
    plan_player1=strategy(player1_score,player2_score) #the plan for the current turn player 1
    plan_player2=strategy(player1_score,player2_score) #the plan for the current turn player 2
    is_turn_over=False
    while player1_score<100 and player2_score<100:
        plan_player1=strategy(player1_score,player2_score)
        plan_player2=strategy(player1_score,player2_score)
        if (player1_score+player2_score)%7!=0:
            if who==0:
                player1_score=player1_score+take_turn(plan_player1,dice=make_fair_die(6),who='Player 1',comments=False)
                who=1
                """print("Player 1 has ",player1_score,"points")"""
            elif who==1:
                player2_score=player2_score+take_turn(plan_player2,dice=make_fair_die(6),who='Player 2',comments=False)
                who=0
                """print("Player 2 has ",player2_score,"points")"""
        else:
            if who==0:
                player1_score=player1_score+take_turn(plan_player1,dice=make_fair_die(4),who='Player 1',comments=False)
                who=1
                """print("Player 1 has ",player1_score,"points")"""
            elif who==1:
                player2_score=player2_score+take_turn(plan_player2,dice=make_fair_die(4),who='Player 2',comments=False)
                who=0
                """print("Player 2 has ",player2_score,"points")"""
    if player1_score>=100:
        return 0
    elif player2_score>=100:
        return 1
    
            
        
    
    
    return who
Пример #14
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    "*** YOUR CODE HERE ***"

    who = 0  # Which player is about to take a turn, 0 (first) or 1 (second)
    player1_score = 0  #Total score for player 1
    player2_score = 0  #Total score for player 2
    is_game_over = False
    while player1_score < 100 and player2_score < 100:
        while is_game_over == False:
            if (player1_score + player2_score) % 7 == 0:
                dice = make_fair_die(4)
            else:
                dice = make_fair_die()
            if who == 0:
                player1_score = player1_score + take_turn(strategy(
                    player1_score, player2_score),
                                                          dice,
                                                          who='Player',
                                                          comments=False)
                if player1_score >= 100:
                    is_game_over = True
                    return 0
                else:
                    who = 1
            elif who == 1:
                player2_score = player2_score + take_turn(opponent_strategy(
                    player2_score, player1_score),
                                                          dice,
                                                          who='Enemy',
                                                          comments=False)
                if player2_score >= 100:
                    is_game_over = True
                    return 1
                else:
                    who = 0

    return who
Пример #15
0
def play(strategy, opponent_strategy, comments=False):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    who = 0  # Which player is about to take a turn, 0 (first) or 1 (second)
    "*** YOUR CODE HERE ***"
    dice_4 = make_fair_die(4)
    dice_6 = make_fair_die(6)

    if comments:
        print("--- Start of the play. ---")
    player1_score = 0
    player2_score = 0
    while True:
        if comments:
            print("Current score: %d vs %d" % (player1_score, player2_score))

        dice = dice_4 if (player1_score + player2_score) % 7 == 0 else dice_6

        player1_plan = strategy(player1_score, player2_score)
        player2_plan = opponent_strategy(player2_score, player1_score)

        player1_score += take_turn(player1_plan, dice, who='player1')
        if player1_score >= 100:
            who = 0
            break
        else:
            player2_score += take_turn(player2_plan, dice, who='player2')
            if player2_score >= 100:
                who = 1
                break

    if comments:
        print("\nPlayer %d won! Scored: %d\n---End of the play.---\n\n" % (who + 1,
            [player1_score, player2_score][who]))

    return who
Пример #16
0
def take_turn(tactic, dice=make_fair_die(), who="Someone", comments=False):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The dice function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    tactic -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    turn_score = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"
    return turn_score
Пример #17
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=False):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"
    return score_for_turn
Пример #18
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=True):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  # Points scored in the whole turn
    "*** YOUR CODE HERE ***"
    
    score_for_turn=plan(sore_for_turn)(sore_for_turn,dice()).get(0)
    if comments=True:
       print commentate.__doc__
Пример #19
0
def take_turn(plan, dice=make_fair_die(), who='Someone', comments=True):
    """Simulate a single turn and return the points scored for the whole turn.

    Important: The d function should be called once, **and only once**, for
               every action taken!  Testing depends upon this fact.
    
    Arguments:
    plan -- a function that takes the turn total and returns an action function
    dice -- a function that takes no args and returns an integer outcome.
            Note: dice is non-pure!  Call it exactly once per action.
    who -- name of the current player
    comments -- a boolean; whether commentary is enabled
    """
    score_for_turn = 0  #Points scored in the whole turn
    outcome = dice()
    result = []
    turn_total = score_for_turn
    action = plan(score_for_turn)

    while not action == hold:
        result = action(turn_total, outcome)  #(score, turn_total, over)
        turn_total = result[1]
        over = result[2]
        score_for_turn = result[0] + turn_total
        # commentate(action, outcome, score_for_turn, turn_total, over, who):
        if over:
            action = hold
        else:
            if comments:
                commentate(action, outcome, score_for_turn, turn_total, over,
                           who)
            outcome = dice()
            action = plan(turn_total)
    over = True
    if comments:
        commentate(action, outcome, score_for_turn, turn_total, over, who)
    return score_for_turn
Пример #20
0
def play(strategy, opponent_strategy):
    """Simulate a game and return 0 if the first player wins and 1 otherwise.
    
    strategy -- The strategy function for the first player (who plays first)
    opponent_strategy -- The strategy function for the second player
    """
    who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
    is_game_over = False
    score_player1, score_player2 = 0, 0
    while not is_game_over:
        if who == 0:
            plan = strategy(score_player1, score_player2)
        elif who == 1:
            plan = opponent_strategy(score_player2, score_player1)
        if (score_player1 + score_player2) % 7 == 0:
            sides = 4
        else:
            sides = 6
        score_for_turn = take_turn(plan, make_fair_die(sides), 'player1' if who == 0 else 'player2')
        if who == 0:
            score_player1 += score_for_turn
        elif who == 1:
            score_player2 += score_for_turn
        who = 1 if who == 0 else 0
        if score_player1 >= 100:
            who = 0
            is_game_over = True
        elif score_player2 >= 100:
            who = 1
            is_game_over = True
    print('score of player1:', score_player1)
    print('score of player2:', score_player2)
    if who == 0:
        print('player1 wins!')
    elif who == 1:
        print('player2 wins!')
    return who
Пример #21
0
def take_turn_test():
    """Test the take_turn function using deterministic test dice."""
    plan = make_roll_until_plan(10)  # plan is a function (see problem 2)
    "*** YOUR CODE HERE ***"
    print(take_turn(plan, dice=make_fair_die()))  # deterministic