def play(player, dealer, deck): playing = True while playing: print(f"Welcome {player.name} to the Blackjack game") game = Game(player, dealer) print("Distributing cards ") game.give_hand(deck) print(f"Your balance is ${player.balance}") game.set_bet() deck.new_deck() deck.shuffle_deck() # game.give_card(deck) player_play = True dealer_play = True while player_play or dealer_play: try: print("-----------------------------------------") print("Your hand is ") print(player.get_hand()) print(f"Your total points: {player.get_points()}") print("-----------------------------------------") print("The dealer hand is ") print(dealer.get_hand(0)) if player_play: if game.continue_ask() == True and player.get_points() < 21: game.give_card(deck, game.player) print("-----------------------------------------") else: player_play = False if dealer_play == True: if dealer.get_points() < 17: game.give_card(deck, game.dealer) print("-----------------------------------------") else: dealer_play = False except: print("some error happened") else: if game.can_continue(game.player): print("can continue") else: print("You can't continue you reach the limit") player_play = False game.check_winner() print(f"Your balance is ${player.balance}") if player.balance <= 0: print("You need money yo play!!!!") playing = False if game.continue_playing() == False and player.balance > 0: playing = False
def game(): """Main function, generates a game of blackjack.""" """1st stage, setting starting conditions""" dealer = Dealer("DEALER") if human_player: player_1 = Player(human_player_name) else: player_1 = Player(assets.player_names[randint(0, len(assets.player_names) - 1)]) print("_" * 40) deck = deck.new_deck(deck_copies) dealer.draw_card(deck, 2) dealer.count_points() dealer.display_hidden_hand() player_1.draw_card(deck, 2) player_1.count_points() """2nd stage, players taking turns""" print("_" * 40) player_1.display_hand() if human_player: while not player_1.finished_turn: entry = input("Choose your action: Hit[1] Stand[2] Double[3] Split[4] Surrender[5] Quit[Q]: ") entry = entry.upper() moves = { "1": partial(player_1.hit, deck), "2": partial(player_1.stand), "3": partial(player_1.double, deck), "4": partial(player_1.split, deck), "5": partial(player_1.surrender, deck), "Q": exit #TODO: Sort this out } moves[entry]() # TODO: Sort KeyError (try/except or if/else statement) else: while player_1.total < 17: player_1.hit(deck) print("_" * 40) while dealer.total < 17: dealer.hit(deck) """3rd stage, calculating and displaying results""" print("\n" + "_" * 25 + "RESULTS:" + "_" * 25) player_1.display_hand() results.winner(player_1, dealer) dealer.display_hand()
def play(self): print("Starting new game") self.deck = new_deck() self.player_hand = new_hand([]) self.dealer_hand = new_hand([]) self.winner = "Nobody" for _ in range(2): self.player_hand.add_card(self.deck.deal_card()) self.dealer_hand.add_card(self.deck.deal_card()) player = True while player: self.print_hands() if self.player_blackjack(): print("Player has Blackjack!") player = False self.winner = "Player" break if self.player_bust(): print("Player gone bust.") player = False dealer = False self.winner = "Dealer" break print() choice = input("Hit or Stand? ").lower() if choice in ["s", "stand"]: player = False elif choice in ["h", "hit"]: self.player_hand.add_card(self.deck.deal_card()) dealer = True print("Dealer's turn.") while dealer: self.print_hands() if self.dealer_blackjack(): print("Dealer has Blackjack!") dealer = False break if self.dealer_bust(): dealer = False break if self.dealer_hand.get_score() < 21: self.dealer_hand.add_card(self.deck.deal_card()) if self.player_blackjack() and self.dealer_blackjack(): self.winner = "Both" self.announce_winner() if self.player_bust() and self.dealer_bust(): self.winner = "Nobody" self.announce_winner() if self.player_bust() != True and self.dealer_bust(): self.winner = "Player" self.announce_winner() if self.dealer_bust() != True and self.player_bust(): self.winner = "Dealer" self.announce_winner() if self.player_hand.get_score() > self.dealer_hand.get_score() and self.player_hand.get_score() <= 21: self.winner = "Player" self.announce_winner() elif self.player_hand.get_score() < self.dealer_hand.get_score() and self.dealer_hand.get_score() <= 21: self.winner = "Dealer" self.announce_winner() else: self.winner = "Both" self.announce_winner()
def get_new_deck(self): self.deck = deck.new_deck(self.deck_size)
def test_total_score_ok(self): deck = new_deck() hand = new_hand(deck.get_cards()) # total value for a deck with 52 cards == 364 self.assertEqual(hand.get_score(), 364, "Score should be 364")