def test_adds_cards_to_its_collection(self): card = Card(rank= "Ace",suit="Spades") deck = Deck() deck.add_cards([card]) self.assertEqual( deck.cards, [card] )
def test_adds_cards_to_its_collections(self): card1 = Card(rank = "Ace", suit = "Spades") card2 = Card(rank = "Jack", suit = "Diamonds") deck = Deck() deck.add_cards([card1, card2]) self.assertEqual( deck._cards, [card1, card2] )
def test_remove_specified_number_of_card_from_deck(self): ace = Card(rank="Ace", suit="Spades") eight = Card(rank="8", suit="Diamonds") cards = [ace, eight] deck = Deck() deck.add_cards(cards) self.assertEqual(deck.remove_card(1), [ace]) self.assertEqual(deck.cards, [eight])
def test_removes_specified_number_of_cards_from_deck(self): two = Card(rank="2", suit="Clubs"), ace = Card(rank="Ace", suit="Spades") cards = [two, ace] deck = Deck() deck.add_cards(cards) self.assertEqual(deck.remove_cards(1), [two]) self.assertEqual(deck.cards, [ace])
def test_to_remove_number_of_cards_from_deck(self): deck = Deck() ace = Card(rank="Ace", suite="spades") eight = Card(rank="8", suite="diamonds") cards = [ace, eight] deck.add_cards(cards) self.assertEqual(deck.remove_cards(1), [ace]) self.assertEqual(deck.cards, [eight])
def setupDeck(heroHand, villainHand, currentBoard=None): deck = Deck() # Remove dealt cards from Deck deck.cards.remove(heroHand.card1) deck.cards.remove(heroHand.card2) deck.cards.remove(villainHand.card1) deck.cards.remove(villainHand.card2) if currentBoard: for card in currentBoard.cards: deck.cards.remove(card) deck.shuffle() return deck
def train(self, *, iterations): deck = Deck() self.iteration = 1 self.cumulative_stack_utilities = [ 0 for stack_size in range(TOTAL_STACKS + 1) # (stack_size) / TOTAL_STACKS ] for self.iteration in range(1, iterations): for p0_stack in range(1, TOTAL_STACKS): p1_stack = TOTAL_STACKS - p0_stack deck.reset() deck.shuffle() p0_hand = Hand([deck.pop(), deck.pop()]) p1_hand = Hand([deck.pop(), deck.pop()]) big_blind = min(BIG_BLIND, p0_stack, p1_stack) self.cumulative_stack_utilities[p0_stack] += self.cfr( deck, (p0_hand, p1_hand), EMPTY_HISTORY, (p0_stack, p1_stack), (SMALL_BLIND, big_blind), (1, 1), ) # print(f"Average game value: {utility/iterations}") print(f"Iterations: {iterations}") print("Stack size,Utility") for stack_size, utility in enumerate(self.cumulative_stack_utilities): print(f"{stack_size},{self.get_average_stack_utility(stack_size)}") for node in map(lambda node: node[1], sorted(self.node_map.items())): print(node)
def __init__(self, id_, table): """ :type id_: int :type table: GameTable """ self.current_stage = None self.id_ = id_ self.table = table self.deck = Deck() self.pot = 0 self.current_bet = 0 self.current_raise = 0 self.game_states_iterator = iter(GameStageEnum)
def main(): print('Welcome to Poker Scorer') card_array = [ Card(value, suit) for suit in CARD_SUITS for value in CARD_VALUES ] deck = Deck(card_array) poker_game = Poker(deck) hand_1 = Hand([ Card('3', 'hearts'), Card('4', 'hearts'), Card('5', 'diamonds'), Card('4', 'clubs'), Card('2', 'hearts') ]) hand_2 = Hand([ Card('2', 'spades'), Card('4', 'spades'), Card('5', 'diamonds'), Card('4', 'diamonds'), Card('2', 'hearts') ]) poker_game.add_single_hand_to_game(hand_1) poker_game.add_single_hand_to_game(hand_2) print(poker_game.compare_two_hands())
def test_side_pot(sidepots): bot = SopelMock() player1 = Player(bot, "player1") player1.stack = 1000 player2 = Player(bot, "player2") player2.stack = 1000 player3 = Player(bot, "player3") player3.stack = 1000 players = [player1, player2, player3] dealer = Dealer(mock.MagicMock()) dealer.deal_cards(Deck(), players, 0) player1.in_play = 25 player2.in_play = 50 player3.in_play = 100 sidepots.set() pots = sidepots.calculate(players) assert pots assert len(pots) == 3 assert pots[0].value == 75 assert player1 in pots[0].players assert player2 in pots[0].players assert player3 in pots[0].players assert pots[1].value == 50 assert player2 in pots[1].players assert player3 in pots[1].players assert pots[2].value == 50 assert player3 in pots[2].players
def compute_hand_win_probability(deck: Deck, private_hand_0: Hand, private_hand_1: Hand): """ Returns a real between 0 and 1 representing the expected win rate of Private Hand 0 against Private Hand 1, when randomly sampling public cards. """ wins = 0 for _ in range(EXPECTED_VALUE_SIMULATIONS): deck.shuffle() public_cards = deck.peek(5) full_hand_0 = Hand(private_hand_0.cards + public_cards) full_hand_1 = Hand(private_hand_1.cards + public_cards) if full_hand_0 > full_hand_1: wins += 1 elif full_hand_0 == full_hand_1: wins += 0.5 return wins / EXPECTED_VALUE_SIMULATIONS
def test_shuffles_card_in_random_order(self, mock_shuffle): deck = Deck() cards = [Card(rank="2", suit="Clubs"), Card(rank="Ace", suit="Spades")] deck.add_cards(cards) deck.shuffle() mock_shuffle.assert_called_once_with(cards)
def test_remove_players_who_wants_to_fold(self): mock_player1 = MagicMock() mock_player2 = MagicMock() players = [mock_player1, mock_player2] mock_player1.wants_to_fold.return_value = True mock_player2.wants_to_fold.return_value = False play_ground = PlayGround(deck = Deck(), player=players) play_ground.play() self.assertEqual( play_ground.players, [mock_player2] )
def test_deck_shuffles_the_cards(self, mock_shuffle): deck = Deck() cards = [ Card(rank="Ace", suite="clubs"), Card(rank="8", suite="clubs") ] deck.add_cards(cards) deck.shuffle() mock_shuffle.assert_called_once_with(cards)
def __init__(self, bot): self.bot = bot self.players = [] self.hands = 0 self.round = 0 self.board = [] self.current_bet = 0 self.minraise = 0 self.nplyin = [0, 0, 0, 0] self.potsize = [0, 0, 0, 0] self.dealer = Dealer(self) self.blinds = Blinds(self) self.seats = SeatTracker(self) self.deck = Deck() self.pots = SidePots() self.shuffle_players = True
def test_overbet(sidepots): bot = SopelMock() player1 = Player(bot, "player1") player2 = Player(bot, "player2") players = [player1, player2] dealer = Dealer(mock.MagicMock()) dealer.deal_cards(Deck(), players, 0) player1.in_play = 25 player2.in_play = 100 sidepots.set() pots = sidepots.calculate(players) assert pots assert len(pots) == 2 assert pots[0].value == 50 assert player1 in pots[0].players assert player2 in pots[0].players assert pots[1].value == 75 assert player2 in pots[1].players
def test_has_length_that_is_equal_to_counts_of_cards(self): deck = Deck() self.assertEqual(len(deck), 0)
def test_stores_no_card_at_start(self): deck = Deck() self.assertEqual(deck.cards, [])
def test_shuffle(self): deck1 = Deck() deck2 = Deck() deck2.shuffle() assert deck1.cards != deck2.cards assert len(deck2.cards) == 52
def test_length_of_deck(self): deck = Deck() self.assertEqual(len(deck), 0)
def poker_client(): card_array = [Card(value, suit) for suit in CARD_SUITS for value in CARD_VALUES] deck = Deck(card_array) poker_game = Poker(deck) yield poker_game
def test_has_len_equal_to_cards_count(self): deck = Deck() self.assertEqual(len(deck), 0)
def test_add_cards_to_deck(self): card = Card(rank="2", suite="clubs") deck = Deck() deck.add_cards([card]) self.assertEqual(deck.cards, [card])
def test_deck_cards(self): deck = Deck() self.assertEqual(deck.cards, [])
class Game(PokerObject): SMALL_BLIND_AMOUNT = 2 BIG_BLIND_AMOUNT = 4 current_bet = 0 def __init__(self, id_, table): """ :type id_: int :type table: GameTable """ self.current_stage = None self.id_ = id_ self.table = table self.deck = Deck() self.pot = 0 self.current_bet = 0 self.current_raise = 0 self.game_states_iterator = iter(GameStageEnum) # ------- Game Process def play(self): # main loop: preflop -> flop -> turn -> river -> winners for self.current_stage in self.game_states_iterator: self.play_stage() print("=================================================================") def play_stage(self): print(self.current_stage.name.upper()) getattr(self, 'stage_' + self.current_stage.name)() def play_round(self): print(self.table.cards) while True: player = self.table.next_reacting_player() if not player or not player.action_required(self.current_stage, self.current_bet): break print("Table cards: " + str(self.table.cards)) self.request_player_action() print("\n- - - - - - - - -\n") self.current_bet = 0 self.current_raise = self.BIG_BLIND_AMOUNT self.table.make_dealer_current_player() # ------- Player def assign_pocket_cards(self): for player in self.table.players: player.pocket_cards = self.deck.get(2) def request_player_action(self): player = self.table.current_player() amount = player.request_action(self.current_stage, self.current_bet, self.current_raise, self.table.reacting_players_count) if amount > self.current_bet: new_raise = amount - self.current_bet # If the previous all-in raise amount was less than the minimum raise, # then the minimum raise is equal to the previous minimum raise. # The minimum legal raise is equal to the previous raise amount. if new_raise > self.current_raise: self.current_raise = new_raise self.current_bet = amount self.pot += amount # ------- Chips def charge_for_blinds(self): small_blind_amount = self.table.small_blind_player.charge(self.current_stage, Player.ACTION_SMALL_BLIND, self.SMALL_BLIND_AMOUNT) big_blind_amount = self.table.big_blind_player.charge(self.current_stage, Player.ACTION_BIG_BLIND, self.BIG_BLIND_AMOUNT) self.pot = small_blind_amount + big_blind_amount @staticmethod def __use_pot_contribution(players, contribution): result = 0 for p in players: amount = min(contribution, p.pot_contribution) p.pot_contribution -= amount result += amount return result def distribute_pot(self, winners): # make winners groups by power of the hands groups = [[winners[0]]] current_hand_power = winners[0].evaluator.power group_idx = 0 for winner in winners[1:]: if current_hand_power == winner.evaluator.power: groups[group_idx].append(winner) else: current_hand_power = winner.evaluator.power group_idx += 1 groups.append([winner]) # split in a group for group in groups: if self.pot == 0: break group_players_count = len(group) group = sorted(group, key=lambda x: x.pot_contribution) for i, p in enumerate(group): side_pot = Game.__use_pot_contribution(winners, p.pot_contribution) for w in group[i:group_players_count + 1]: reward_part = int(side_pot / (group_players_count - i)) w.reward += reward_part self.pot -= reward_part # extra chip after split should be given to a player left to the dealer button if self.pot > 0: p = self.table.left_from_dealer_not_folded_player() p.reward += self.pot self.pot = 0 for w in winners: w.increase_chips(w.reward) # ------- Stages def stage_welcome(self): print("Lets begin poker game #{}\n".format(self.id_)) self.assign_pocket_cards() def stage_preflop(self): self.charge_for_blinds() self.current_bet = self.BIG_BLIND_AMOUNT self.current_raise = self.BIG_BLIND_AMOUNT self.play_round() def stage_flop(self): self.table.flop = self.deck.get(3) self.play_round() def stage_turn(self): self.table.turn = self.deck.get(1) self.play_round() def stage_river(self): self.table.river = self.deck.get(1) self.play_round() def stage_winners(self): for p in self.table.players: if p.is_folded(): continue p.evaluator = Evaluator(p.pocket_cards, self.table.cards) winners = sorted([x for x in self.table.players if x.evaluator], key=lambda x: x.evaluator.power, reverse=True) print("=================================================================") print(self.table.cards) self.distribute_pot(winners) for winner in winners: e = winner.evaluator print( '{}, won ${}, balance ${}. {}: {} => {}, {}'.format(winner.account.name, winner.reward, winner.chips, e.name, winner.pocket_cards, e.combination_cards, e.power))
def start_round(self): self.flop = [] self.turn = None self.river = None self.deck = Deck() self.deck.shuffle()
class Game: def __init__(self, small_b, big_b): self.small_blind = small_b self.big_blind = big_b self.players = [None, None, None, None, None, None, None, None] self.fil_players = [] self.flop = [] self.turn = None self.river = None self.deck = None self.small_b_pos = None self.big_b_pos = None self.utg_pos = None self.small_b_pos_f = None self.big_b_pos_f = None self.side_pot = 0 self.pot = 0 self.current_bet = 0 # highest bet on the table self.round_num = 0 self.side_pot_active = False self.folded_val = 0 self.round_over = False def add_player(self, seat_pos, username, chips, id): if seat_pos < 8: if not self.players[seat_pos]: self.players[seat_pos] = Player() self.players[seat_pos].user = username self.players[seat_pos].seat_num = seat_pos self.players[seat_pos].coins = chips self.players[seat_pos].id = id def start_round(self): self.flop = [] self.turn = None self.river = None self.deck = Deck() self.deck.shuffle() def deal_cards(self): for i in range(8): if self.players[i] is not None: fst_card = self.deck.deck_card.pop(0) self.players[i].addToHand(fst_card) s_card = self.deck.deck_card.pop(0) self.players[i].addToHand(s_card) print(self.players[i].user, ":", fst_card, "and", s_card) def deal_flop(self): self.flop.append(self.deck.deck_card.pop(0)) self.flop.append(self.deck.deck_card.pop(0)) self.flop.append(self.deck.deck_card.pop(0)) print("The flop is:") for i in self.flop: print(i.number, i.suit) for i in self.flop: for j in self.fil_players: j.addToHand(i) def deal_turn(self): self.turn = self.deck.deck_card.pop(0) for j in self.fil_players: j.addToHand(self.turn) print("The turn is: ", self.turn) def deal_river(self): self.river = self.deck.deck_card.pop(0) for j in self.fil_players: j.addToHand(self.river) print("The river is: ", self.river) def set_blinds(self): cnt = 0 if self.round_num == 0: self.round_num += 1 for i in range(8): if self.players[i] is not None: self.players[i].small_blind = False self.players[i].big_blind = False for i in range(8): if self.players[i] is not None and cnt == 0: self.players[i].small_blind = True self.small_b_pos = i cnt += 1 elif self.players[i] is not None and cnt == 1: self.players[i].big_blind = True self.big_b_pos = i cnt += 1 else: big_b_set = False for i in range(8): if self.players[i] is not None and self.players[ i].small_blind and cnt == 0: self.players[i].small_blind = False elif self.players[i] is not None and self.players[ i].big_blind and cnt == 0: self.players[i].small_blind = True self.players[i].big_blind = False self.small_b_pos = i cnt += 1 elif self.players[i] is not None and cnt == 1: self.players[i].big_blind = True self.big_b_pos = i big_b_set = True cnt += 1 elif i == 7 and big_b_set == False: for j in range(8): if self.players[j] is not None and cnt == 1: self.players[j].big_blind = True self.big_b_pos = i big_b_set = True self.round_num = 0 cnt = 0 def one_pl_left(self): cnt = 0 for i in self.fil_players: if i.is_playing: cnt += 1 if cnt == 1: return True def last_player_pos(self): for i in range(len(self.fil_players)): if self.fil_players[i].is_playing: return i def fst_pos_after_bb(self): lst = [] for i in range(1, len(self.fil_players)): if self.fil_players[(self.big_b_pos_f + i) % len(self.fil_players)].is_playing: lst.append((self.big_b_pos_f + i) % len(self.fil_players)) return lst[0] def process_turn(self, pl_id, bet_size): for i in self.fil_players: if i.id == pl_id: if bet_size == -1: print("You have folded.") i.fold() self.folded_val += i.cumu_bet if self.one_pl_left(): print(self.fil_players[self.last_player_pos()].user, " has won the pot") self.fil_players[ self.last_player_pos()].coins += self.pot self.round_over = True self.pot = 0 break elif bet_size + i.bet == self.current_bet: i.call_check_raise(bet_size) i.coins -= bet_size self.pot += bet_size self.current_bet = i.bet i.cumu_bet += i.bet i.rebet = False elif bet_size + i.bet > self.current_bet: i.call_check_raise(bet_size) i.coins -= bet_size self.pot += bet_size self.current_bet = i.bet i.cumu_bet += i.bet i.rebet = False for j in self.fil_players: if j.is_playing and not j.id == i.id: j.rebet = True elif bet_size == -2: # means player is still in but has no coins to bet i.rebet = False else: print("Invalid bet. You have folded.") i.fold() if self.one_pl_left(): print(self.fil_players[self.last_player_pos()].user, " has won the pot") self.fil_players[ self.last_player_pos()].coins += self.pot self.pot = 0 break def bet_round_pf_new(self): re_bet = True while re_bet: re_bet = False for j in range(1, len(self.fil_players) + 1): if self.fil_players[(self.big_b_pos_f + j) % len( self.fil_players)].is_playing and self.fil_players[ (self.big_b_pos_f + j) % len(self.fil_players)].coins != 0: x = int(input("Enter a bet(-1 if folding): ")) y = self.fil_players[(self.big_b_pos_f + j) % len(self.fil_players)].id self.process_turn(y, x) some_true = False for i in self.fil_players: if i.is_playing: if i.rebet: some_true = True if not some_true: break for i in self.fil_players: if i.is_playing: if i.rebet: re_bet = True if not re_bet: self.current_bet = 0 for i in self.players: if i is not None: i.bet = 0 i.rebet = True for i in self.fil_players: i.bet = 0 i.rebet = True def bet_round_af_new(self): re_bet = True while re_bet: re_bet = False for j in range(len(self.fil_players)): if self.fil_players[(self.small_b_pos_f + j) % len( self.fil_players)].is_playing and self.fil_players[ (self.big_b_pos_f + j) % len(self.fil_players)].coins != 0: x = int(input("Enter a bet(-1 if folding): ")) y = self.fil_players[(self.small_b_pos_f + j) % len(self.fil_players)].id self.process_turn(y, x) some_true = False for i in self.fil_players: if i.is_playing: if i.rebet: some_true = True if not some_true: break for i in self.fil_players: if i.is_playing: if i.rebet: re_bet = True if not re_bet: self.current_bet = 0 for i in self.players: if i is not None: i.bet = 0 i.rebet = True for i in self.fil_players: i.bet = 0 i.rebet = True def check_dup(self, list_elems): if len(list_elems) == len(set(list_elems)): return False else: return True def get_cumu(self, c): return c.cumu_bet def get_winner(self): winner = [] win_cumu_bets = [] loser_bets = [] players_in = 0 for i in self.fil_players: if i.is_playing: players_in += 1 top_val = 0 for i in self.fil_players: if i.bestHand() > top_val: top_val = i.bestHand() winner = [i] win_cumu_bets = [i.cumu_bet] elif i.bestHand() == top_val: winner.append(i) win_cumu_bets.append(i.cumu_bet) for i in self.fil_players: if i not in winner: loser_bets.append(i) winner.sort(key=self.get_cumu) for i in winner: i.coins += i.cumu_bet print(i.user, "wins", "with a", i.top_hand[0]) prev_win = 0 prev_bet = 0 while winner: for i in loser_bets: if i.cumu_bet <= winner[0].cumu_bet: r_value = round(i.cumu_bet / len(winner)) winner[0].coins += r_value i.cumu_bet -= r_value else: prev_win = round((winner[0].cumu_bet - prev_bet) / len(winner)) + prev_win winner[0].coins += prev_win i.cumu_bet -= prev_win prev_bet = winner[0].cumu_bet winner.pop(0) for i in loser_bets: i.coins += i.cumu_bet self.pot = 0 def sb_bb_setup(self): for i in range(8): if self.players[i] is not None and self.players[i].small_blind: self.players[i].call_check_raise(self.small_blind) self.players[ i].coins = self.players[i].coins - self.small_blind self.players[i].bet = self.small_blind self.pot += self.small_blind print(self.players[i].user, " puts in a small blind of ", self.small_blind) if self.players[i] is not None and self.players[i].big_blind: self.players[i].call_check_raise(self.big_blind) self.players[i].coins = self.players[i].coins - self.big_blind self.pot += self.big_blind self.current_bet = self.big_blind self.players[i].bet = self.big_blind print(self.players[i].user, " puts in a small blind of ", self.big_blind) def filter_players(self): for i in self.players: if i is not None: self.fil_players.append(i) def play(self): self.start_round() self.deal_cards() self.set_blinds() self.sb_bb_setup() self.filter_players() for i in range(len(self.fil_players)): if self.fil_players[i].big_blind: self.small_b_pos_f = (i - 1) % len(self.fil_players) self.big_b_pos_f = i self.bet_round_pf_new() if not self.round_over: self.deal_flop() print("---------------------")
from poker.card import Card from poker.deck import Deck from poker.game import Game from poker.hand import Hand from poker.player import Player deck = Deck() cards = Card.create_standard_52_cards() deck.add_cards(cards) hand1 = Hand() hand2 = Hand() hand3 = Hand() player1 = Player(name="Leo", hand=hand1) player2 = Player(name="Rodrigo", hand=hand2) player3 = Player(name="Diego", hand=hand3) players = [player1, player2, player3] game = Game(deck=deck, players=players) game.play() for player in players: index, hand_name, hand_cards = player.best_hand() hand_cards_strings = [str(card) for card in hand_cards] hand_cards_string = " and ".join(hand_cards_strings) print( f"{player.name} has a {hand_name} with the following hand: {hand_cards_string}." ) winners_list_name = [max(players).name]
from poker.deck import Deck from poker.card import Card from poker.evaluator import Evaluator players_number = 4 d = Deck() five = d.get(5) #five = [Card(4, 'D'), Card(5, 'D'), Card(3, 'D'), Card(14, 'D')] # straight from '2' print(five) players_hands = [] for i in range(1, players_number + 1): two = d.get(2) players_hands.append([two, Evaluator(two, five)]) players_hands_sorted = sorted(players_hands, key=lambda x: x[1].power, reverse = True) for player_hand in players_hands_sorted: two = player_hand[0] c = player_hand[1] # print('Pocket cards #{}: {} '.format(i, two)) # print(c.evaluator[c.combination]['name']) # print('Winning combination #{}: {} '.format(i, c.combination_cards)) # print('Possible kickers #{}: {} '.format(i, c.kicker_cards)) # print('Power #{}: {}'.format(i, c.power))
def test_empty_at_start(self): deck = Deck() self.assertAlmostEqual(deck.cards, [])