Widespread Augmented Reality

Widespread Augmented Reality
Click on the image to get the Android Augmented Reality Heads up Display

Sunday, March 26, 2023

Python - Count Winning Poker Hands in File

import time
start = time.time()
pokerhands = open("p054_poker.txt", "r+")
#pokerhands = open("p054_poker_hand.txt", "r+")
print("File to read: ", pokerhands.name)
# Read single line in file
two_hands = pokerhands.readline().strip()
#---------------- functions -----------
def convertPic(c):
if c == "T":
c = 10
if c == "J":
c = 11
if c == "Q":
c = 12
if c == "K":
c = 13
if c == "A":
c = 14
return c
def stripNums(player):
nums = []
for card in player:
nums.append(int(convertPic(card[0])))
return nums
def stripSuits(player):
suits = []
for card in player:
suits.append(card[1])
return suits
def isFlush(s):
if([s[0]]*len(s) == s):
return True
else:
return False
def isRoyal(nums):
nums.sort()
if (nums == [10, 11, 12, 13, 14]):
return True
else:
return False
def isStraight(nums):
if sorted(nums) == list(range(min(nums), max(nums)+1)) or sorted(nums) == [2,3,4,5,14]:
return True
def lowStraight(nums):
if sorted(nums) == [2, 3, 4, 5, 14]:
return True
def countSimilar(cards):
#see groupcards.py
likecnt = 1
groupings = []
grouped = []
for index in range(0, len(cards)):
if index < 4:
if cards[index] == cards[index+1]:
likecnt+=1
else:
if likecnt > 1:
grouped = [cards[index], likecnt]
groupings.append(grouped)
likecnt = 1
grouped = []
else:
if likecnt > 1:
grouped = [cards[index], likecnt]
groupings.append(grouped)
# no combos
if len(groupings) == 0:
hand = 1
high = max(cards)
# one combo of either pair, three or four
if len(groupings) == 1:
if groupings[0][1] == 4:
hand = 8
high = groupings[0][0]
if groupings[0][1] == 3:
hand = 4
high = groupings[0][0]
if groupings[0][1] == 2:
hand = 2
high = groupings[0][0]
# two combos - two pair or full house
if len(groupings) == 2:
pairs = []
threes = 0
for g in groupings:
if g[1] == 2:
pairs.append(g[0])
if g[1] == 3:
threes = g[0]
# two pairs
if threes == 0:
hand = 3
high = max(pairs)
else:
# full house with high of 3 combo
hand = 7
high = threes
result = [hand, high]
return result
#---------------------
def calcHand(player):
hand = 1
high = 1
nums = stripNums(player)
suits = stripSuits(player)
#check for flush first
if isFlush(suits) == True:
hand = 6
high = max(nums)
if isRoyal(nums) == True:
hand = 10
high = 15
if isStraight(nums) == True:
hand = 9
high = max(nums)
if lowStraight(nums) == True:
hand = 9
nums.remove(14)
high = max(nums)
#fall through to all other possible groupings including none
#scored 1,2,4,3,7,8
if hand == 1 and high == 1:
hand = countSimilar(nums)[0]
high = countSimilar(nums)[1]
return [hand, high]
def nextHigh(player, high):
#get next high if hands identical
nums = stripNums(player)
newnums = [i for i in nums if i != high]
return max(newnums)
# ------ start main
rec_cnt = 0
player1wins = 0
player2wins = 0
while two_hands:
cardarray = two_hands.split(" ")
player1 = cardarray[0:5]
player1.sort()
player1hand = calcHand(player1)[0]
player1high = calcHand(player1)[1]
###################
player2 = cardarray[5:10]
player2.sort()
player2hand = calcHand(player2)[0]
player2high = calcHand(player2)[1]
# compare and count wins
if player1hand > player2hand:
player1wins+=1
else:
if player1hand < player2hand:
player2wins+=1
else:
if player1hand == player2hand:
if player1high > player2high:
player1wins+=1
else:
if player1high < player2high:
player2wins+=1
else:
if player1high == player2high:
if nextHigh(player1, player1high) > nextHigh(player2, player2high):
player1wins+=1
else:
player2wins+=1
############ continue reading file #######
# use readline() to read next line
two_hands = pokerhands.readline().strip()
rec_cnt+=1
end = time.time()
print("execution time : ", (end-start) * 10**3, "ms")
# print scores and close opened file
print("record_cnt = "+str(rec_cnt))
print("player1 wins = "+str(player1wins))
print("player2 wins = "+str(player2wins))
pokerhands.close()

File to read: p054_poker.txt
execution time : 627.8073787689209 ms
record_cnt = 1000
player1 wins = 376
player2 wins = 624
[Program finished]

No comments:

Post a Comment