def setUp(self): self.players_hand = classes.Hand() self.dealers_hand = classes.Hand() self.card1 = classes.Card("\u2663", "8") self.card2 = classes.Card("\u2665", "J") self.card3 = classes.Card("\u2666", "10") self.card4 = classes.Card("\u2660", "2") self.card5 = classes.Card("\u2666", "K") self.card6 = classes.Card("\u2663", "A")
def test_calc_score(self): self._player3.cards.append(self.card1) self._player3.cards.append(self.card2) self._player4.cards.append(self.card5) self._player4.cards.append(self.card6) self._player5.cards.append(self.card5) self._player5.cards.append(self.card6) self._player5.cards.append(self.card4) self.assertEqual(classes.Hand().calc_score(self._player3), 18) self.assertEqual(classes.Hand().calc_score(self._player4), 21) self.assertEqual(classes.Hand().calc_score(self._player5), 13)
def test_hit(self): text_trap = io.StringIO() sys.stdout = text_trap self._player5.cards.append(self.card5) self._player5.cards.append(self.card6) self._player3.cards.append(self.card3) self._player3.cards.append(self.card2) self._player4.cards.append(self.card6) self._player5.cards.append(self.card4) self.assertEqual(classes.Hand().hit(self._player5, self.card1), 4) self.assertEqual(classes.Hand().hit(self._player3, self.card1), 3) self.assertEqual(classes.Hand().hit(self._player4, self.card1), 2)
def make_hand(deck): card1 = deck.draw_card() deck.add_card(card1) card2 = deck.draw_card() deck.add_card(card2) hand = c.Hand([card1, card2]) return hand
def deal(self): random.shuffle(self.deck) card_one = self.deck.pop(0) card_two = self.deck.pop(0) return classes.Hand([card_one, card_two])
def setUp(self): self.deck = classes.Deck() self.deck.shuffle() self.players_hand = classes.Hand('player') self.dealers_hand = classes.Hand('dealer') self.players_hand.draw_card(self.deck.deal()) self.players_hand.draw_card(self.deck.deal()) utils.first_hand(self.players_hand, self.dealers_hand) utils.play_hand(self.deck, self.players_hand) utils.dealer_sim(self.deck, self.players_hand, self.dealers_hand) utils.reset_deck(self.players_hand, self.dealers_hand) self.card1 = classes.Card("\u2663", "7") self.card2 = classes.Card("\u2665", "J") self.card3 = classes.Card("\u2666", "3") self.card4 = classes.Card("\u2660", "5") self.card5 = classes.Card("\u2666", "A") self.card6 = classes.Card("\u2663", "Q") text_trap = io.StringIO() sys.stdout = text_trap
def simulate_hands(hand, communal_cards, deck, num_simulations): result = 0.0 for _ in range(num_simulations): sample_cards = deck.draw(num_cards=2) sample_hand = classes.Hand(sample_cards[0], sample_cards[1]) additional_communal_cards = deck.draw(num_cards=5 - len(communal_cards)) final_communal_cards = communal_cards + additional_communal_cards sample_hand_rank = hand_rank.get_rank(sample_hand.read_as_list() + final_communal_cards) this_hand_rank = hand_rank.get_rank(hand.read_as_list() + final_communal_cards) comparison = hand_rank.Rank.compare_ranks(this_hand_rank, sample_hand_rank) if comparison == 1: result += 1 elif comparison == 0: result += 0.5 restore_deck(deck, sample_cards + additional_communal_cards) return result
def main(): global playing print("Welcome to Blackjack") player_name = input("What's your name? ") player_hand = classes.Hand(player_name) dealer_hand = classes.Hand("dealer") while True: try: player_total = int(input("How much are you bringing to the table? ")) player_chips = classes.Chips(player_total) break except: print("We are sorry, this is an inappropriate value for your chips.") while True: deck = classes.Deck() deck.shuffle() functions.take_bet(player_chips) print("\n") player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) functions.show_some(player_hand, dealer_hand) while playing: playing = functions.player_action(deck, player_hand) print("\n") functions.show_some(player_hand, dealer_hand) if player_hand.value > 21: functions.player_busts(player_hand, dealer_hand, player_chips) break if player_hand.value <= 21: functions.show_all(player_hand, dealer_hand) input("Press Enter to Continue ...") if dealer_hand.value < 17: print("The dealer has elected to hit:") dealer_hand.add_card(deck.deal()) functions.show_all(player_hand, dealer_hand) input("Press Enter to Continue ...") if dealer_hand.value > 21: functions.dealer_busts(player_hand, dealer_hand, player_chips) elif dealer_hand.value > player_hand.value: functions.dealer_wins(player_hand, dealer_hand, player_chips) elif dealer_hand.value == player_hand.value: functions.push(player_hand, dealer_hand, player_chips) else: functions.player_wins(player_hand, dealer_hand, player_chips) if player_chips.total == 0: print("You have no more chips to play with, thank you for playing.") break action = input("Do you wish to play again? (y/n) ") if action[0].lower() == "n": break player_hand.clean_hand() dealer_hand.clean_hand() playing = True
def test_hand(self): self.assertEqual(classes.Hand().hand(self.players_hand, (4, 5), (5, 6)), [(4, 5), (5, 6)]) self.assertIsInstance(classes.Hand().hand(self.dealers_hand, self.card1, self.card2), list)
def dealer_wins(player, dealer, chips): print("Dealer wins!") chips.lose_bet() def push(player, dealer): print("Dealer and Player tie! It's a push.") b = classes.Deck() a = classes.Card("blue", "yellow") print(a) print(b) b.shuffle_deck() print(b) test_player = classes.Hand() test_player.add_card(b.deal()) test_player.add_card(b.deal()) for card in test_player.cards: print(card) print(test_player.value) test_player.adjust_aces() print(test_player.value) player_chips = classes.Chips(100) money = [] money.append(player_chips.amount) while True: print("Welcome to blackjack baby") deck = classes.Deck() deck.shuffle_deck()
import game_func import globals_var chips_check = 0 while globals_var.game: # Print an opening statement print( "\nHello and Welcome to Black Jack! Get as close to 21 before the dealer does! \nThe Dealer hits till they reach 17. Aces count for 11 or 1" ) # Create & shuffle the deck, deal two cards to each player deck = classes.Deck() deck.shuffle() player = classes.Hand() dealer = classes.Hand() for num in range(0, 2): player.add_card(deck.deal()) dealer.add_card(deck.deal()) # Set up the Player's chips player_chips = classes.Chips() # Deafult Chips is set to 150 if chips_check <= 0: player_chips.rn_total = player_chips.total else: player_chips.rn_total = chips_check # Prompt the Player for their bet print(f'\nYou have a starting value of {player_chips.rn_total} chips ')