Esempio n. 1
0
def resolve_hands(p, g):
    """Check who has strongest hand and divide the pot accordingly

    :param p: List of players to resolve the winner
    :param g: Game object
    :return:
    """
    # take five additional cards from the deck
    # add them to users hand
    for c in range(5):
        new_card = g.deck.draw_card()
        for player in p:
            player.hand.add_card(new_card)

    # sort the hands
    for pl in p:
        pl.hand.sort()

    print("Resolving player hands")
    g.render_game()

    # get the absolute score of the hand and the best five cards
    results = []
    for player in p:
        results.append(Game.score(player.hand))
    for i in range(g.p.__len__()):
        print(g.p[i].name, "has", g.name_of_hand(results[i][0]))

    # select the winner
    winners = Game.determine_winner(results)
    # award the pot to the winner
    if winners.__len__() > 1:
        # split the pot
        print("No winner - split the pot")
        g.split_the_pot()
    else:
        print(p[winners[0]].name, "has taken the pot")
        print(p[winners[0]].name, "gained", min(p[winners[0]].bet * 2, g.pot))
        g.player_won(p[winners[0]])
Esempio n. 2
0
def main(p1, p2, num_of_games, num_of_chips):
    import time
    start_time = time.time()
    # [0] player1 won accumulator, [1] player2 won accumulator
    stats = [0, 0]
    game_lengths_won_by_p1 = [0]
    game_lengths_won_by_p2 = [0]
    #  successful bluff is when p1 is when SB goes all-in, and opponent folds, but would have won if called
    bluffs_p1 = [0, 0]
    bluffs_p2 = [0, 0]
    if "n" in [p1, p2]:
        neural_npc = NeuralNetworkNPC()
    else:
        neural_npc = None
    if "q" in [p1, p2]:
        q_table_npc = QtableNPC()
    else:
        q_table_npc = None
    for games in range(num_of_games):
        # q - indicate q-table, indicate
        game = Game(player_types[p1],
                    player_types[p1],
                    player_types[p2],
                    player_types[p2],
                    bank=num_of_chips)
        if random() > 0.5:
            game.end_round()
        game_length = 0
        while not game.done:
            if game.a_player().bank <= 0 or game.na_player().bank <= 0:
                game.done = True
                break
            else:
                game.render_game()
                print(game.p[game.turn].name + " is small blind")
                print("Placing blinds")
                game.place_blinds()
                game.render_game()
            game.players_draw_cards()
            current_player_index = game.turn
            result = small_blind(game.p[game.turn],
                                 game,
                                 neural_model_npc=neural_npc,
                                 q_table_npc=q_table_npc)
            game.next_player()
            if result:
                result = big_blind(game.p[game.turn],
                                   game,
                                   neural_model_npc=neural_npc,
                                   q_table_npc=q_table_npc)
                if result:
                    resolve_hands(game.p, game)
                else:
                    # bluff?
                    results = []
                    community_cards = []
                    for i in range(5):
                        community_cards.append(game.deck.draw_card())
                    for ep in game.p:
                        hand = copy.deepcopy(ep.hand)
                        hand.cards.extend(community_cards)
                        hand.sort()
                        results.append(Game.score(hand))
                    # select the winner
                    winners = Game.determine_winner(results)
                    if winners[0] == current_player_index:
                        if current_player_index == 0:
                            bluffs_p1[0] += 1
                        else:
                            bluffs_p2[0] += 1
                    if current_player_index == 0:
                        bluffs_p1[1] += 1
                    else:
                        bluffs_p2[1] += 1
                    game.opponent_folded(game.na_player())
            else:
                game.opponent_folded(game.a_player())
            game.new_step()
            print()
            game_length += 1

        # region End game
        if game.done:
            if game.p[0].bank > game.p[1].bank:
                stats[0] += 1
                game_lengths_won_by_p1.append(game_length)
            else:
                stats[1] += 1
                game_lengths_won_by_p2.append(game_length)
            for pl in game.p:
                pl.bank += pl.bet
                pl.bet = 0
            if game.a_player().bank <= 0:
                pl = game.na_player()
            else:
                pl = game.a_player()
            s = pl.name + " has won the game with "
            s += str(pl.bank) + " coins"

            print(s)
        print('\n')
        # endregion
        print(stats)
    print("In total: Player1[" + player_types[p1] + "] has won " +
          str(stats[0]))
    print("In total: Player2[" + player_types[p2] + "] has won " +
          str(stats[1]))
    if len(game_lengths_won_by_p1) > 1:
        print("Average game length when Player1[" + player_types[p1] +
              "] has won " + str(
                  round(
                      sum(game_lengths_won_by_p1) /
                      len(game_lengths_won_by_p1), 2)))
    if len(game_lengths_won_by_p2) > 1:
        print("Average game length when Player2[" + player_types[p2] +
              "] has won " + str(
                  round(
                      sum(game_lengths_won_by_p2) /
                      len(game_lengths_won_by_p2), 2)))
    print("Successful bluffs by Player1[" + player_types[p1] + "] " +
          str(bluffs_p1[0]) + " / " + str(bluffs_p1[1]))
    print("Successful bluffs by Player2[" + player_types[p2] + "] " +
          str(bluffs_p2[0]) + " / " + str(bluffs_p2[1]))
    print("time elapsed: {:.2f}s".format(time.time() - start_time))