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())
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() # Get choice from Player player_choice = raw_input("Enter Choice : H(it) or S(tand) - ").lower() if player_choice == 'h': rank, suit = deal_card().split(',') player.add_card(Card(rank,suit)) print "Dealers Hand : %s", ' & '.join([card[0] + ' of ' + card[1] for card in dealer.cards])
class PlayerTests(unittest.TestCase): def setUp(self): self.player = Player() def test_init_accepts_hand(self): cards = ace, king = u'A♠', u'K♠' player = Player(list(cards)) self.assertEquals(2, len(player.hand)) self.assertEquals(ace, player.hand[0]) self.assertEquals(king, player.hand[1]) def test_add_card(self): hand_size = len(self.player.hand) card = Card(u'A♠') self.player.add_card(card) self.assertEquals(hand_size + 1, len(self.player.hand)) self.assertEquals(card, self.player.hand[0]) def test_can_hit_with_hand_value_lt_21(self): '''A player can hit if their hand value is less than 21.''' self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'Q♠')) self.assertEquals(20, self.player.hand_value()) self.assertTrue(self.player.can_hit()) def test_can_hit_with_hand_value_gte_21(self): '''A player cannot hit if their hand value is 21 or higher.''' self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'6♠')) self.player.add_card(Card(u'5♠')) self.assertFalse(self.player.can_hit()) def test_get_hand_value_with_hard_ace(self): '''An ace is hard (1) if a soft ace (11) would cause the player to bust.''' self.player.add_card(Card(u'A♠')) self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'Q♠')) self.assertEquals(21, self.player.hand_value()) def test_get_hand_value_with_soft_ace(self): '''An ace is soft (11) if it won't cause the player to bust.''' self.player.add_card(Card(u'A♠')) self.player.add_card(Card(u'K♠')) self.assertEquals(21, self.player.hand_value()) def test_get_hand_value_with_many_aces(self): '''Only one ace in a hand can be soft (11). The rest must be hard (1).''' self.player.add_card(Card(u'A♠')) self.player.add_card(Card(u'A♥')) self.player.add_card(Card(u'A♣')) self.player.add_card(Card(u'A♦')) self.assertEquals(14, self.player.hand_value()) def test_get_hand_value_with_no_aces(self): self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'Q♠')) self.assertEquals(20, self.player.hand_value()) def test_has_blackjack_with_blackjack(self): self.player.add_card(Card(u'A♠')) self.player.add_card(Card(u'K♠')) self.assertTrue(self.player.has_blackjack()) def test_has_blackjack_with_hand_value_ne_21(self): self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'Q♠')) self.assertFalse(self.player.has_blackjack()) def test_has_blackjack_with_gt_2_cards(self): self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'6♠')) self.player.add_card(Card(u'5♠')) self.assertFalse(self.player.has_blackjack()) def test_has_busted_with_hand_value_lte_21(self): self.player.add_card(Card(u'A♠')) self.player.add_card(Card(u'K♠')) self.assertFalse(self.player.has_busted()) def test_has_busted_with_hand_value_gt_21(self): self.player.add_card(Card(u'K♠')) self.player.add_card(Card(u'Q♠')) self.player.add_card(Card(u'2♠')) self.assertTrue(self.player.has_busted())
def check_hands_for_winning_hand_test(self): p1 = Player() p1.add_card(Card("ace", "spades")) p1.add_card(Card("jack", "hearts")) p2 = Player() p2.add_card(Card("ace", "spades")) p2.add_card(Card("nine", "hearts")) self.assertEqual(p1, get_winning_hand(p1, p2)) p3 = Player() p3.add_card(Card("ace", "diamonds")) p3.add_card(Card("jack", "clubs")) self.assertEqual("draw", get_winning_hand(p1, p3))
def get_hand_score_test(self): p1 = Player() p1.add_card(Card("ace", "spades")) p1.add_card(Card("jack", "hearts")) self.assertEqual(21, p1.calculate_hand_score())
def create_player_test(self): p1 = Player() p1.add_card(Card("ace", "spades")) p1.add_card(Card("jack", "hearts")) self.assertEqual([("ace", "spades"), ("jack", "hearts")], p1.cards)