Advertisement
tomophilia

Texas Hold 'Em

Aug 30th, 2018
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.94 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Aug  2 20:59:43 2018
  5.  
  6. @author: tomophilia
  7. """
  8.  
  9. #!/usr/bin/env python3
  10. # -*- coding: utf-8 -*-
  11. """
  12. Created on Mon Mar 12 02:04:37 2018
  13.  
  14. @author: tomophilia
  15. """
  16.  
  17. import collections
  18. from random import choice, shuffle
  19. from itertools import combinations
  20. from functools import reduce
  21. '''Create a card tuple'''
  22. Card = collections.namedtuple('Card', ['rank', 'suit'])
  23.  
  24. '''create a deck, using the card tuple. this was taken from Fluent Python. '''
  25. class FrenchDeck():
  26.     ranks = [str(n) for n in range(2, 11)] + list('JQKA')
  27.     suits = 'spades diamonds clubs hearts'.split()
  28.  
  29.     def __init__(self):
  30.         self.cards = [Card(rank, suit) for rank in self.ranks for suit in self.suits]
  31.  
  32.     def __len__(self):
  33.         return len(self.cards)
  34.  
  35.     def __getitem__(self, position):
  36.         return self.cards[position]
  37.  
  38.  
  39. deck = list(FrenchDeck())
  40.  
  41.  
  42. class Player():
  43.     '''Create a player and various methods for the player.'''
  44.     def __init__(self, name, hand, bankroll,bet):
  45.         self.name = name
  46.         self.hand = hand
  47.         self.bankroll = bankroll
  48.         self.action = False
  49.         self.winner = False
  50.         self.live_hand = False
  51.         self.bet = bet
  52.     def place_bet(self,bet):
  53.         '''place a bet'''
  54.         self.bankroll-=bet
  55.         print(str(self.name) + ' bets ' + str(bet)+'\n'+'...his stack is now '+str(self.bankroll))
  56.         self.bet=bet
  57.  
  58.     def final_hand_met(self, hand, board):
  59.         '''create a function that will return the best five card hand from the board and players cards'''
  60.         all_nine_cards = self.hand+board
  61.         self.final_hand = [0, []]
  62.         combos = combinations(all_nine_cards, 5)
  63.         for i in combos:
  64.             l = rank_the_hand(i)
  65.             if l[0] > self.final_hand[0]:
  66.                 self.final_hand = l
  67.             elif l[0] == self.final_hand[0]:
  68.                 for j in range(len(self.final_hand[1])):
  69.                     if l[1][j] > self.final_hand[1][j]:
  70.                         self.final_hand = l
  71.                     else:
  72.                         continue
  73.             else:
  74.                 continue
  75.             #muck.append(i.hand)
  76.         self.hand = []
  77.         return self.final_hand
  78.     def identify_the_hand(self,final_hand):
  79.         '''write a function to return a string that will say flush, straight, two pair, etc.
  80.         '''
  81.         hand_text = ''
  82.         new_hand_list=[]
  83.         for i in self.final_hand[1]:
  84.             if i==11:
  85.                 new_hand_list.append('Jack')
  86.             elif i==12:
  87.                 new_hand_list.append('Queen')
  88.             elif i ==13:
  89.                 new_hand_list.append('King')
  90.             elif i==14:
  91.                 new_hand_list.append('Ace')
  92.             else:
  93.                 new_hand_list.append(str(i))
  94.         self.final_hand.remove(self.final_hand[1])
  95.         self.final_hand.append(new_hand_list)
  96.         #letter_cards={'J':11,'Q':12,'K':13,'A':14}
  97.         print(final_hand)
  98.         if final_hand[0]==8:
  99.             hand_text=str(final_hand[1][0])+' high straight flush. '
  100.         elif final_hand[0]==7:
  101.             hand_text='Quad '+str(final_hand[1][0])+'s'
  102.         elif final_hand[0]==6:
  103.             hand_text='Full House - '+str(final_hand[1][0])+'s'+' over.'
  104.         elif final_hand[0]==5:
  105.             hand_text= str(final_hand[1][0])+' high flush. '
  106.         elif final_hand[0]==4:
  107.             hand_text=str(final_hand[1][0])+' high straight. '
  108.         elif final_hand[0]==3:
  109.             hand_text='Three ' + str(final_hand[1][0])+'s'
  110.         elif final_hand[0]==2:
  111.             hand_text= 'Two pair. '+str(final_hand[1][0])+'s'+' and '+ str(final_hand[1][1])+'s'
  112.         elif final_hand[0]==1:
  113.             hand_text=' a pair of '+str(final_hand[1][0])+'s'
  114.         else:
  115.             try:
  116.                 hand_text=str(final_hand[1][0])+' high junk'
  117.             except:
  118.                 hand_text='high card'
  119.         return hand_text
  120.    
  121.     def deal_a_hand(self, dadeck, howmanycards):
  122.         self.hand = []
  123.         self.live_hand = True
  124.         '''Deal a random hand of n length and remove those cards from the deck.'''
  125.         for i in range(howmanycards):
  126.             z = choice(dadeck)
  127.             self.hand.append(z)
  128.             dadeck.remove(z)
  129.         return self.hand
  130.  
  131.  
  132. def rank_the_hand(cards):
  133.     '''create a list object for evaluating a winner. '''
  134.     wheel = [14,5,4,3,2]
  135.     straight_id = [14,13,12,11,10,9,8,7,6,5,4,3,2,1]
  136.     Flush = False
  137.     Straight = False
  138.     '''Final_hand variable returns the hand type i.e. straight flush(8), quads(7) or full house(6) etc. As well as a second list to store other info.  '''
  139.     final_hand = [0,[]]
  140.     '''List of card ranks - Ace, King, 7, 3, 7 etc. (No suits) '''
  141.     card_ranks = [card.rank for card in cards]
  142.     '''List of card suits.'''
  143.     card_suits = [card.suit for card in cards]
  144.     if len(list(set(card_suits))) == 1:
  145.         Flush = True
  146.         #print(Flush)
  147.         #print(card_suits)
  148.         '''Convert the ranks to numbers.'''
  149.     for i in range(len(card_ranks)):
  150.       try:
  151.         card_ranks[i] = int(card_ranks[i])
  152.       except:
  153.         if card_ranks[i] == 'J':
  154.           card_ranks[i] = 11
  155.         elif card_ranks[i] == 'Q':
  156.           card_ranks[i] = 12
  157.         elif card_ranks[i] == 'K':
  158.           card_ranks[i] = 13
  159.         elif card_ranks[i] == 'A':
  160.           card_ranks[i] = 14
  161.     card_ranks = sorted(card_ranks, reverse=True)
  162.     bernanners = collections.Counter(card_ranks)
  163.     counts = bernanners.most_common(3)
  164.     if len(set(card_ranks)) == 2:
  165.       '''Four of a Kind. '''
  166.       if counts[0][1] == 4:
  167.         final_hand[0] = 7
  168.         final_hand[1].append(counts[0][0])
  169.       else:
  170.         '''Full House. '''
  171.         final_hand[0] = 6
  172.         final_hand[1].append(counts[0][0])
  173.     elif len(set(card_ranks)) == 3:
  174.       if counts[0][1] == 3:
  175.         #print('''Three of a Kind. ''')
  176.         final_hand[0] = 3
  177.         final_hand[1].append(counts[0][0])
  178.         final_hand[1].append(card_ranks[3])
  179.         final_hand[1].append(card_ranks[4])
  180.       else:
  181.         #print('''Two Pair''')
  182.         final_hand[0] = 2
  183.         final_hand[1].append(counts[1][0])
  184.         final_hand[1].append(counts[0][0])
  185.         final_hand[1] = sorted(final_hand[1], reverse=True)
  186.         final_hand[1].append(counts[2][0])
  187.     elif len(list(set(card_ranks))) == 4:
  188.       '''One Pair.'''
  189.       #print('One Pair')
  190.       final_hand[0] = 1
  191.       final_hand[1].append(card_ranks[0])
  192.       final_hand[1].append(card_ranks[2])
  193.       final_hand[1].append(card_ranks[3])
  194.       final_hand[1].append(card_ranks[4])
  195.     else:
  196.       '''All five cards different- Nothing, Straight, Flush or, Straight Flush.  '''
  197.       '''card_ranks[0] - card_ranks[4]'''
  198.       for i in range(len(straight_id)):
  199.         if straight_id[i:i + 5] == card_ranks or card_ranks == wheel:
  200.           Straight = True
  201.         else:
  202.           continue
  203.       if Flush == True and Straight == True:
  204.         #print('Straight Flush')
  205.         final_hand[0] = 8
  206.         if card_ranks == wheel:
  207.           final_hand[1].append(card_ranks[1])
  208.         else:
  209.           final_hand[1].append(card_ranks[0])
  210.       else:
  211.         if Flush == True:
  212.           #print('''Flush''')
  213.           final_hand[0] = 5
  214.           final_hand[1].append(card_ranks[0])
  215.           final_hand[1].append(card_ranks[1])
  216.           final_hand[1].append(card_ranks[2])
  217.           final_hand[1].append(card_ranks[3])
  218.           final_hand[1].append(card_ranks[4])
  219.         elif Straight == True:
  220.           #print("""Straight""")
  221.           final_hand[0] = 4
  222.           if card_ranks == wheel:
  223.             final_hand[1].append(card_ranks[1])
  224.           else:
  225.             final_hand[1].append(card_ranks[0])
  226.         else:
  227.           '''Interpret the high card.  '''
  228.           #print('High Card')
  229.           final_hand[0] = 0
  230.           final_hand[1].append(card_ranks[0])
  231.           final_hand[1].append(card_ranks[1])
  232.           final_hand[1].append(card_ranks[2])
  233.           final_hand[1].append(card_ranks[3])
  234.           final_hand[1].append(card_ranks[4])
  235.     return final_hand
  236.  
  237.  
  238. def winner(p1, p2,board):
  239.     '''return a winner from two hands'''
  240.     if p1.final_hand[0] > p2.final_hand[0]:
  241.       return p1
  242.     elif p2.final_hand[0] > p1.final_hand[0]:
  243.       return p2
  244.     else:
  245.       for i in range(len(p1.final_hand[1])):
  246.         if p1.final_hand[1][i] > p2.final_hand[1][i]:
  247.             return p1
  248.         elif p2.final_hand[1][i] > p1.final_hand[1][i]:
  249.             return p2
  250.         else:
  251.           tie = [p1, p2]
  252.           print(' Tie. ')
  253.           return tie
  254. def winner_sequel(p1, p2):
  255.     # print('Lets see all hands, please. ')
  256.     if p1[0] > p2[0]:
  257.         #print(p1.name+ ' wins. ')
  258.         return p1
  259.     elif p2[0] > p1[0]:
  260.         #print(p2.hand+' wins. ')
  261.         return p2
  262.     else:
  263.         for i in range(len(p1[1])):
  264.             if p1[1][i] > p2[1][i]:
  265.                 return p1
  266.             elif p2[1][i] > p1[1][i]:
  267.                 return p2
  268.             else:
  269.                 print(p1)
  270.                 print(p2)
  271.                 tie = [p1, p2]
  272.                 #print('Split pot. ')
  273.                 return p1
  274.  
  275. def gather_pot(table):
  276.     '''collect bets from players after round of betting. '''
  277.     pot = 0
  278.     for player in table:
  279.         pot += player.bet
  280.         player.bet = 0
  281.     return pot
  282.        
  283.            
  284. def callRaiseFold(player,previous_bet,muck):
  285.     while True:
  286.         if player.live_hand==True and previous_bet==0:
  287.             choice=input((str(player.name)+' check or bet, please. '))
  288.             #check or bet function
  289.             if choice == 'bet':
  290.                 previous_bet=int(input(' How much '+str(player.name)+'?'))
  291.                 player.place_bet(previous_bet)
  292.                 break
  293.             elif choice=='check':
  294.                 break
  295.         elif player.live_hand==True and previous_bet>0:
  296.             choice=input(str(player.name)+' call '+str(previous_bet)+', raise or fold. ')
  297.             if choice == 'raise':
  298.                 pre_bet = previous_bet
  299.                 previous_bet = int(input(' Raise to what?'))
  300.                 while pre_bet>=previous_bet:
  301.                     print('You have to bet more')
  302.                     previous_bet = int(input(' Raise to what?'))
  303.                 player.place_bet(previous_bet)
  304.                 break
  305.             elif choice=='fold':
  306.                 muck+=player.hand
  307.                 player.hand=[]
  308.                 player.live_hand=False
  309.                 break
  310.                 #call raise or fold function
  311.             elif choice=='call':
  312.                 '''place the player difference in calling and raising here'''
  313.                 player.bankroll+=player.bet
  314.                 player.place_bet(previous_bet)
  315.                 break
  316.         elif player.live_hand==False:
  317.             break
  318.            
  319.             print('Error somewheres. ')
  320.             break
  321.             continue
  322.         else:
  323.             print('new error somehow. ')
  324.     return previous_bet
  325. straight=rank_the_hand([Card(rank='7',suit='clubs'),Card(rank='8',suit='hearts'),Card(rank='9',suit='spades'),Card(rank='10',suit='clubs'),Card(rank='J',suit='spades')])
  326.  
  327. '''some hands to play with. '''
  328. two_pair = rank_the_hand([Card(rank='J',suit='spades'),Card(rank='J',suit='clubs'),Card(rank='A',suit='diamonds'),Card(rank='A',suit='hearts'),Card(rank='7',suit='spades')])
  329. steelwheel = rank_the_hand([Card(rank='5', suit='hearts'), Card(rank='4', suit='hearts'), Card(rank='3', suit='hearts'),
  330.                             Card(rank='2', suit='hearts'), Card(rank='A', suit='hearts')])
  331. fourofakind = rank_the_hand([Card(rank='A', suit='clubs'), Card(rank='A', suit='hearts'), Card(rank='A', suit='spades'),
  332.                              Card(rank='A', suit='diamonds'), Card(rank='2', suit='hearts')])
  333. fullhouse = rank_the_hand([Card(rank='Q', suit='clubs'), Card(rank='Q', suit='hearts'), Card(rank='Q', suit='diamonds'),
  334.                            Card(rank='2', suit='spades'), Card(rank='2', suit='hearts')])
  335. flushy = rank_the_hand([Card(rank='K', suit='hearts'), Card(rank='5', suit='hearts'), Card(rank='4', suit='hearts'),
  336.                        Card(rank='J', suit='hearts'), Card(rank='7', suit='hearts')])
  337.  
  338. Rocky = Player('Rocky', [], 100,0)
  339. #Rocky.deal_a_hand(deck, 2)
  340. Todd = Player('Todd', [], 100,0)
  341. #Todd.deal_a_hand(deck, 2)
  342. Dave = Player('Dave', [], 100,0)
  343. #Dave.deal_a_hand(deck, 2)
  344. Myrna = Player('Myrna', [], 100,0)
  345. #Myrna.deal_a_hand(deck, 2)
  346. Tom = Player('Tom', [], 100,0)
  347. #Tom.deal_a_hand(deck, 2)
  348. Larry = Player('Larry', [], 100,0)
  349. #Larry.deal_a_hand(deck, 2)
  350.  
  351. table = [Rocky, Todd, Dave, Myrna, Tom, Larry]
  352. muck = []
  353.  
  354. button = 0
  355.  
  356.  
  357. def action_round(table,button,bet,muck,pot):
  358.     '''A round of betting. '''
  359.     for player in table:
  360.         player.action=False
  361.     print('Round of betting. Action starts here. ')
  362.     action_on_you = button+1
  363.     all_acted_in_table=[i.action for i in table if i.live_hand==True]
  364.     new_bet=bet
  365.     laMesa=table[action_on_you:]+table[:action_on_you]
  366.     while all(all_acted_in_table)==False:
  367.         #print(all(all_acted_in_table))
  368.         for i in laMesa:
  369.             all_acted_in_table=[i.action for i in table if i.live_hand==True]
  370.             if all(all_acted_in_table)==True:
  371.                 break
  372.             #print(all_acted_in_table)
  373.             new_bet=bet
  374.             i.action=True
  375.             bet=callRaiseFold(i,bet,muck)
  376.             if bet>new_bet:
  377.                 for d in laMesa:
  378.                     if d.name==i.name:
  379.                         i.action=True
  380.                     else:
  381.                         d.action=False
  382.             else:
  383.                 continue
  384.     for i in laMesa:
  385.         pot+=i.bet
  386.         i.bet=0
  387.         i.action=False
  388.     print('All bets in. '+str(pot)+' dollars added to the pot.  ')
  389.     #print(str(len(muck))+' cards in muck. ')
  390.     return pot
  391.  
  392.  
  393.  
  394. def handOPoker(button, table,deck):
  395.     '''one hand from dealing a hand, flop,turn,river,payout and button move'''
  396.     board = []
  397.     muck = []
  398.     pot=0
  399.     final_pot=0
  400.     after_button=button+1
  401.     for player in table:
  402.         '''move the deal a hand loop to happen after blinds are placed.  '''
  403.         player.deal_a_hand(deck, 2)
  404.     '''big blind dollar amount'''
  405.     big_blind_temp = 2
  406.     small_blind_temp = 1
  407.     '''big blind player seat'''
  408.     big_blind = button + 2
  409.     small_blind = big_blind - 1
  410.     table[small_blind].place_bet(small_blind_temp)
  411.     print(str(table[small_blind].name)+' places '+str(small_blind_temp) +' for a small blind')
  412.     table[big_blind].place_bet(big_blind_temp)
  413.     print(str(table[big_blind].name)+' places '+str(big_blind_temp) + ' for a big blind')
  414.     '''first action after big blind'''
  415.     first_act=button+2
  416.     '''Round of betting pre flop'''
  417.     print('\n Your hand is...')
  418.     print(Tom.hand)
  419.     bet=0
  420.     table_post_blind=table[first_act:]+table[:first_act]
  421.     final_pot+=action_round(table_post_blind,button,big_blind_temp,muck,pot)
  422.     bet=0
  423.     print('The working pot is '+str(final_pot)+' dollars. ')
  424.     '''da flop'''
  425.     for i in range(3):
  426.         z = choice(deck)
  427.         deck.remove(z)
  428.         board.append(z)
  429.     print('The Flop \n')
  430.     print(board)
  431.     '''before the turn'''
  432.     print('\n Your hand is...')
  433.     print(Tom.hand)
  434.     bet = 0
  435.     final_pot += action_round(table,button,bet,muck,pot)
  436.     bet = 0
  437.     print('The working pot is '+str(final_pot)+' dollars. ')
  438.     '''the turn'''
  439.     turn = choice(deck)
  440.     deck.remove(turn)
  441.     board.append(turn)
  442.     print('The Turn. ')
  443.     print(board)
  444.     '''round of betting'''
  445.     bet = 0
  446.     final_pot+=action_round(table,button,bet,muck,pot)
  447.     '''the river'''
  448.     river = choice(deck)
  449.     deck.remove(river)
  450.     board.append(river)
  451.     print('The River. ')
  452.     print(board)
  453.     '''last round of betting'''
  454.     bet = 0
  455.     final_pot += action_round(table,button,bet,muck,pot)
  456.     bet = 0
  457.     '''Lets see all hands please
  458.    combine the players cards with the board. iterate over all combinations of five cards to get their best hand.'''
  459.     print('There are '+str(final_pot)+(' dollars in the pot. '))
  460.     winner=[i for i in table if i.live_hand==True]
  461.     winner =list(winner)
  462.     temp_hand=[0,[0,0,0]]
  463.     for i in winner:
  464.         i.final_hand_met(i.hand,board)
  465.         print(str(i.name)+'s'+ ' hand is '+'\n')
  466.         print(i.final_hand)
  467.         print('\n')
  468.         temp_hand=winner_sequel(i.final_hand,temp_hand)
  469.         print('The winning hand is '+ str(temp_hand))
  470.     confirmed_win_list=[i for i in table if i.live_hand==True and i.final_hand==temp_hand]
  471.     winnum=len(confirmed_win_list)
  472.     for i in winner:
  473.         if i.final_hand==temp_hand:
  474.             print('We have a winner. ')
  475.             i.bankroll+=final_pot*(1/winnum)
  476.             print(str(i.name)+' wins '+' with '+ str(i.identify_the_hand(i.final_hand)))
  477.             print(i.bankroll)
  478.         else:
  479.             continue
  480.     deck+=muck
  481.     print('cards in deck: '+str(len(deck)))
  482.     print('Button is: '+str(button))
  483.     return button
  484.  
  485. Tray = Player('Tray',[], 100,0)
  486. Tray.deal_a_hand(deck, 2)
  487. test_board=[Card(rank='4',suit='hearts'),Card(rank='4',suit='spades'),Card(rank='K',suit='hearts')]
  488. steelwheel_almost = [Card(rank='5', suit='hearts'), Card(rank='8', suit='spades'), Card(rank='3', suit='hearts'),
  489.                             Card(rank='2', suit='hearts'), Card(rank='A', suit='hearts')]
  490. Tray.hand=steelwheel_almost
  491.  
  492. print(Tray.final_hand_met(Tray.hand,test_board))
  493. solid = Player('Solid_1',[],100,0)
  494. solid.deal_a_hand(deck,2)
  495. liquid = Player('Liquid_2',[],100,0)
  496. liquid.deal_a_hand(deck,2)
  497. solidus = Player('Solidus_3',[],100,0)
  498. solidus.deal_a_hand(deck,2)
  499. ocelot = Player('Ocelot_4',[],50,0)
  500. ocelot.deal_a_hand(deck,2)
  501.  
  502. dietisch =[solid,liquid,solidus,ocelot]
  503. all_bets_in = all([i.bet for i in table if i.live_hand])
  504.  
  505. actionyyy = False
  506. bet = 0
  507. pot=0
  508. #handOPoker(button,table)
  509. #whileaction(dietisch,3,pot)
  510. flop_turn_river= [choice(deck) for i in range(5)]
  511. #print(flop_turn_river)
  512. solid.final_hand_met(solid.hand,flop_turn_river)
  513. liquid.final_hand_met(liquid.hand,flop_turn_river)
  514. solidus.final_hand_met(solidus.hand,flop_turn_river)
  515. ocelot.final_hand_met(ocelot.hand,flop_turn_river)
  516.  
  517. #print(winner_sequel(solid.final_hand,ocelot.final_hand))
  518. #print(solid.final_hand_met(solid.hand,flop_turn_river))
  519. #c=reduce(winner(solid,liquid,flop_turn_river),dietisch)
  520. def game_of_poker(table):
  521.     game=True
  522.     button=0
  523.     while game==True:
  524.         deck=list(FrenchDeck())
  525.         handOPoker(button,table,deck)
  526.         button+=1
  527.         another_hand=input('Do you wish to play another hand? ')
  528.         if another_hand=='yes':
  529.             continue
  530.         elif game=='no':
  531.             game=False
  532.         else:
  533.             continue
  534.         if button>=len(table):
  535.             button=0
  536.         else:
  537.             continue
  538. game_of_poker(table)
  539. #action_round(dietisch,0,0,muck,pot)
  540. finale=0
  541. '''
  542. for i in range(2):
  543.     butt=1
  544.     finale=action_round(dietisch,butt,bet,muck,pot)
  545.     butt+=1
  546.  
  547. '''
  548.  
  549.            
  550.            
  551.        
  552.  
  553. Von meinem iPhone gesendet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement