Пример #1
0
def test_true_count():
    rules = {"num_decks": 1}
    game = Game([Player(100)], rules)
    game.play_round()
    remaining_decks = len(game.deck) / 52
    true_count = game.count / remaining_decks
    assert game.true_count == pytest.approx(true_count)
Пример #2
0
def test_charts(basic_player):
    game = Game([basic_player])
    game.simulate(10000)
    # heatmap
    chart = result_heatmap(basic_player.history)
    # outcome bars
    chart = outcome_bars(basic_player.history)
Пример #3
0
def test_data():
    """
    A few sanity check on the data we collect
    """
    p = Player(100)
    game = Game([p])
    game.simulate(100)
    data = pd.DataFrame(p.history)
    assert data["num_hard_aces"].min() >= 0
    assert data["num_aces"].max() >= 0
    assert np.all(data["num_hard_aces"] <= data["num_aces"])
    assert np.all(data["num_aces"] <= data["num_cards"])
Пример #4
0
def test_game_implementation():
    with pytest.raises(TypeError):
        Game([Player(100)], "str")
    # ensure that the parameters are updated properly
    rules = {"insurance_allowed": False, "blackjack_payout": 1.2}
    game = Game([Player(100)], rules)
    assert game.game_params.blackjack_payout == 1.2
    assert not game.game_params.insurance_allowed

    # ensure player limits are imposed
    rules = {"max_players": 2}
    with pytest.raises(AssertionError):
        Game([Player(100), Player(100), Player(100)], rules)
    with pytest.raises(AssertionError):
        Game([])
Пример #5
0
def test_simulation():
    game = Game([Player(100)])
    game.simulate(10)
"""
import random
from py21 import Game, Player
from py21.utils import result_heatmap, outcome_bars
from py21.strategies import hit_to_seventeen
from embedplots import add_plots
from pathlib import Path

CUR_PATH = Path(__file__).resolve().parent
random.seed(409)

bankroll = 1000000
# create first player
player = Player(bankroll)
# first game
game = Game([player])
rounds = 1000000
game.simulate(rounds)

# create heatmap
heat = result_heatmap(player.history, result="win", title="Winning Pct")
heat_path = Path(CUR_PATH, "heatmap.html")
heat.save("heatmap.html")
heat.save("../images/heatmap.json")

# second game
player = Player(bankroll)
player2 = Player(bankroll, strategy_func=hit_to_seventeen)

game2 = Game([player, player2])
game2.simulate(rounds)
Пример #7
0
def test_game_implementation():
    with pytest.raises(TypeError):
        Game([Player(100)], "str")
    # ensure that the parameters are updated properly
    rules = {"insurance_allowed": False,
             "blackjack_payout": 1.2}
    game = Game([Player(100)], rules)
    assert game.game_params.blackjack_payout == 1.2
    assert not game.game_params.insurance_allowed

    # ensure player limits are imposed
    rules = {"max_players": 2}
    with pytest.raises(AssertionError):
        Game([Player(100), Player(100), Player(100)], rules)
    with pytest.raises(AssertionError):
        Game([])

    # ensure surrender throws and error if not allowed
    # decided this test just isn't relevant right now. will bring back
    # when I figure out a better implementation
    rules = {"surrender_allowed": False}

    def surrender(**kwargs):
        return "SURRENDER"

    player = Player(100, strategy_func=surrender)
    game = Game([player], rules=rules)
    with pytest.raises(ValueError):
        game.play_round()

    # test split and count implementations
    rules = {"shuffle_freq": 0}
    t_game = Game([Player(100)], rules=rules, test=True)
    t_game.play_round()
    assert t_game.player_list[0].bankroll == 110.0
    assert t_game.count == 3
Пример #8
0
def test_house_edge(basic_player):
    g = Game([basic_player])
    edge = house_edge(basic_player, g.game_params)
    assert isinstance(edge, float)