def estimateEquity2players3cards(heroCards, villainCards, numberOfRunouts=1000, board=None): deck = FULL_DECK.copy() for h in heroCards + villainCards: deck.remove(h) if board: for h in board: deck.remove(h) heroWins, villainWins, tie = 0, 0, 0 for i in range(numberOfRunouts): board = deal_cards(deck.copy(), 5) # print(board) b1 = best5plus3(board, heroCards) b2 = best5plus3(board, villainCards) if (b1 > b2): heroWins += 1 elif (b1 < b2): villainWins += 1 else: tie += 1 return heroWins, villainWins, tie
def main_program(): """ :return: the table output """ res = { 'Pair': 0, 'Two-pairs': 0, 'Flush': 0, 'High-card': 0, } header = ['# of hands', 'pairs', '%', '2 pairs', '%', 'flushes', '%', 'high card', '%'] print('%10s %11s %3s %13s %3s %12s %3s %14s %3s' % (tuple(header))) for i in range(NUM_OF_HANDS_IN_TABLE): deck = deck_of_cards.create_deck() for k in range(CARDS_IN_ONE_HAND): hands = deck_of_cards.deal_cards(deck) # Replicate to make cards in both hands for j in range(2): hand = hands[5 * j:5 * j + 5] if poker_hand.check_pair(hand) == IS_PAIR: res['Pair'] += 1 elif poker_hand.check_pair(hand) == IS_TWO_PAIRS: res['Two-pairs'] += 1 else: if poker_hand.check_flush(hand): res['Flush'] += 1 else: res['High-card'] += 1 if (i + 1) % 1000 == 0: # only consider when the number of hands less than or equal to 100000 # and divisible by 1000 (10,000; 20,000; ... ;100,000) percent = {} for key in res.keys(): percent[key] = res[key] / i / 10 * 100 # Calculate the percent of kinds of cards num_hands = (i + 1) * 10 # the number of hands run from 10,000 to 100,000 and distance is 10,000 print('{:>10,} {:>11d} {:>05.2f} {:>11d} {:>05.2f} {:>10d} {:>05.2f} {:>12d} {:>05.2f}' .format(num_hands, res['Pair'], percent['Pair'], res['Two-pairs'], percent['Two-pairs'], res['Flush'], percent['Flush'], res['High-card'], percent['High-card']))
def hit(self, deck): if self.name == 'Dealer': deck.deal_cards(self) elif self.name == 'Human': deck.deal_cards(self)
board = deal_cards(deck.copy(), 5) # print(board) b1 = best5plus3(board, heroCards) b2 = best5plus3(board, villainCards) if (b1 > b2): heroWins += 1 elif (b1 < b2): villainWins += 1 else: tie += 1 return heroWins, villainWins, tie d = FULL_DECK.copy() h1 = deal_cards(d, 3) print("Hero has:", h1) h2 = deal_cards(d, 3) print("Villain has:", h2) trials = 1000 heroWins, villainWins, ties = estimateEquity2players3cards(h1, h2, trials) print("1k -- Hero wins:", heroWins / trials, "Villain wins:", villainWins / trials, "Ties:", ties / trials) trials = 2000 heroWins, villainWins, ties = estimateEquity2players3cards(h1, h2, trials) print("2k -- Hero wins:", heroWins / trials, "Villain wins:", villainWins / trials, "Ties:", ties / trials) trials = 5000
from deck import Card, FULL_DECK, deal_cards from hand import Hand, rank5 for i in range(50): rank5(deal_cards(FULL_DECK.copy(), 5)) royal = Hand.fromString('As Ks Qs Js Ts') quads = Hand.fromString('3s 3h 3d 3c 2c') full = Hand.fromString('As Ah Kd Ks Kh') flush = Hand.fromString('3s 4s 7s 9s Js') trip2sAK = Hand.fromString('As Kh 2d 2s 2c') trip2sAQ = Hand.fromString('As Qh 2d 2s 2c') str8 = Hand.fromString('9d Ks Qs Js Ts') twop = Hand.fromString('As 2c Ks Ah Kc') pearA_962 = Hand.fromString('As Ah 9s 6d 2c') pearA_952 = Hand.fromString('As Ah 9s 5d 2c') hi = Hand.fromString('As Kh 9s 6d 2c') wheel = Hand.fromString('As 5h 4s 3d 2c') rank5(royal) rank5(quads) rank5(full) rank5(flush) rank5(str8) rank5(trip2sAK) rank5(trip2sAQ) rank5(twop) rank5(pearA_962) rank5(hi) rank5(wheel, loud=True)