Let's play some poker
packages = [
"numpy"
]
player1
player2
Board
import numpy as np
##---------------Build Deck and Deal---------------------------##
def shuffle_deck():
#Build Deck of 52 Cards
deck=[]
for suit in ['C', 'S', 'H', 'D']:
for rank in ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']:
deck.append(rank+suit)
#Shuffles Deck of 52 Cards
np.random.shuffle(deck)
return deck
def deal_turn(deck):
deal_array = deck.pop(0)
return deal_array
def deal_river(deck):
deal_array = deck.pop(0)
return deal_array
def deal_hand(deck):
deal_array = [deck.pop(0) , deck.pop(0)]
return deal_array
def deal_floop(deck):
deal_array = [deck.pop(0) , deck.pop(0), deck.pop(0)]
return deal_array
##---------------Check for winner--------------------------##
def winner(hands):
"Return the best hand: poker([hand,...]) => hand"
return max(hands, key=hand_rank)
def hand_rank(hand):
ranks = card_ranks(hand)
if straight(ranks) and flush(hand): # straight flush
return (8, max(ranks))
elif kind(4, ranks): # 4 of a kind
return (7, kind(4, ranks), kind(1, ranks))
elif kind(3, ranks) and kind(2, ranks): # full house
return (6,kind(3,ranks),kind(2,ranks))
elif flush(hand): # flush
return (5,ranks)
elif straight(ranks): # straight
return (4,max(ranks))
elif kind(3, ranks): # 3 of a kind
return (3,kind(3,ranks),ranks)
elif two_pair(ranks): # 2 pair
return (2,two_pair(ranks),ranks)
elif kind(2, ranks): # kind
return (1,kind(2,ranks),ranks)
else: # high card
return (0,ranks)
def card_ranks(cards):
"Return a list of the ranks,sorted with higher first"
ranks = ['--23456789TJQKA'.index(r) for r,s in cards]
ranks.sort(reverse = True)
return ranks
def straight(ranks):
"Return True if the ordered ranks from a 5-card straight"
return (max(ranks)-min(ranks) == 4) and len(set(ranks)) ==5
def flush(hand):
"Return True if all the cards have the same suit"
suits = [s for r,s in hand]
return len(set(suits)) == 1
def kind(n,ranks):
for r in ranks:
if ranks.count(r) == n: return r
return None
def two_pair(ranks):
pair = kind(2,ranks)
lowpair = kind(2,list(reversed(ranks)))
#print (pair and lowpair)
if pair and lowpair !=pair:
return (pair,lowpair)
else:
return None
#----------------Adding Players-------------------#
def ai_player_hand(deck):
hand = deal_hand(deck)
return hand
def game_board(deck):
board = deal_floop(deck)
board = np.append(board, deal_turn(deck))
board = np.append(board, deal_turn(deck))
return board
#----------------TESTING-------------------#
#Deal's Full hand of the player
roal = ['AD', 'TD', 'QD', 'KD', 'JD']
fk = "9D 9H 9S 9C 7D".split()
deck = shuffle_deck()
ai1 = ai_player_hand(deck)
ai2 = ai_player_hand(deck)
game = game_board(deck)
ai1b = np.append(ai1, game)
ai2b = np.append(ai2, game)
whowon = winner([ai1b, ai2b])
Element("ai1").write(str(ai1))
Element("ai2").write(str(ai2))
Element("game").write(str(game))
def areEqual(arr1, arr2, N, M):
# If lengths of array are not
# equal means array are not equal
if (N != M):
return False
# Sort both arrays
arr1.sort()
arr2.sort()
# Linearly compare elements
for i in range(0, N):
if (arr1[i] != arr2[i]):
return False
# If all elements were same.
return True
if areEqual(whowon, ai1b, 7, 7):
Element("whowonplayer").write(str("Player 1 Wins"))
else:
Element("whowonplayer").write(str("Player 2 Wins"))
Element("whowonhand").write(str(whowon))
#player1 = deal_hand(deck)
#player1 = np.append(player1, deal_floop(deck))
#player1 = np.append(player1, deal_turn(deck))
#player1 = np.append(player1, deal_river(deck))
#print(winner([roal,fk]))