def main(): # create a player P1 = Player('P1', 200) # start w 200 P1_bank = P1.bank deck = Deck() dealer = Player('dealer') value = Value() # show beginning bank # test count print(len(deck.show_deck())) print(c(deck.show_deck())) # win round P1.add_bet(20) P1_bank = P1.win() print(P1_bank) # check if a card in deck is discarded print('deck before drawing: %s' % (len(deck._deck))) print() print('you drew a %s' % (deck.deal_card())) print() print('deck after drawing: %s' % (len(deck._deck))) # checking hand P1.hand.append(deck.deal_card()) P1.hand.append(deck.deal_card()) print('current hand:') print(P1.show_hand()) P1_hand_value = value.hand_value(P1.hand) + value.ace_present(P1.hand) print(P1_hand_value) while P1_hand_value < 21 and value.hand_value(P1.hand) != 0: P1.hand.append(deck.deal_card()) print('you drew %s' % (P1.hand[-1])) print('hand value is now %s' % (P1_hand_value)) if value.hand_value(P1.hand) == 0: print('You busted')
class TestPlayer(unittest.TestCase): def setUp(self): self.player = Player() def test_hand_property(self): self.player.hand = "A♣" self.assertEqual(self.player.hand, ["A♣"]) def test_show_hand_one_card(self): """Test show_hand output with one card""" import sys from io import StringIO self.player.hand = "A♣" saved_stdout = sys.stdout try: fake_out = StringIO() sys.stdout = fake_out self.player.show_hand() output = fake_out.getvalue().strip() self.assertEqual(output, '1 card: A♣') finally: sys.stdout = saved_stdout def test_show_hand_more_than_two_card(self): """Test show_hand output with more than two cards""" import sys from io import StringIO self.player.hand = ["A♣", "2♣", "3♣"] saved_stdout = sys.stdout try: fake_out = StringIO() sys.stdout = fake_out self.player.show_hand() output = fake_out.getvalue().strip() self.assertEqual(output, '3 cards: A♣, 2♣, 3♣') finally: sys.stdout = saved_stdout def test_hit_card(self): deck = ["A♣", "2♣", "3♣"] self.player.hit(deck) self.assertEqual(self.player.hand, ["A♣"]) self.assertEqual(len(deck), 2)
def main(): # name variables deck, value = Deck(), Value() shuffled_deck = deck.shuffle_deck() spacer = '--------------------------' print (spacer) print ("BLACKJACK!!!") print (spacer) bank = eval(input('how much do you want to play with: ')) print (spacer) P1 = Player('P1', bank) dealer = Player('dealer') print ('You now have %s to bet' %(P1.bank)) print (spacer) answer = ['Y', 'y', 'n', 'N'] # input bank keep_playing = True a = 'Y' # does user want to play again while keep_playing == True: if a not in answer: # did not select Y/N print ('that answer is not an option...') a = input('do you want to keep playing? (Y/N) ') # ask once more continue else: if a == 'N' or a == 'n': keep_playing = False break # cut to game # start game if P1.bank == 0: P1.bank = int(input('you ran out of money, enter more money: ')) bet = int(input('enter your bet: ')) print (P1.add_bet(bet)) print () # pass cards to dealer & player(s) P1.hand.append(deck.deal_card()) dealer.hand.append(deck.deal_card()) P1.hand.append(deck.deal_card()) dealer.hand.append(deck.deal_card()) # show your hand & dealer's last hand print ('your current hand:') print (spacer) print (P1.show_hand()) print ('value of the hand is: %s' % (value.actual_value(P1.hand))) print (spacer) print ('dealer\'s hand: %s'% (dealer.hand[-1])) print (spacer) print ('card count: %s' % (c(deck.history))) bj = False if value.actual_value(P1.hand) != 21: choose = input('Do you want to hit or stand? (H/S): ') else: bj = True # plauer's turn while ((choose == 'H') and not bj) or ((choose == 'h') and not bj): P1.hand.append(deck.deal_card()) print (spacer) print ('you drew a %s. Hand value: %s' % (P1.hand[-1], value.actual_value(P1.hand))) print (spacer) print ('Current Hand:') print (P1.show_hand()) if value.actual_value(P1.hand) == 0: print ('You busted') break print ('card count: %s' % (c(deck.history))) choose = input('Do you want to hit or stand? (H/S): ') # Dealer's turn print ('Dealer\'s turn') print (spacer) print ('dealer\'s hand: ') print (dealer.show_hand()) print ('dealer\'s current value is: %s' % (value.actual_value(dealer.hand) )) while value.actual_value(dealer.hand) < 17 and value.actual_value(P1.hand) != 0: # soft 17 dealer.hand.append(deck.deal_card()) print ('dealer drew a %s' % (dealer.hand[-1])) print (spacer) # did dealer break? if value.hand_value(dealer.hand) != 0: print ('dealer value: %s' % (value.actual_value(dealer.hand))) if value.hand_value(dealer.hand) == 0: print ('dealer busted') break # reveal winner and give earnings print ('Your hand value: %s || Dealer\'s hand value: %s' % (value.actual_value(P1.hand), value.actual_value(dealer.hand))) if value.actual_value(P1.hand) > value.actual_value(dealer.hand): P1.win() # add winnings to bank if value.hand_value(dealer.hand) == 0: print ('Dealer busted') print ('You win!') print ('you now have %s dollars total' %(P1.bank)) elif value.hand_value(P1.hand) == value.hand_value(dealer.hand): print ('Tie.') else: P1.lose() # subtract bet from P1 bank print ('Dealer wins') print ('you now have %s dollars total' %(P1.bank)) print ('end of the round') print () # clear old hands dealer.new_hand() P1.new_hand() a = input('do you want to keep playing? (Y/N) ') bj = False if len(deck.history) < 26: deck._deck = deck.shuffle_deck()
class TestPlayer(unittest.TestCase): def setUp(self): self.player = Player() def test_player_hand_property(self): self.player.hand = "8♥" self.assertEqual(self.player.hand, ["8♥"]) self.assertRaises(ValueError, setattr, self.player, "hand", 1) def test_player_money_property(self): self.assertEqual(self.player.money, Decimal("2000.0")) def test_hit_card(self): deck = ["8♥", "6♣", "2♦"] self.player.hit_card(deck) self.assertEqual(self.player.hand, ["8♥"]) self.assertEqual(self.player.points, 8) self.assertEqual(len(deck), 2) def test_show_points(self): deck = ["8♥", "6♣", "2♦"] for card in range(3): self.player.hit_card(deck) import sys from io import StringIO saved_stdout = sys.stdout try: fake_out = StringIO() sys.stdout = fake_out self.player.show_points() output = fake_out.getvalue().strip() self.assertEqual(output, "Points in game: 16") finally: sys.stdout = saved_stdout def test_show_hand(self): self.player.hand = ["8♥", "6♣", "2♦"] import sys from io import StringIO saved_stdout = sys.stdout try: fake_out = StringIO() sys.stdout = fake_out self.player.show_hand() output = fake_out.getvalue().strip() self.assertEqual(output, "The player have 3 cards: 8♥, 6♣, 2♦") finally: sys.stdout = saved_stdout def test_show_money(self): import sys from io import StringIO saved_stdout = sys.stdout try: fake_out = StringIO() sys.stdout = fake_out self.player.show_money() output = fake_out.getvalue().strip() self.assertEqual(output, "Money in game: 2000.0") finally: sys.stdout = saved_stdout def test_deal_cards(self): deck = ["8♥", "6♣", "2♦", "10♦", "3♣"] self.player.deal_cards(deck) self.assertEqual(self.player.hand, ["8♥", "6♣", "2♦"]) self.assertEqual(deck, ["10♦", "3♣"]) self.assertEqual(len(deck), 2) def test_bet(self): self.assertEqual(self.player.bet(100), 100) self.assertRaises(Exception, lambda: self.player.bet(10)) def test_get_card_value(self): self.assertEqual(self.player.get_card_value("6♣"), 6) def tearDown(self): pass