Пример #1
0
class TestDeck(TestCase):
    """Test decks
    """
    @classmethod
    def setUpClass(self):
        self.deck = Deck(1)
        self.std_deck = Deck(4)

    def test_deck_init(self):
        self.assertEqual(len(self.deck.cards), 52)
        self.assertEqual(len(self.std_deck.cards), 208)

    def test_deck_shuffle(self):
        """Test shuffle deck
        """
        self.assertEqual(str(self.deck.cards[0]), "A of Spades")
        self.deck.shuffle()

    def test_deck_cut(self):
        """Test cut deck at random point
        """
        d = Deck(1)
        l = len(d.cards)
        d.cut()
        self.assertGreater(l, len(d.cards))

    def test_deck_deal(self):
        """Test deck deal card
        """
        d = Deck(1)
        c = d.deal()
        self.assertEqual(c.value, "A")
Пример #2
0
 def test_deck_cut(self):
     """Test cut deck at random point
     """
     d = Deck(1)
     l = len(d.cards)
     d.cut()
     self.assertGreater(l, len(d.cards))
Пример #3
0
class TestDealerStrategy(TestCase):
    """Test dealer strat
    """
    @classmethod
    def setUpClass(self):
        self.deck = Deck(1)
        self.deck.shuffle()
        self.deck.cut()
        self.strategy = DealerStrategy(self.deck)

    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)

    def test_dealer_strategy_with_ace_stand(self):
        dealer_hand = Hand()
        dealer_hand.add_card(Card("Spades", "8"))
        dealer_hand.add_card(Card("Clubs", "A"))
        self.strategy.play(dealer_hand)
        self.assertEqual(len(dealer_hand.cards), 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)
Пример #4
0
class TestSimpleStrategy(TestCase):
    """Test simplest strat
    """
    @classmethod
    def setUpClass(self):
        self.deck = Deck(1)
        self.deck.shuffle()
        self.deck.cut()
        self.strategy = SimpleStrategy(self.deck)

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

        self.strategy.play(player_hand, None)
        self.assertEqual(len(player_hand.cards), 2)

    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)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
0
 def test_deck_deal(self):
     """Test deck deal card
     """
     d = Deck(1)
     c = d.deal()
     self.assertEqual(c.value, "A")
Пример #10
0
 def setUpClass(self):
     self.deck = Deck(1)
     self.std_deck = Deck(4)
Пример #11
0
 def setUpClass(self):
     self.deck = Deck(1)
     self.deck.shuffle()
     self.deck.cut()
     self.strategy = BasicStrategy(self.deck)
Пример #12
0
class TestBasicStrategy(TestCase):
    """Test basic strat
    """
    @classmethod
    def setUpClass(self):
        self.deck = Deck(1)
        self.deck.shuffle()
        self.deck.cut()
        self.strategy = BasicStrategy(self.deck)

    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_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_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)

    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)

    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_double_1(self):
        """Test double down 1
        """
        player_hand = Hand()
        player_hand.add_bet(2.0)
        player_hand.add_card(Card("Spades", "A"))
        player_hand.add_card(Card("Clubs", "2"))

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

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
        self.assertEqual(player_hand.bet, 4.0)

    def test_basic_strat_double_2(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", "5"))

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
        self.assertEqual(player_hand.bet, 4.0)

    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)

    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)

    def test_basic_strat_scen_2(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", "7"))

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

        self.strategy.play(player_hand, dealer_hand)
        self.assertGreater(len(player_hand.cards), 2)
        self.assertEqual(player_hand.bet, 4.0)