Exemple #1
0
class DealerTests(unittest.TestCase):
  def setUp(self):
    self.dealer = Dealer()

  def test_build_deck_has_52_cards(self):
    deck = Dealer.build_deck()
    self.assertEquals(52, len(deck))

  def test_can_hit_with_hand_value_lte_16(self):
    self.dealer.add_card(Card(u'K♠'))
    self.dealer.add_card(Card(u'6♠'))
    self.assertTrue(self.dealer.can_hit())

  def test_can_hit_with_hand_value_gt_16(self):
    self.dealer.add_card(Card(u'K♠'))
    self.dealer.add_card(Card(u'7♠'))
    self.assertFalse(self.dealer.can_hit())

  def test_deal_card(self):
    player = Player()
    hand_size = len(player.hand)

    self.dealer.deal_card(player)
    self.assertEquals(hand_size + 1, len(player.hand))

  def test_reveal_hole_card(self):
    self.assertFalse(self.dealer.is_hole_card_visible)
    self.dealer.reveal_hole_card()
    self.assertTrue(self.dealer.is_hole_card_visible)
    def test_get_action(self):
        """A test which makes sure that the get_action method of the Dealer class functions properly."""

        # Try several test cases
        test_cases = [
            [Card("Clubs", "2"), Card("Clubs", "2"), "Hit"],
            [Card("Clubs", "10"), Card("Clubs", "10"), "Stand"],
            [Card("Clubs", "10"), Card("Clubs", "7"), "Stand"],
            [Card("Clubs", "10"), Card("Clubs", "6"), "Hit"],
            [Card("Clubs", "Ace"), Card("Clubs", "6"), "Hit"],
            [Card("Clubs", "Ace"), Card("Clubs", "6"), Card("Clubs", "10"), "Stand"],
            [Card("Clubs", "Jack"), Card("Clubs", "7"), "Stand"],
            [Card("Clubs", "Queen"), Card("Clubs", "6"), "Hit"],
            [Card("Clubs", "King"), Card("Clubs", "6"), "Hit"],
            [Card("Clubs", "Ace"), Card("Clubs", "Ace"), Card("Clubs", "6"), "Hit"],
            [Card("Clubs", "Ace"), Card("Clubs", "Ace"), Card("Clubs", "6"), "Hit"],
            [Card("Clubs", "7"), Card("Clubs", "8"), "Hit"],
            [Card("Clubs", "7"), Card("Clubs", "8"), Card("Clubs", "Ace"), "Hit"],
            [Card("Clubs", "7"), Card("Clubs", "8"), Card("Clubs", "2"), "Stand"],
            [Card("Clubs", "Jack"), Card("Clubs", "Queen"), "Stand"],
        ]
        for case in test_cases:
            dealer = Dealer()
            cards = len(case) - 1
            for index in range(cards):
                dealer.add_card(case[index])
            action = dealer.get_action()
            self.assertEqual(
                action.move,
                case[-1],
                msg="The test of the get_action method with the index of " + str(test_cases.index(case)) + " failed.",
            )
    def check_hands_test(self):
        p1 = Player()
        p1.add_card(Card("ace", "spades"))
        p1.add_card(Card("jack", "hearts"))

        self.assertEqual("ace of spades & jack of hearts", p1.print_hand())

        p2 = Dealer()
        p2.add_card(Card("one", "spades"))
        p2.add_card(Card("ten", "diamonds"))

        self.assertEqual("XX & ten of diamonds", p2.print_hand())
	playing_card = random.choice(deck)
	deck.remove(playing_card)	
	return playing_card


if __name__ == '__main__':

	play_game = True
	
	while play_game:
		print "---- Start of Round ----"
		print "dealing cards ...."
		dealer = Dealer()
		# Deal 2 cards for Dealer
		rank, suit = deal_card().split(',')
		dealer.add_card(Card(rank,suit))

		rank, suit = deal_card().split(',')
		dealer.add_card(Card(rank,suit))

		# Deal player cards
		player = Player()
		rank, suit = deal_card().split(',')
		player.add_card(Card(rank,suit))

		rank, suit = deal_card().split(',')
		player.add_card(Card(rank,suit))

		# Show hands
		print "Dealers Hand : %s", dealer.print_hand()
		print "Players Hand : %s", player.print_hand()