From 2cd6851bcc728190807760c83f59112adf71f7aa Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 21 Jun 2024 20:24:03 -0700 Subject: [PATCH] Code cleanup --- game/poker_main.py | 12 ++++++------ slumbot/slumbot_api.py | 12 +++--------- src/aiplayer.py | 44 +++++++++++++++++++++++++----------------- src/environment.py | 13 ++++++++----- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/game/poker_main.py b/game/poker_main.py index 8e89956..df00e5f 100644 --- a/game/poker_main.py +++ b/game/poker_main.py @@ -177,20 +177,23 @@ def display_opponent_bet(env: PokerEnvironment): def display_sessions_winnings(env: PokerEnvironment): - # winnings = sum(env.players_balance_history[0]) winnings = 0 + if len(env.players_balance_history) != 0: + winnings = sum(env.players_balance_history[0]) if winnings < 0: text = POT_FONT.render("Session Winnings: -$" + str(-winnings), 1, WHITE) else: text = POT_FONT.render("Session Winnings: $" + str(winnings), 1, WHITE) + WIN.blit(text, scale_tuple((WIDTH - 400, 40), scale_factor)) + def display_turn(env: PokerEnvironment): if env.position_in_play == 0: # AI text = POT_FONT.render("YOUR TURN", 1, WHITE) else: text = POT_FONT.render("OPPONENT TURN", 1, RED) - WIN.blit(text, (70, 40)) + WIN.blit(text, scale_tuple((70, 40), scale_factor)) def display_user_cards(env: PokerEnvironment): @@ -387,8 +390,6 @@ def main(): # env.add_player() # Player since we want everything to be entered manually # else: # env.add_AI_player() # Opponent - # play as the AI - # env.add_AI_player() # env.add_player() env.add_AI_player() env.add_player() @@ -414,8 +415,7 @@ def place_custom_bet(): global input_bet_text, active, cursor_counter, warning_text cursor_counter = (cursor_counter + 1) % 30 - if user_input or replay: # If you want to render PyGame - clock.tick(FPS) + clock.tick(FPS) if env.players[env.position_in_play].is_AI: env.handle_game_stage() diff --git a/slumbot/slumbot_api.py b/slumbot/slumbot_api.py index 253bc3b..e00bf61 100644 --- a/slumbot/slumbot_api.py +++ b/slumbot/slumbot_api.py @@ -94,11 +94,6 @@ USERNAME = "cfr_abstraction" PASSWORD = "cfr_abstraction" player = CFRAIPlayer(20000) -# if os.path.exists(f"../data/slumbot/{USERNAME}.joblib"): # Load previous history if it exists -# history = joblib.load(f"../data/slumbot/{USERNAME}.joblib") - -if not os.path.exists("../data/slumbot"): - os.makedirs("../data/slumbot") NUM_STREETS = 4 SMALL_BLIND = 50 @@ -318,10 +313,6 @@ def Act(token, action): print("Could not get JSON from response") sys.exit(-1) - if "error_msg" in r: - print("Error: %s" % r["error_msg"]) - sys.exit(-1) - return r @@ -474,6 +465,9 @@ def PlayHand(token, debug=False): if debug: print("Sending incremental action: %s" % incr) r = Act(token, incr) + if "error_msg" in r: + print("Error: %s" % r["error_msg"]) + return (token, 0, 0) # Should never get here diff --git a/src/aiplayer.py b/src/aiplayer.py index 9f72ac5..f6028d1 100644 --- a/src/aiplayer.py +++ b/src/aiplayer.py @@ -17,6 +17,7 @@ class AIPlayer(Player): def __init__(self, balance) -> None: super().__init__(balance) self.is_AI = True + self.speak = True self.engine = pyttsx3.init() @@ -33,7 +34,7 @@ def get_trash_talk(self, action_type, bet_amount=0): "I call your bet. Bring it on!", ], "f": [ - "I Fold. You win this one.", + "I Fold. You win this one, but not for long.", "Fold. Consider yourself lucky.", "I'm folding. Don't get used to it.", ], @@ -67,15 +68,18 @@ def get_trash_talk(self, action_type, bet_amount=0): def trash_talk_win(self): self.engine.say(random.choice(self.get_trash_talk("win"))) - self.engine.runAndWait() + if self.speak: + self.engine.runAndWait() def trash_talk_lose(self): self.engine.say(random.choice(self.get_trash_talk("lose"))) - self.engine.runAndWait() + if self.speak: + self.engine.runAndWait() def trash_talk_fold(self): self.engine.say(random.choice(self.get_trash_talk("opponent_fold"))) - self.engine.runAndWait() + if self.speak: + self.engine.runAndWait() def process_action(self, action, observed_env): if action == "k": # check @@ -84,7 +88,7 @@ def process_action(self, action, observed_env): else: self.current_bet = 0 - self.engine.say("I Check") + self.engine.say(random.choice(self.get_trash_talk("k"))) elif action == "c": if observed_env.get_highest_current_bet() == self.player_balance: self.engine.say("I call your all-in. You think I'm afraid?") @@ -101,7 +105,8 @@ def process_action(self, action, observed_env): else: self.engine.say(random.choice(self.get_trash_talk("b", self.current_bet))) - self.engine.runAndWait() + if self.speak: + self.engine.runAndWait() def place_bet(self, observed_env): raise NotImplementedError @@ -245,7 +250,8 @@ def get_action( # Bet sizing uses the pot balance # stage_pot_balance used for preflop, total_pot_balance used for postflop - HEURISTICS = False # trying this in case my preflop strategy sucks + action = None + HEURISTICS = True # trying this in case my preflop strategy sucks if len(community_cards) == 0: # preflop if HEURISTICS: player = EquityAIPlayer(self.player_balance) @@ -263,36 +269,38 @@ def get_action( abstracted_history = self.perform_preflop_abstraction(history, BIG_BLIND=BIG_BLIND) infoset_key = "".join(PreflopHoldemHistory(abstracted_history).get_infoSet_key()) strategy = self.preflop_infosets[infoset_key].get_average_strategy() - action = getAction(strategy) - print("Abstracted action: ", action) - if action == "bMIN": + abstracted_action = getAction(strategy) + if abstracted_action == "bMIN": action = "b" + str(max(BIG_BLIND, int(stage_pot_balance))) - elif action == "bMID": + elif abstracted_action == "bMID": action = "b" + str(max(BIG_BLIND, 2 * int(stage_pot_balance))) elif ( - action == "bMAX" + abstracted_action == "bMAX" ): # in training, i have it set to all in... but wiser to 4x pot? action = "b" + str(min(player_balance, 4 * int(stage_pot_balance))) + else: + action = abstracted_action else: - print("history: ", history) abstracted_history = self.perform_postflop_abstraction( history, BIG_BLIND=BIG_BLIND ) # condense down bet sequencing infoset_key = PostflopHoldemHistory(abstracted_history).get_infoSet_key_online() strategy = self.postflop_infosets[infoset_key].get_average_strategy() - action = getAction(strategy) + abstracted_action = getAction(strategy) print("Abstracted action: ", action) - if action == "bMIN": + if abstracted_action == "bMIN": action = "b" + str(max(BIG_BLIND, int(1 / 3 * total_pot_balance))) - elif action == "bMAX": + elif abstracted_action == "bMAX": action = "b" + str(min(total_pot_balance, player_balance)) + else: + action = abstracted_action + print("history: ", history) if not HEURISTICS: print("Abstracted history: ", abstracted_history) print("Infoset key: ", infoset_key) print("AI strategy ", strategy) - - print("Action selected", action) + print("Abstracted Action:", abstracted_action, "Final Action:", action) return action diff --git a/src/environment.py b/src/environment.py index c9bbfab..58606b1 100644 --- a/src/environment.py +++ b/src/environment.py @@ -40,11 +40,11 @@ def __init__(self) -> None: self.showdown = False # flag that can be used to reveal opponents cards if needed # FIXED BALANCES - self.new_player_balance = 100 - self.SMALL_BLIND = 1 - self.BIG_BLIND = 2 + self.new_player_balance = 2500 + self.SMALL_BLIND = 10 + self.BIG_BLIND = 20 - self.INPUT_CARDS = False + self.INPUT_CARDS = True self.history = [] self.players_balance_history = [] # List of "n" list for "n" players @@ -242,7 +242,7 @@ def play_preflop(self): card_str += str(card) self.players[player_idx].add_card_to_hand(card) - self.history += [card_str] # always deal to the non-dealer first + self.history += [card_str] # always deal to the non-dealer first def play_flop(self): self.deck.draw() # We must first burn one card, TODO: Show on video @@ -331,6 +331,9 @@ def end_of_round(self): return self.game_stage == 6 def end_round(self): + self.update_player_balances_at_end_of_stage() + self.move_stage_to_total_pot_balance() + if self.count_remaining_players_in_round() > 1: self.showdown = True evaluator = Evaluator()