def test_simple_strat_hit(self):
        player_hand = Hand()
        player_hand.add_card(Card("Spades", "2"))
        player_hand.add_card(Card("Clubs", "9"))

        self.strategy.play(player_hand, None)
        self.assertEqual(len(player_hand.cards), 3)
Exemple #2
0
    def test_calculate_earnings(self):
        d = Deck(4)
        d.shuffle()
        d.cut()

        h1 = Hand()
        h1.add_card(Card("Spades", "2"))
        h1.add_card(Card("Clubs", "2"))
        h1.add_bet(5.0)

        h2 = Hand()
        h2.add_card(Card("Spades", "2"))
        h2.add_card(Card("Clubs", "A"))
        h2.add_bet(5.0)

        h3 = Hand()
        h3.add_card(Card("Spades", "2"))
        h3.add_card(Card("Clubs", "J"))
        h3.add_bet(5.0)

        h4 = Hand()
        h4.add_card(Card("Spades", "2"))
        h4.add_card(Card("Clubs", "Q"))
        h4.add_bet(5.0)

        h5 = Hand()
        h5.add_card(Card("Spades", "2"))
        h5.add_card(Card("Clubs", "K"))
        h5.add_bet(5.0)

        strategy = BasicStrategy(d)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h1, WIN, False)
        self.assertEqual(earnings, 5.0)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h1, LOSS, False)
        self.assertEqual(earnings, 5.0)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h1, WIN, True)
        self.assertEqual(earnings, 5.0)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h2, WIN, False)
        self.assertEqual(earnings, 7.5)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h3, WIN, False)
        self.assertEqual(earnings, 7.5)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h4, WIN, False)
        self.assertEqual(earnings, 7.5)

        game = Game(d, strategy)
        earnings = game.calculate_earnings(h5, WIN, False)
        self.assertEqual(earnings, 7.5)
Exemple #3
0
 def test_card_value(self):
     c = Card("Clubs", "A")
     self.assertEqual(c.get_value(), 11)
     c2 = Card("Aces", "J")
     self.assertEqual(c2.get_value(), 10)
     c3 = Card("Aces", "2")
     self.assertEqual(c3.get_value(), 2)
Exemple #4
0
 def test_hand_add_cards(self):
     """Test adding cards
     """
     player_hand = Hand()
     dealer_hand = Hand()
     player_hand.add_card(Card("Spades", "2"))
     dealer_hand.add_card(Card("Clubs", "A"))
     self.assertEqual(len(player_hand.cards), 1)
     self.assertEqual(len(dealer_hand.cards), 1)
     self.assertEqual(player_hand.get_value(), 2)
     self.assertEqual(dealer_hand.get_value(), 11)
    def test_basic_strat_normal_hit_two(self):
        """Test with hit scenario 2 (border edge of chart)
        """
        player_hand = Hand()
        player_hand.add_card(Card("Spades", "J"))
        player_hand.add_card(Card("Clubs", "6"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "2"))
        dealer_hand.add_card(Card("Clubs", "7"))

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
    def test_basic_strat_normal_hit(self):
        """Test with hit scenario
        """
        player_hand = Hand()
        player_hand.add_card(Card("Spades", "4"))
        player_hand.add_card(Card("Clubs", "4"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "Q"))
        dealer_hand.add_card(Card("Clubs", "10"))

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
    def test_basic_strat_normal_stand(self):
        """Test with stand scenario (border edge of chart)
        """
        player_hand = Hand()
        player_hand.add_card(Card("Spades", "J"))
        player_hand.add_card(Card("Clubs", "6"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "2"))
        dealer_hand.add_card(Card("Clubs", "6"))

        self.strategy.play(player_hand, dealer_hand)
        self.assertEqual(len(player_hand.cards), 2)
Exemple #8
0
    def test_check_blackjack(self):
        """Test blackjack
        """
        h = Hand()
        h.add_card(Card("Spades", "10"))
        h.add_card(Card("Clubs", "A"))
        result = Game.check_blackjack(h)
        self.assertTrue(result)

        h = Hand()
        h.add_card(Card("Spades", "9"))
        h.add_card(Card("Clubs", "A"))
        result = Game.check_blackjack(h)
        self.assertFalse(result)
    def test_basic_strat_scen_1(self):
        """Test basic scenario, A, 3, dealer: 6, 10
        """
        player_hand = Hand()
        player_hand.add_bet(2.0)
        player_hand.add_card(Card("Spades", "A"))
        player_hand.add_card(Card("Clubs", "3"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "6"))
        dealer_hand.add_card(Card("Clubs", "10"))

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
Exemple #10
0
    def test_initial_blackjack(self):
        """Test initial blackjack
        """
        h1 = Hand()
        h1.add_card(Card("Spades", "11"))
        h1.add_card(Card("Clubs", "10"))

        h2 = Hand()
        h2.add_card(Card("Spades", "11"))
        h2.add_card(Card("Clubs", "8"))

        h1_blackjack, h2_blackjack = Game.check_inital_blackjack(h1, h2)
        self.assertTrue(h1_blackjack)
        self.assertFalse(h2_blackjack)
    def test_basic_strat_double_hit(self):
        """Test double down 2
        """
        player_hand = Hand()
        player_hand.add_bet(2.0)
        player_hand.add_card(Card("Spades", "A"))
        player_hand.add_card(Card("Clubs", "3"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "2"))
        dealer_hand.add_card(Card("Clubs", "4"))

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
        self.assertEqual(player_hand.bet, 2.0)
Exemple #12
0
    def test_bust(self):
        """Test busted
        """
        h = Hand()
        h.add_card(Card("Spades", "11"))
        h.add_card(Card("Clubs", "10"))
        h.add_card(Card("Hearts", "2"))
        result = Game.check_bust(h)
        self.assertTrue(result)

        h = Hand()
        h.add_card(Card("Spades", "11"))
        h.add_card(Card("Clubs", "10"))
        result = Game.check_bust(h)
        self.assertFalse(result)
    def test_basic_strat_split_nonace(self):
        """Test split non-ace
        """
        strategy = BasicStrategy(self.deck)
        player_hand = Hand()
        player_hand.add_card(Card("Spades", "3"))
        player_hand.add_card(Card("Clubs", "3"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "2"))
        dealer_hand.add_card(Card("Clubs", "3"))

        is_playing = strategy.play_hand(SPLIT, player_hand, dealer_hand)
        self.assertEqual(len(strategy.split_hands), 2)
        self.assertEqual(is_playing, True)
    def test_basic_strat_split(self):
        """Test split
        """
        strategy = BasicStrategy(self.deck)
        player_hand = Hand()
        player_hand.add_card(Card("Spades", "A"))
        player_hand.add_card(Card("Clubs", "A"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "2"))
        dealer_hand.add_card(Card("Clubs", "9"))

        cont_playing = strategy.play_hand(SPLIT, player_hand, dealer_hand)
        self.assertEqual(len(strategy.split_hands), 2)
        self.assertEqual(cont_playing, True)
        self.assertEqual(strategy.cont_split, False)
Exemple #15
0
    def test_hand_ace_case(self):
        """Test ace bust case
        """
        player_hand = Hand()
        dealer_hand = Hand()
        player_hand.add_card(Card("Spades", "2"))
        dealer_hand.add_card(Card("Clubs", "A"))
        player_hand.add_card(Card("Hearts", "2"))
        dealer_hand.add_card(Card("Clubs", "J"))

        self.assertEqual(len(player_hand.cards), 2)
        self.assertEqual(len(dealer_hand.cards), 2)
        self.assertEqual(player_hand.get_value(), 4)
        self.assertEqual(dealer_hand.get_value(), 21)

        # Switch Ace from 11 to 1 to avoid bust
        dealer_hand.add_card(Card("Clubs", "2"))
        self.assertEqual(dealer_hand.get_value(), 13)
Exemple #16
0
    def test_calculate_result_hit(self):
        """Test result for normal stand
        """
        d = Deck(4)
        d.shuffle()
        d.cut()

        player_hand = Hand()
        player_hand.add_card(Card("Spades", "Q"))
        player_hand.add_card(Card("Clubs", "2"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "10"))
        dealer_hand.add_card(Card("Hearts", "7"))

        strategy = BasicStrategy(d)

        game = Game(d, strategy)
        game.calculate_results(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
Exemple #17
0
    def test_calculate_result_split_ace(self):
        """Test result for split
        """
        d = Deck(4)
        d.shuffle()
        d.cut()

        player_hand = Hand()
        player_hand.add_card(Card("Spades", "A"))
        player_hand.add_card(Card("Clubs", "A"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "10"))
        dealer_hand.add_card(Card("Hearts", "3"))

        strategy = BasicStrategy(d)

        game = Game(d, strategy)
        game.calculate_results(player_hand, dealer_hand)
        game.display_results()
        self.assertGreaterEqual(len(strategy.split_hands), 2)
        self.assertEqual(len(strategy.split_hands[0].cards), 2)
        self.assertEqual(len(strategy.split_hands[1].cards), 2)
Exemple #18
0
    def test_calculate_result_split_noneace(self):
        """Test result for split without aces
        """
        d = Deck(4)
        d.shuffle()
        d.cut()

        player_hand = Hand()
        player_hand.add_card(Card("Spades", "2"))
        player_hand.add_card(Card("Clubs", "2"))

        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "10"))
        dealer_hand.add_card(Card("Hearts", "3"))

        strategy = BasicStrategy(d)

        game = Game(d, strategy)
        game.calculate_results(player_hand, dealer_hand)
        game.display_results()
        self.assertGreaterEqual(len(strategy.split_hands), 2)
        self.assertEqual(
            game.game_info["losses"] + game.game_info["wins"] + game.game_info["ties"], 2)
 def test_dealer_strategy_generic(self):
     dealer_hand = Hand()
     dealer_hand.add_card(Card("Spades", "J"))
     dealer_hand.add_card(Card("Clubs", "A"))
     self.strategy.play(dealer_hand)
     self.assertEqual(len(dealer_hand.cards), 2)
Exemple #20
0
 def test_card_init(self):
     c = Card("Spades", "2")
     self.assertEqual(c.suit, "Spades")
     self.assertEqual(c.value, "2")
 def test_dealer_strategy_with_ace(self):
     dealer_hand = Hand()
     dealer_hand.add_card(Card("Spades", "2"))
     dealer_hand.add_card(Card("Clubs", "A"))
     self.strategy.play(dealer_hand)
     self.assertGreaterEqual(len(dealer_hand.cards), 3)