I’ve been learning python for two days and I’d like to write a script that reads cards on the table while playing poker and calculates the chance of winning. Here’s what I’ve managed to write in 2 days of learning python. It somehow works but is there a way to make this code better? The task is to check whether there is a king of hearts (KH.png) or ace of hearts (AH.png) on the screen and do the math and stop the code. In my case I run the code, it works, but after the math it doesn’t stop and then it begins to run very slowly (No ACES, No KINGS (print) appear with a very high delay)
import pied_poker as pp
import numpy as np
import pyautogui
import time
np.random.seed(420)
def kings_search():
while 1:
try:
pyautogui.locateOnScreen('KH.png', confidence=0.9)
card_one = "kh"
card_two = "kd"
print("KINGS!")
p1 = pp.Player('Artur', pp.Card.of(card_one, card_two))
p2 = pp.Player('Opponent', pp.Card.of()) # We don't know the opponent's cards, so we leave that empty
community_cards = pp.Card.of("2h", "qh", "4h", "10c", "6c")
print(p1)
print(f'Community cards: {community_cards}')
simulator = pp.PokerRound.PokerRoundSimulator(community_cards=community_cards, players=[p1, p2], total_players=2)
num_simulations = 10000
simulation_result=simulator.simulate(n=num_simulations, n_jobs=1)
print(simulation_result.probability_of(pp.Probability.PlayerWins(p1)))
break
except pyautogui.ImageNotFoundException:
print("No KINGS")
aces()
def aces():
while 1:
try:
pyautogui.locateOnScreen('AH.png', confidence=0.9)
card_one = "ah"
card_two = "ad"
print("ACES!")
p1 = pp.Player('Artur', pp.Card.of(card_one, card_two))
p2 = pp.Player('Opponent', pp.Card.of()) # We don't know the opponent's cards, so we leave that empty
community_cards = pp.Card.of("2h", "qh", "4h", "10c", "6c")
print(p1)
print(f'Community cards: {community_cards}')
simulator = pp.PokerRound.PokerRoundSimulator(community_cards=community_cards, players=[p1, p2], total_players=2)
num_simulations = 10000
simulation_result=simulator.simulate(n=num_simulations, n_jobs=1)
print(simulation_result.probability_of(pp.Probability.PlayerWins(p1)))
break
except pyautogui.ImageNotFoundException:
print("No ACES")
kings_search()
aces()
I actually don’t know what to do with that.
>Solution :
So the issue you are having is twofold.
- the
aces()andkings_search()are both in while loops that never really break unless an item is found in both of the loops. - the
aces()andkings_search()call each other vice versa — when the element is not found so you keep creating instances of each of the function to run indefinitely.
Here is a suggestion for the code that you should use (just a single check of whether there is a king or ace at a time during your game event loop)
def find_item_on_screen(card_image, card_one, card_two, card_type) -> bool: # returns a TF value to use elsewhere # generic function that handles finding cards of multiple types
try:
pyautogui.locateOnScreen(card_image, confidence=0.9)
print(f"{card_type}!")
p1 = pp.Player('Artur', pp.Card.of(card_one, card_two))
p2 = pp.Player('Opponent', pp.Card.of()) # We don't know the opponent's cards, so we leave that empty
community_cards = pp.Card.of("2h", "qh", "4h", "10c", "6c")
print(p1)
print(f'Community cards: {community_cards}')
simulator = pp.PokerRound.PokerRoundSimulator(community_cards=community_cards, players=[p1, p2], total_players=2)
num_simulations = 10000
simulation_result=simulator.simulate(n=num_simulations, n_jobs=1)
print(simulation_result.probability_of(pp.Probability.PlayerWins(p1)))
return True
except pyautogui.ImageNotFoundException:
print(f"No {card_type}")
return False
def check_king():
locate_item = 'KH.png'
card_one = "kh"
card_two = "kd"
card_type = 'KING'
find_item_on_screen(locate_item, card_one, card_two, card_type)
def check_aces(): # a more specific function for aces when you have a generic function find_item_on_screen()
locate_item = 'AH.png'
card_one = "ah"
card_two = "ad"
card_type = 'ACES'
find_item_on_screen(locate_item, card_one, card_two, card_type)
check_king() # how to call each of the functions # place these inside of the area that you are running the game
check_aces()