Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Gongsta committed Jun 22, 2024
1 parent 9f7a532 commit 2cd6851
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
12 changes: 6 additions & 6 deletions game/poker_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
12 changes: 3 additions & 9 deletions slumbot/slumbot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
44 changes: 26 additions & 18 deletions src/aiplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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.",
],
Expand Down Expand Up @@ -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
Expand All @@ -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?")
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
13 changes: 8 additions & 5 deletions src/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2cd6851

Please sign in to comment.