I'm writting this poker hand evaluator and it's really coming along. But, as you can see I repeat myself many times and I would love to know what's your thought process when you're repeating yourself? Other than this, it's not finished. I still need to account for high cards etc. But you get the jist of it. I'm really working hard on this one and I hope it shows.
Please be blunt! Take it out on me today. :)
The program is split into two files:
analyzer.py and goodhands.py in that order.
This is the main program for running the analyzer: analyzer.py
"""Poker hand probalities"""
hands = 0
while True:
from goodhands import *
import random
from time import sleep
from collections import Counter
def deck():
"""Makes a list of 52 shuffled cards """
suits = "♣♦♥♠"
letts = "JQKA"
specials = [char + suit for suit in suits for char in letts]
numbers = [str(num) + suit for suit in suits for num in range(2, 11)]
deck = numbers + specials
shuffled = random.sample(deck, len(deck))
return shuffled
win_deck_check = deck()[:]
deck = deck()
def players():
"""Make a dictionary of players to store the values of cards"""
players = {"p" + str(num):None for num in range(1, 10)}
return players
players = players()
def dealer(deck):
"""Assign one card per player for two turns for a total of two cards"""
CARD_COUNT = 0
deck.pop(CARD_COUNT) # Discard the first card of the deck.
deal = True
table = {}
while deal:
for i in players:
players[i] = deck[CARD_COUNT] # Assign the first card.
CARD_COUNT += 1
if CARD_COUNT == len(players):
for i in players:
players[i] = players[i] + deck[CARD_COUNT] # Assign the second card.
CARD_COUNT += 1
del deck[:18] # Delete player cards from deck.
for i in range(3):
table['flop'] = deck[1] + deck[2] + deck[3]
del deck[:4]# Delete flop from deck.
table['turn'] = deck[0]
table['river'] = deck[2]
del deck[:3] # Delete turn and river from deck.
break
return players, table
plays = dealer(deck)
# Player hand outcome.
pone = max(good_hands(plays, 0)) if good_hands(plays, 0) else None
ptwo = max(good_hands(plays, 1)) if good_hands(plays, 1) else None
pthree = max(good_hands(plays, 2)) if good_hands(plays, 2) else None
pfour = max(good_hands(plays, 3)) if good_hands(plays, 3) else None
pfive = max(good_hands(plays, 4)) if good_hands(plays, 4) else None
psix = max(good_hands(plays, 5)) if good_hands(plays, 5) else None
pseven = max(good_hands(plays, 6)) if good_hands(plays, 6) else None
peight = max(good_hands(plays, 7)) if good_hands(plays, 7) else None
pnine = max(good_hands(plays, 8)) if good_hands(plays, 8) else None
outcome = [pone, ptwo, pthree, pfour, pfive, psix, pseven, peight, pnine]
hands += 1
if hands == 1:
break
print(outcome)
print(plays)
This is the analyzer itself: goodhands.py
from collections import Counter
def good_hands(plays, player):
good_hands = [] # This list will hold all the winning values.
table = plays[1] # This is the flop, turn and river.
player_cards = list(plays[0].values()) # List of player_cards
table_list = "".join(list(table.values())) # list of table values.
all_cards = [i + table_list for i in player_cards] # Player cards with table values.
occurrences = list(Counter(all_cards[player]).items()) # Counter is used to make a dict with all the occurences. i.e. "k":2 is two K's.
count = 0
letters = "JQKA"
suits = "♣♦♥♠"
# Card Counters
atofive = 0
twosix = 0
threeseven = 0
foureight = 0
fivenine = 0
six_ten = 0
seventoj = 0
eighttoq = 0
ninetok = 0
tentoa = 0
royal_flush = 0
# This will replace "1" and "0" with a "10"
for i in occurrences:
if "1" in i[0]:
occurrences.remove(i)
for i in occurrences:
if "0" in i[0]:
occurrences.remove(i)
occurrences.append(("10",i[1]))
for i in occurrences:
if i[0].isdigit() == True: # i[0] is the Suit or the number of a card
card_value = int(i[0])
# A - 5 LOW STRAIGHT
if i[0]:
if i[0] == "A":
atofive +=1
if i[0].isdigit():
for n in range(2, 6):
if int(i[0]) == n:
atofive += 1
if atofive == 5 and 21 not in good_hands:
good_hands.append(21)
# 2 - 6 LOW STRAIGHT
if i[0].isdigit() == True:
for n in range(2, 7):
if int(i[0]) == n:
twosix += 1
if twosix == 5 and 21.1 not in good_hands:
good_hands.append(21.1)
# 3 - 7 LOW STRAIGHT
if i[0].isdigit() == True:
for n in range(3, 8):
if int(i[0]) == n:
threeseven += 1
if threeseven == 5 and 21.2 not in good_hands:
good_hands.append(21.2)
# 4 - 8 LOW STRAIGHT
if i[0].isdigit() == True:
for n in range(4, 9):
if int(i[0]) == n:
foureight += 1
if foureight == 5 and 21.3 not in good_hands:
good_hands.append(21.3)
# 5 - 9 LOW STRAIGHT
if i[0].isdigit() == True:
for n in range(5, 10):
if int(i[0]) == n:
fivenine += 1
if fivenine == 5 and 21.4 not in good_hands:
good_hands.append(21.4)
# 6 - 10 LOW STRAIGHT
if i[0].isdigit() == True:
for n in range(6, 11):
if int(i[0]) == n:
six_ten += 1
if six_ten == 5and 21.5 not in good_hands:
good_hands.append(21.5)
# 7 - J MID STRAIGHT
if i[0]:
if i[0] == "J":
seventoj +=1
if i[0].isdigit():
for n in range(7, 11):
if int(i[0]) == n:
seventoj += 1
if seventoj == 5 and 21.6 not in good_hands:
good_hands.append(21.6)
# 8 - Q MID STRAIGHT
if i[0]:
if i[0] == "J":
eighttoq +=1
if i[0] == "Q":
eighttoq +=1
if i[0].isdigit():
for n in range(8, 11):
if int(i[0]) == n:
eighttoq += 1
if eighttoq == 5 and 21.7 not in good_hands:
good_hands.append(21.7)
# 9 - k HIGH STRAIGHT
if i[0]:
if i[0] == "J":
ninetok +=1
if i[0] == "Q":
ninetok +=1
if i[0] == "K":
ninetok +=1
if i[0].isdigit():
for n in range(9, 11):
if int(i[0]) == n:
ninetok += 1
if ninetok == 5 and 21.8 not in good_hands:
good_hands.append(21.8)
# 10 - A HIGHEST STRAIGHT
if i[0]:
if i[0] == "10":
tentoa += 1
if i[0] == "J":
tentoa +=1
if i[0] == "Q":
tentoa +=1
if i[0] == "K":
tentoa +=1
if i[0] == "A":
tentoa += 1
if tentoa == 5 and 21.9 not in good_hands:
good_hands.append(21.9)
if i[0].isdigit() == True and i[1] == 2: # i[1] are the instances
count += 1
if card_value <= 6:
good_hands.append(0) # Low Pair
if card_value > 6 and card_value <= 9:
good_hands.append(1) # Mid Pair
if card_value == 10:
good_hands.append(2) # Mid-High Pair
# Two Pair
if i[0].isdigit() == True and count == 2:
good_hands.append(9)
# Letters Pairs
if i[0] in letters:
if i[1] == 2:
if i[0] == "J":
good_hands.append(3) # Mid-High
if i[0] == "Q":
good_hands.append(4) # High Pair
if i[0] == "K":
good_hands.append(5) # High Pair
if i[0] == "A":
good_hands.append(10) # Highest Pair
# Letter Trips
if i[1] == 3:
if i[0] == "J":
good_hands.append(11.90)
if i[0] == "Q":
good_hands.append(11.91)
if i[0] == "K":
good_hands.append(11.92)
if i[0] == "A":
good_hands.append(11.93)
# Letter Quads
if i[1] == 4:
if i[0] == "J":
good_hands.append(25.90)
if i[0] == "Q":
good_hands.append(25.91)
if i[0] == "K":
good_hands.append(25.92)
if i[0] == "A":
good_hands.append(25.93)
# Letter QUADS VALUE OF **** 20 *****
# Num Trips
if i[0].isdigit() == True and i[1] >= 3:
if [0] == "2":
good_hands.append(11.1)
if [0] == "3":
good_hands.append(11.2)
if [0] == "4":
good_hands.append(11.3)
if [0] == "5":
good_hands.append(11.4)
if [0] == "6":
good_hands.append(11.5)
if [0] == "7":
good_hands.append(11.6)
if [0] == "8":
good_hands.append(11.7)
if [0] == "9":
good_hands.append(11.8)
# Number Quads
if i[1] == 4:
if [0] == "2":
good_hands.append(25.1)
if [0] == "3":
good_hands.append(25.2)
if [0] == "4":
good_hands.append(25.3)
if [0] == "5":
good_hands.append(25.4)
if [0] == "6":
good_hands.append(25.5)
if [0] == "7":
good_hands.append(25.6)
if [0] == "8":
good_hands.append(25.7)
if [0] == "9":
good_hands.append(25.8)
# Full House
pairs = [0,1,2,3,4,5,10]
trips = [11.1, 11.2, 11.3, 11.4,
11.5, 11.6, 11.7, 11.8,
11.9, 11.91, 11.92, 11.93, 11.93]
for p in pairs:
if p in good_hands:
for t in trips:
if t in good_hands and 26 not in good_hands:
good_hands.append(26)
# Flush
if i[0].isdigit() == False and i[1] == 5:
good_hands.append(23)
# Straight Flushes
# Royal Flush
p1_cards = all_cards[0]
royal_count = 0
for suit in suits:
if "10" + suit in p1_cards:
royal_count += 1
if "J" + suit in p1_cards:
royal_count += 1
if "Q" + suit in p1_cards:
royal_count += 1
if "K" + suit in p1_cards:
royal_count += 1
if "A" + suit in p1_cards:
royal_count += 1
if royal_count == 5:
print("ROYAL FLUSH!!!!")
good_hands.append(100)
return good_hands
I'm more than thankful for the advice because I really need it. I've been coding 'seriously' for about two months after one year off.