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([])
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)
def test_wager(): def bad_min_wager(player, min_bet, max_bet): return 1 def bad_max_wager(player, min_bet, max_bet): return 1000 p = Player(100, wager_func=bad_min_wager) with pytest.raises(AssertionError): p.wager(min_bet=5, max_bet=500) p = Player(100, wager_func=bad_max_wager) with pytest.raises(AssertionError): p.wager(min_bet=5, max_bet=500) with pytest.raises(AssertionError): p.wager(min_bet=5, max_bet=5000)
def test_add_card_two(): card = Card(3, "C") player = Player(100, None) hand = Hand(card, player) with pytest.raises(TypeError): hand.add_card_two("2C") card_two = Card(2, "D") hand.add_card_two(card_two) assert hand.total == 5
def test_comparisons(): hand = Hand(Card(2, "H"), player=Player(100, None, None), min_bet=5, max_bet=500) hand.add_card_two(Card(3, "D")) with pytest.raises(TypeError): hand > 3 with pytest.raises(TypeError): hand < 3 with pytest.raises(TypeError): hand == 3
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"])
def test_hand_implementation(): card_one = Card(2, "C") card_two = Card(3, "H") player = Player(100, None, None) Hand(card_one, player) with pytest.raises(TypeError): Hand("2C", card_two, player=player) with pytest.raises(TypeError): Hand(card_one, card_two, dict) hand = Hand(Card(12, "H")) hand.add_card_two(Card(5, "S")) assert hand.total == 15 assert not hand.soft hand.add_card(Card(4, "D")) assert hand.total == 19 hand.add_card(Card(10, "S")) assert hand.bust
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
def test_simulation(): game = Game([Player(100)]) game.simulate(10)
""" Code for creating plots found in 'Introduction to Py21' """ 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)
def basic_player(): return Player(100)