Skip to content

Commit

Permalink
reworked the tests to reuse the same code, and rewrote colins test on…
Browse files Browse the repository at this point in the history
…ly so it wasnt recursive which makes it a lot easier to add a break point in the right place
  • Loading branch information
fedden committed Apr 26, 2020
1 parent 90cadc8 commit 0193bec
Showing 1 changed file with 57 additions and 82 deletions.
139 changes: 57 additions & 82 deletions test/functional/test_short_deck.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
from typing import Tuple, Union

import pytest
import numpy as np

from pluribus.games.short_deck.state import ShortDeckPokerState
from pluribus.games.short_deck.player import ShortDeckPokerPlayer
from pluribus.poker.pot import Pot
from pluribus.utils.random import seed

def test_short_deck_1():
"""Test the short deck poker game state works as expected."""
from pluribus.games.short_deck.player import ShortDeckPokerPlayer
from pluribus.games.short_deck.state import ShortDeckPokerState
from pluribus.poker.pot import Pot

n_players = 3
def _new_game(
n_players: int,
small_blind: int = 50,
big_blind: int = 100,
initial_chips: int = 10000,
) -> Tuple[ShortDeckPokerState, Pot]:
"""Create a new game."""
pot = Pot()
players = [
ShortDeckPokerPlayer(player_i=player_i, pot=pot, initial_chips=10000)
ShortDeckPokerPlayer(player_i=player_i, pot=pot, initial_chips=initial_chips)
for player_i in range(n_players)
]
state = ShortDeckPokerState(players=players, load_pickle_files=False)
state = ShortDeckPokerState(
players=players,
load_pickle_files=False,
small_blind=small_blind,
big_blind=big_blind,
)
return state, pot


def test_short_deck_1():
"""Test the short deck poker game state works as expected."""
n_players = 3
state, _ = _new_game(n_players=n_players)
# Call for all players.
player_i_order = [2, 0, 1]
for i in range(n_players):
assert state.current_player.name == f"player_{player_i_order[i]}"
assert len(state.legal_actions) == 3
assert state._betting_stage == "pre_flop"
state = state.apply_action(action_str="call")
assert state._betting_stage == "flop"
# Fold for all but last player.
for player_i in range(n_players - 1):
assert state.current_player.name == f"player_{player_i}"
Expand All @@ -35,17 +55,8 @@ def test_short_deck_1():

def test_short_deck_2():
"""Test the short deck poker game state works as expected."""
from pluribus.games.short_deck.player import ShortDeckPokerPlayer
from pluribus.games.short_deck.state import ShortDeckPokerState
from pluribus.poker.pot import Pot

