Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from Poker.player import *
- import sys
- from random import shuffle
- from itertools import product
- 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'}
- suits = {1:'clubs', 2:'diamonds', 3:'hearts', 4:'spades'}
- 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'}
- #Print the card tuple in a human-readable manner
- def printCard(card):
- print(values[card[0]], 'of', suits[card[1]], end='')
- #Print the cards on the table (Flop, Turn and River)
- def printTable(table):
- print('On the table: ', end='')
- for i in range(len(table)-1):
- printCard(table[i]), print(', ', end='')
- printCard(table[-1]), print()
- #52 cards deck generator
- def create_deck():
- num = list(range(1,14))
- suits = list(range(1,5))
- deck = list(product(num, suits))
- shuffle(deck)
- return deck
- #Select the winner from a list of Player
- #Return a list of indices of the winning Player(s)
- # followed by a 3-tuple of the hand, the highest
- # card and a score for ties
- def select_winner(player, table):
- res = list()
- for i in range(len(player)):
- res.append((player[i].place, player[i].best_hand(table)))
- #We keep only the best hands
- res = [elem for elem in res if elem[1][0] == max(res, key=lambda x: x[1][0])[1][0]]
- if len(res) == 1:
- return res
- #If there's multiple best hands, we keep only the highest card
- res = [elem for elem in res if elem[1][1] == max(res, key=lambda x: x[1][1])[1][1]]
- if len(res) == 1:
- return res
- #If there's still the same highest ranking card, we keep the highest ticker score
- res = [elem for elem in res if elem[1][2] == max(res, key=lambda x: x[1][2])[1][2]]
- #We return the result.
- #It's up to the calling function to check if there's still a tie
- return res
- #Distribute cards from a 52 cards deck
- def main():
- try:
- nbr_players = int(input('How many players (2 to 8)? :\n'))
- except ValueError:
- print('Not a number')
- if not(2 <= nbr_players <= 8):
- print('There must be between 2 and 8 players')
- sys.exit()
- #Create a new deck
- deck = create_deck()
- #Create a list of all players
- players = list()
- for i in range(nbr_players):
- players.append( Player(i, nbr_players, [deck.pop(), deck.pop()]) )
- #List of players in the game
- playing = list(players)
- #Print the cards distributed to the players
- print('Your cards : ', end=''), printCard(players[0].hand[0]), print(', ', end=''), printCard(players[0].hand[1]), print()
- for i in range(1, nbr_players):
- print('CPU ', players[i].place, ': ', end=''), printCard(players[i].hand[0]), print(', ', end=''), printCard(players[i].hand[1]), print()
- #Cards on the table
- table = list()
- #Draw the Flop
- deck.pop()
- for _ in range(3):
- table.append(deck.pop())
- print('\nThe Flop:')
- printTable(table), print()
- #First chance to fold
- for i in range(1, len(playing)):
- if playing[i].fold(table):
- print('CPU', playing[i].place, 'folds!')
- playing = [elem for elem in playing if elem.ingame == True]
- #Draw the Turn
- deck.pop()
- table.append(deck.pop())
- print('Turn: ', end=''), printCard(table[-1]), print()
- printTable(table), print()
- #Second chance to fold
- for i in range(1, len(playing)):
- if playing[i].fold(table):
- print('CPU', playing[i].place, 'folds!')
- playing = [elem for elem in playing if elem.ingame == True]
- #Draw the River
- deck.pop()
- table.append(deck.pop())
- print('River: ', end=''), printCard(table[-1]), print()
- printTable(table), print()
- '''
- for i in range(len(playing)):
- hand = playing[i].best_hand(table)
- print('Player', playing[i].place, ':', hands_name[hand[0]])
- '''
- #Select winner from playing
- print('Winning hand(s):')
- winner = select_winner(playing, table)
- if len(winner) > 1:
- print('There\'s a tie!')
- for i in winner:
- if i[0] == 0:
- print('You win with:', hands_name[i[1][0]], 'of', values[i[1][1]])
- else:
- print('CPU', i[0], 'win with:', hands_name[i[1][0]], 'of', values[i[1][1]])
- print()
- #Select best hand from all players
- print('Best hand(s):')
- winner = select_winner(players, table)
- if len(winner) > 1:
- print('There\'s a tie!')
- for i in winner:
- if i[0] == 0:
- print('You should have won with:', hands_name[i[1][0]], 'of', values[i[1][1]])
- else:
- print('CPU', i[0], 'should have won with:', hands_name[i[1][0]], 'of', values[i[1][1]])
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment