Beispiel #1
0
  def play(self, hand_size=3):
    """Starts a card game between two players.

    Args:
      hand_size: An int representing the number of cards a player will draw.
    """
    self.playing = True

    while self.playing:
      cards = Deck()
      cards.shuffle_deck()

      p1 = Player(input("\nPLAYER 1 NAME:  "))
      p2 = Player(input("\nPLAYER 2 NAME:  "))

      for _ in range(hand_size):
        p1.draw_card(cards.deck)
        p2.draw_card(cards.deck)

      p1.sort_hand(cards.suits, cards.suit_cards)
      p2.sort_hand(cards.suits, cards.suit_cards)

      p1.score_hand(cards.suits, cards.suit_cards)
      p2.score_hand(cards.suits, cards.suit_cards)

      self.show_hands(p1, p2)
      self.declare_winner(p1, p2)

      play_again = input("\nPlay again? [y/N]  ")

      if not strtobool(play_again):
        self.playing = False
Beispiel #2
0
class blackjack_game:
    def __init__(self, purse):

        #store the new deck
        self.game_deck = Deck()

        #initialize player with a purse
        self.Player = Player(purse)

        #initialize computer player with a purse
        self.Computer = Player(purse)

    def play_game(self, user_ante):
        """
        function used to implement game play.
        """

        #flag indicating game has started
        user_play = 1

        while ((self.Player.get_purse() > 0) and
               (self.Computer.get_purse() > 0)) and (user_play != 0):

            #create deck
            self.game_deck.create_deck()

            #shuffle deck
            self.game_deck.shuffle_deck()

            #add bet before game start
            self.Player.add_bet(user_ante)
            self.Computer.add_bet(user_ante)

            #deal first 2 card to player and computer
            self.deal_hand_initial(self.Player)
            self.deal_hand_initial(self.Computer)

            print("Player: ")
            self.Player.player_show_hand()
            print("CPU: ")
            self.Computer.player_show_hand()

            #print current purse and ante
            self.Player.player_stats()
            self.Computer.player_stats()

            #determine if we have a black jack winner
            if self.blackjack_payout(self.Player, self.Computer) == True:
                continue

            user_raise = int(input("Player, how much do you want to raise?"))

            #add ante to the current bet
            self.Player.add_bet(user_raise)
            self.Computer.add_bet(user_raise)

            user_choice = int(input("Player, hit (1) or stay (0)?"))

            while user_choice == 1:

                self.Player.draw_card(self.game_deck.deal_card())
                print("Player: ")
                self.Player.player_show_hand()

                user_choice = int(input("Player, hit (1) or stay (0)?"))

            #cpu keeps drawing cards until hand score > 16
            cpu_hand_score = self.calc_hand_score(self.Computer.get_hand())

            while cpu_hand_score < 16:

                #draw card
                self.Computer.draw_card(self.game_deck.deal_card())
                print("CPU: ")
                self.Computer.player_show_hand()

                #update hand score
                cpu_hand_score = self.calc_hand_score(self.Computer.get_hand())

            #calculate total value of hand
            player_hand_score = self.calc_hand_score(self.Player.get_hand())
            self.Player.set_hand_score(player_hand_score)
            self.Computer.set_hand_score(cpu_hand_score)

            #identify the winner of the round
            self.calc_winner_round()

            user_play = int(
                input("Do you want to continue? 1 = Continue, 0 = Quit"))

    def blackjack_payout(self, player, computer):
        """Calculates payout for player if the first 2 cards is 21."""
        if self.determine_black_jack(player) == True:

            if self.determine_black_jack(computer) == False:

                #add the bet to the game
                computer.add_bet(computer.get_current_bet() * 1.5)

                #add computers bet to player
                player.add_to_purse(computer.get_current_bet())

                player.reset_bet()
                computer.reset_bet()

                player.reset_hand()
                computer.reset_hand()

            return True

        else:
            if self.determine_black_jack(computer) == True:

                #add the bet to the game
                player.add_bet(computer.get_current_bet() * 1.5)

                #add players bet to computer
                computer.add_to_purse(player.get_current_bet())

                player.reset_bet()
                computer.reset_bet()

                player.reset_hand()
                computer.reset_hand()

                return True

    def determine_black_jack(self, player):
        """determines if player or computer gets 21 with first 2 cards."""
        return self.calc_hand_score(player.get_hand()) == 21

    def deal_hand_initial(self, Player):
        """Deal the first 2 cards to the player."""
        Player.set_hand(self.game_deck.deal_card())
        Player.set_hand(self.game_deck.deal_card())

    def calc_winner_round(self):
        """
        Determine the winner for each round.
        """
        player_score = self.Player.get_hand_score()
        cpu_score = self.Computer.get_hand_score()

        print("Player Hand: ", player_score)
        print("CPU hand: ", cpu_score)

        if player_score > 21:

            print("Player 1 Loss: ", self.Player.get_current_bet())

            #add to Players purse
            self.Computer.add_to_purse(self.Player.get_current_bet())

        elif cpu_score > 21:

            print("Computer Loss: ", self.Player.get_current_bet())

            #add to Players purse
            self.Player.add_to_purse(self.Computer.get_current_bet())

        #if player and cpu does not bankrupt
        else:

            if player_score > cpu_score:
                print("Computer Loss: ", player_score, cpu_score,
                      self.Computer.get_current_bet())

                #add to Players purse
                self.Player.add_to_purse(self.Computer.get_current_bet())

            else:
                print("Player 1 Loss: ", player_score, cpu_score,
                      self.Player.get_current_bet())

                #add to Players purse
                self.Computer.add_to_purse(self.Player.get_current_bet())

        #reset bet for new round
        self.Player.reset_bet()
        self.Computer.reset_bet()

        #reset hand for new round
        self.Player.reset_hand()
        self.Computer.reset_hand()

    def calc_hand_score(self, list_of_cards):

        hand_score = 0

        ace = " "

        for card in list_of_cards:

            #get value of card
            current_card = card.get_card()[3]

            #check if adding an ace makes the player bust
            if current_card == 'A':

                if hand_score + 11 < 22:

                    hand_score += 11

                    #skip everything else in the loop
                    continue

            #this case looks at all face cards including A, A is treated as 1
            if type(current_card) == str:
                #convert K,Q,J,A
                hand_score += self.facecard_to_int(current_card)
            else:
                hand_score += current_card

        return hand_score

    def facecard_to_int(self, current_card):

        face_cards = {"K": 10, "Q": 10, "J": 10, "A": 1}
        try:

            #if K,Q,J or A
            return face_cards.get(current_card)

        except:

            #if numeric card
            return current_card
Beispiel #3
0
def run():
    print(
        "Welcome to Blackjack! Get as close to 21 as you can without going bust! Dealer hits until they reach 17 or over. Aces count as 1 or 11 depending on your current hand value.\n"
    )
    player_chips = Chips(
    )  # Initialise player's chip pool. It is outside of the main loop so it carries between hands
    while True:
        playing = True  # used for the player's turn
        card_deck = Deck()
        card_deck.shuffle_deck()  # setting up the deck

        betting = input("Player, would you like to bet on this round? (y/n) "
                        )  # asks if the player wants to bet
        if betting == "y":
            set_up_bet(player_chips)

        # intialising the player's and dealer's hands
        player = Hand()
        dealer = Hand()

        deal_cards(player, dealer, card_deck)

        # player's turn; the loop will continue until the player stands or goes bust
        while playing:
            show_some_cards(player, dealer)

            # runs if the player is bust
            if player.total_card_value > 21:
                player_busts(player_chips)
                break

            playing = hit_or_stand(
                player, card_deck)  # asks if the player wants to hit or stand

        if player.total_card_value <= 21:

            # the dealer hits until their hand value is greater than 17
            while dealer.total_card_value < 17:
                hit(dealer, card_deck)

            show_cards(player, dealer)  # shows players and dealers cards

            # checks the outcome of the game and calls the respective method (which sorts the bet outcome if there was one)
            if dealer.total_card_value > 21:
                dealer_busts(player_chips)
            elif dealer.total_card_value > player.total_card_value:
                dealer_wins(player_chips)
            elif dealer.total_card_value < player.total_card_value:
                player_wins(player_chips)
            else:
                print("Dealer and Player tie! It's a push.")

        # checks if the player wants to play another hand
        play_again = input("\nWould you like to play another hand? y/n: ")
        if play_again.lower() == "y":
            playing = True
            print("\n" * 100)
        else:
            print("Thanks for playing! You have left the table with {} chips.".
                  format(player_chips.chips_pool))
            break