def test_phase_bet_play(self):
        gs = BjGameState()

        # This keeps the dealer from getting an Ace first and going to an insurance round
        gs.deck.cards[4].face = 'J'

        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))
        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))

        p1 = gs.players[0]
        p2 = gs.players[1]

        self.assertEqual(BjPhases.END, gs.current_phase)
        gs = gs.update(
            BjPlayerUpdate(BjPlayerActions.BET,
                           player_id=p1.id,
                           hand_idx=0,
                           bet_amt=100))
        self.assertEqual(BjPhases.BET, gs.current_phase)
        gs = gs.update(
            BjPlayerUpdate(BjPlayerActions.BET,
                           player_id=p2.id,
                           hand_idx=0,
                           bet_amt=50))
        self.assertEqual(BjPhases.PLAY, gs.current_phase)
    def test_out_of_turn(self):
        gs = BjGameState()
        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))
        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))

        p2 = gs.players[1]

        with self.assertRaises(BjGameError):
            gs.update(
                BjPlayerUpdate(BjPlayerActions.BET,
                               player_id=p2.id,
                               hand_idx=0,
                               bet_amt=100))
    def test_join(self):
        gs = BjGameState()

        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))
        self.assertEqual(1, len(gs.players))
        self.assertEqual(gs.players[0].id, gs.current_turn_player_id)
        self.assertEqual(1000, gs.players[0].total_credit)

        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=2000))
        self.assertEqual(2, len(gs.players))
        self.assertEqual(gs.players[0].id, gs.current_turn_player_id)
        self.assertEqual(1000, gs.players[0].total_credit)
        self.assertEqual(2000, gs.players[1].total_credit)
    def test_single_bet(self):
        gs = BjGameState()
        self.assertEqual(BjPhases.END, gs.current_phase)

        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))

        player = gs.players[0]

        gs = gs.update(
            BjPlayerUpdate(BjPlayerActions.BET,
                           player_id=player.id,
                           hand_idx=0,
                           bet_amt=100))

        self.assertEqual(900, gs.players[0].total_credit)
        self.assertEqual(100, gs.players[0].hands[0].bet_amt)
    def _setup_game(initial_bets=[], force_hands=None, initial_balance=1000):
        gs = BjGameState()

        for i in range(len(initial_bets)):
            gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))

        for i, p in enumerate(gs.players):
            gs = gs.update(
                BjPlayerUpdate(BjPlayerActions.BET,
                               player_id=p.id,
                               hand_idx=0,
                               bet_amt=initial_bets[i]))

        if force_hands is not None:
            gs.dealer.hands[0].cards = [Card(c) for c in force_hands.pop()]
            for i, cards in enumerate(force_hands):
                gs.players[i].hands[0].cards = [Card(c) for c in cards]

        return gs
    def test_bet_turns(self):
        gs = BjGameState()
        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))
        gs = gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))

        p1 = gs.players[0]
        p2 = gs.players[1]

        gs = gs.update(
            BjPlayerUpdate(BjPlayerActions.BET,
                           player_id=p1.id,
                           hand_idx=0,
                           bet_amt=100))
        gs = gs.update(
            BjPlayerUpdate(BjPlayerActions.BET,
                           player_id=p2.id,
                           hand_idx=0,
                           bet_amt=50))

        self.assertEqual(900, gs.players[0].total_credit)
        self.assertEqual(100, gs.players[0].hands[0].bet_amt)
        self.assertEqual(950, gs.players[1].total_credit)
        self.assertEqual(50, gs.players[1].hands[0].bet_amt)
 def test_invalid_action(self):
     gs = BjGameState()
     with self.assertRaises(BjGameError):
         gs.update(BjPlayerUpdate(-1))
    def test_failed_join(self):
        gs = BjGameState()
        gs.current_phase = BjPhases.PLAY

        with self.assertRaises(BjGameError):
            gs.update(BjPlayerUpdate(BjPlayerActions.JOIN, bet_amt=1000))
    def test_deck(self):
        gs = BjGameState()

        aces_in_deck = list(filter(lambda c: c.face == 'A', gs.deck.cards))
        self.assertEqual(6 * 52, len(gs.deck.cards))
        self.assertEqual(6 * 4, len(aces_in_deck))