Guest User

HoldEm.py

a guest
May 29th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from Poker.player import *
  4. import sys
  5. from random import shuffle
  6. from itertools import product
  7.  
  8. values = {1:'Ace', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'10', 11:'Jack', 12:'Queen', 13:'King', 14:'Ace'}
  9. suits = {1:'clubs', 2:'diamonds', 3:'hearts', 4:'spades'}
  10. hands_name = {0:'High card', 1:'Pair', 2:'Two pairs', 3:'Three of a kind', 4:'Straight', 5:'Flush', 6:'Full house', 7:'Four of a kind', 8:'Straight flush'}
  11.  
  12. #Print the card tuple in a human-readable manner
  13. def printCard(card):
  14.     print(values[card[0]], 'of', suits[card[1]], end='')
  15.  
  16. #Print the cards on the table (Flop, Turn and River)
  17. def printTable(table):
  18.     print('On the table: ', end='')
  19.     for i in range(len(table)-1):
  20.         printCard(table[i]), print(', ', end='')
  21.     printCard(table[-1]), print()
  22.  
  23. #52 cards deck generator
  24. def create_deck():
  25.     num = list(range(1,14))
  26.     suits = list(range(1,5))
  27.     deck = list(product(num, suits))
  28.     shuffle(deck)
  29.     return deck
  30.  
  31. #Select the winner from a list of Player
  32. #Return a list of indices of the winning Player(s)
  33. #   followed by a 3-tuple of the hand, the highest
  34. #   card and a score for ties
  35. def select_winner(player, table):
  36.     res = list()
  37.     for i in range(len(player)):
  38.         res.append((player[i].place, player[i].best_hand(table)))
  39.  
  40.     #We keep only the best hands
  41.     res = [elem for elem in res if elem[1][0] == max(res, key=lambda x: x[1][0])[1][0]]
  42.  
  43.     if len(res) == 1:
  44.         return res
  45.  
  46.  
  47.     #If there's multiple best hands, we keep only the highest card
  48.     res = [elem for elem in res if elem[1][1] == max(res, key=lambda x: x[1][1])[1][1]]
  49.  
  50.     if len(res) == 1:
  51.         return res
  52.  
  53.     #If there's still the same highest ranking card, we keep the highest ticker score
  54.     res = [elem for elem in res if elem[1][2] == max(res, key=lambda x: x[1][2])[1][2]]
  55.  
  56.     #We return the result.
  57.     #It's up to the calling function to check if there's still a tie
  58.     return res
  59.  
  60.  
  61. #Distribute cards from a 52 cards deck
  62. def main():
  63.     try:
  64.         nbr_players = int(input('How many players (2 to 8)? :\n'))
  65.     except ValueError:
  66.         print('Not a number')
  67.  
  68.     if not(2 <= nbr_players <= 8):
  69.         print('There must be between 2 and 8 players')
  70.         sys.exit()
  71.  
  72.     #Create a new deck
  73.     deck = create_deck()
  74.  
  75.     #Create a list of all players
  76.     players = list()
  77.     for i in range(nbr_players):
  78.         players.append( Player(i, nbr_players, [deck.pop(), deck.pop()]) )
  79.  
  80.     #List of players in the game
  81.     playing = list(players)
  82.  
  83.     #Print the cards distributed to the players
  84.     print('Your cards : ', end=''), printCard(players[0].hand[0]), print(', ', end=''), printCard(players[0].hand[1]), print()
  85.     for i in range(1, nbr_players):
  86.         print('CPU ', players[i].place, ': ', end=''), printCard(players[i].hand[0]), print(', ', end=''), printCard(players[i].hand[1]), print()
  87.  
  88.     #Cards on the table
  89.     table = list()
  90.  
  91.     #Draw the Flop
  92.     deck.pop()
  93.     for _ in range(3):
  94.         table.append(deck.pop())
  95.     print('\nThe Flop:')
  96.     printTable(table), print()
  97.  
  98.     #First chance to fold
  99.     for i in range(1, len(playing)):
  100.         if playing[i].fold(table):
  101.             print('CPU', playing[i].place, 'folds!')
  102.  
  103.     playing = [elem for elem in playing if elem.ingame == True]
  104.  
  105.     #Draw the Turn
  106.     deck.pop()
  107.     table.append(deck.pop())
  108.     print('Turn: ', end=''), printCard(table[-1]), print()
  109.     printTable(table), print()
  110.  
  111.     #Second chance to fold
  112.     for i in range(1, len(playing)):
  113.         if playing[i].fold(table):
  114.             print('CPU', playing[i].place, 'folds!')
  115.  
  116.     playing = [elem for elem in playing if elem.ingame == True]
  117.  
  118.     #Draw the River
  119.     deck.pop()
  120.     table.append(deck.pop())
  121.     print('River: ', end=''), printCard(table[-1]), print()
  122.     printTable(table), print()
  123.  
  124.     '''
  125.    for i in range(len(playing)):
  126.        hand = playing[i].best_hand(table)
  127.        print('Player', playing[i].place, ':', hands_name[hand[0]])
  128.    '''
  129.  
  130.     #Select winner from playing
  131.     print('Winning hand(s):')
  132.     winner = select_winner(playing, table)
  133.     if len(winner) > 1:
  134.         print('There\'s a tie!')
  135.     for i in winner:
  136.         if i[0] == 0:
  137.             print('You win with:', hands_name[i[1][0]], 'of', values[i[1][1]])
  138.         else:
  139.             print('CPU', i[0], 'win with:', hands_name[i[1][0]], 'of', values[i[1][1]])
  140.  
  141.     print()
  142.     #Select best hand from all players
  143.     print('Best hand(s):')
  144.     winner = select_winner(players, table)
  145.     if len(winner) > 1:
  146.         print('There\'s a tie!')
  147.     for i in winner:
  148.         if i[0] == 0:
  149.             print('You should have won with:', hands_name[i[1][0]], 'of', values[i[1][1]])
  150.         else:
  151.             print('CPU', i[0], 'should have won with:', hands_name[i[1][0]], 'of', values[i[1][1]])
  152.  
  153.  
  154. if __name__ == '__main__':
  155.     main()
Advertisement
Add Comment
Please, Sign In to add comment