n_players = 3
pot = Pot()
players = [
ShortDeckPokerPlayer(player_i=player_i, pot=pot, initial_chips=10000)
for player_i in range(n_players)
]
state = ShortDeckPokerState(players=players, load_pickle_files=False)
state, _ = _new_game(n_players=3)
player_i_order = [2, 0, 1]
# Call for all players.
for i in range(n_players):
Expand All @@ -60,7 +71,7 @@ def test_short_deck_2():
assert state._betting_stage == "flop"
state = state.apply_action(action_str="raise")
# Call for all players and ensure all players have chipped in the same..
for player_i in range(n_players):
for player_i in range(n_players - 1):
assert state.current_player.name == f"player_{player_i}"
assert len(state.legal_actions) == 2
assert state._betting_stage == "flop"
Expand All @@ -72,7 +83,7 @@ def test_short_deck_2():
assert state._betting_stage == "turn"
state = state.apply_action(action_str="raise")
# Call for all players and ensure all players have chipped in the same..
for player_i in range(n_players):
for player_i in range(n_players - 1):
assert state.current_player.name == f"player_{player_i}"
assert len(state.legal_actions) == 2
assert state._betting_stage == "turn"
Expand Down Expand Up @@ -105,16 +116,7 @@ def test_short_deck_3(n_players: int):
order of the players is correct - for the pre-flop it should be
[-1, -2, 0, 1, ..., -3].
"""
from pluribus.games.short_deck.player import ShortDeckPokerPlayer
from pluribus.games.short_deck.state import ShortDeckPokerState
from pluribus.poker.pot import Pot

pot = Pot()
players = [
ShortDeckPokerPlayer(player_i=player_i, pot=pot, initial_chips=10000)
for player_i in range(n_players)
]
state = ShortDeckPokerState(players=players, load_pickle_files=False)
state, _ = _new_game(n_players=n_players)
order = list(range(n_players))
player_i_order = {
"pre_flop": order[2:] + order[:2],
Expand Down Expand Up @@ -146,20 +148,8 @@ def test_short_deck_3(n_players: int):
@pytest.mark.parametrize("big_blind", [100, 1000])
def test_pre_flop_pot(n_players: int, small_blind: int, big_blind: int):
"""Test preflop the state is set up for player 2 to start betting."""
from pluribus.games.short_deck.player import ShortDeckPokerPlayer
from pluribus.games.short_deck.state import ShortDeckPokerState
from pluribus.poker.pot import Pot

pot = Pot()
players = [
ShortDeckPokerPlayer(player_i=player_i, pot=pot, initial_chips=10000)
for player_i in range(n_players)
]
state = ShortDeckPokerState(
players=players,
load_pickle_files=False,
small_blind=small_blind,
big_blind=big_blind,
state, pot = _new_game(
n_players=n_players, small_blind=small_blind, big_blind=big_blind,
)
n_bet_chips = sum(p.n_bet_chips for p in state.players)
target = small_blind + big_blind
Expand All @@ -173,50 +163,35 @@ def test_pre_flop_pot(n_players: int, small_blind: int, big_blind: int):
), f"small and big blind have are not in pot! {n_bet_chips} == {pot.total}"


def _play_game_helper(state, betting_round_dict, bad_seq):
p = 1 / len(state.legal_actions)
probabilities = np.full(len(state.legal_actions), p)
a = np.random.choice(state.legal_actions, p=probabilities)
betting_stage = state._betting_stage
if betting_stage not in ["show_down", "terminal"]:
if state._poker_engine.n_active_players == 2:
betting_round_dict[betting_stage].append(a)
lst = [x for x in betting_round_dict[betting_stage] if x != "skip"]
for i in range(len(lst)):
assert lst[i : i + len(bad_seq)] != bad_seq
state = state.apply_action(a)

_play_game_helper(state, betting_round_dict, bad_seq)


@pytest.mark.parametrize("n_players", [2, 3])
@pytest.mark.parametrize("small_blind", [50])
@pytest.mark.parametrize("big_blind", [100])
def test_call_action_sequence(n_players: int, small_blind: int, big_blind: int):
def test_call_action_sequence():
"""
Make sure we never see an action sequence of "raise", "call", "call" in the same
round with only two players. There would be a similar analog for more than two players,
but this should aid in initially finding the bug.
"""
from pluribus.games.short_deck.player import ShortDeckPokerPlayer
from pluribus.games.short_deck.state import ShortDeckPokerState
from pluribus.poker.pot import Pot

pot = Pot()
players = [
ShortDeckPokerPlayer(player_i=player_i, pot=pot, initial_chips=10000)
for player_i in range(n_players)
]

state = ShortDeckPokerState(
players=players,
load_pickle_files=False,
small_blind=small_blind,
big_blind=big_blind,
)

seed(42)
# example of a bad sequence in a two-handed game in one round
bad_seq = ["raise", "call", "call"]
for t in range(100):
state, _ = _new_game(n_players=3, small_blind=50, big_blind=100)
betting_round_dict = {"pre_flop": [], "flop": [], "turn": [], "river": []}
_play_game_helper(state, betting_round_dict, bad_seq)
iteration_i = 0
while state._betting_stage not in {"show_down", "terminal"}:
uniform_probability = 1 / len(state.legal_actions)
probabilities = np.full(len(state.legal_actions), uniform_probability)
random_action = np.random.choice(state.legal_actions, p=probabilities)
betting_stage = state._betting_stage
if state._poker_engine.n_active_players == 2:
betting_round_dict[betting_stage].append(random_action)
no_fold_action_history = [
action
for action in betting_round_dict[betting_stage]
if action != "skip"
]
# Loop through the action history and make sure the bad
# sequence has not happened.
for i in range(len(no_fold_action_history)):
history_slice = no_fold_action_history[i : i + len(bad_seq)]
assert history_slice != bad_seq
state = state.apply_action(random_action)
iteration_i += 1

0 comments on commit 0193bec

Please sign in to comment.