Skip to content

Instantly share code, notes, and snippets.

@doscsy12
Last active May 24, 2026 03:30
Show Gist options
  • Select an option

  • Save doscsy12/da7b354100fa89c8e21924744646faea to your computer and use it in GitHub Desktop.

Select an option

Save doscsy12/da7b354100fa89c8e21924744646faea to your computer and use it in GitHub Desktop.
Blackjack game
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Blackjack milestone project!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Game Play\n",
"To play a hand of Blackjack the following steps must be followed:\n",
"1. Create a deck of 52 cards\n",
"2. Shuffle the deck\n",
"3. Ask the Player for their bet\n",
"4. Make sure that the Player's bet does not exceed their available chips\n",
"5. Deal two cards to the Dealer and two cards to the Player\n",
"6. Show only one of the Dealer's cards, the other remains hidden\n",
"7. Show both of the Player's cards\n",
"8. Ask the Player if they wish to Hit, and take another card\n",
"9. If the Player's hand doesn't Bust (go over 21), ask if they'd like to Hit again.\n",
"10. If a Player Stands, play the Dealer's hand. The dealer will always Hit until the Dealer's value meets or exceeds 17\n",
"11. Determine the winner and adjust the Player's chips accordingly\n",
"12. Ask the Player if they'd like to play again"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Playing Cards\n",
"A standard deck of playing cards has four suits (Hearts, Diamonds, Spades and Clubs) and thirteen ranks (2 through 10, then the face cards Jack, Queen, King and Ace) for a total of 52 cards per deck. Jacks, Queens and Kings all have a rank of 10. Aces have a rank of either 11 or 1 as needed to reach 21 without busting. As a starting point in your program, you may want to assign variables to store a list of suits, ranks, and then use a dictionary to map ranks to values."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
"ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
"values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,\n",
" 'Queen':10, 'King':10, 'Ace':11}\n",
"\n",
"playing = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Class Definitions\n",
"Consider making a Card class where each Card object has a suit and a rank, then a Deck class to hold all 52 Card objects, and can be shuffled, and finally a Hand class that holds those Cards that have been dealt to each player from the Deck."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class Card:\n",
" \n",
" def __init__(self, suit, rank):\n",
" self.suit = suit\n",
" self.rank = rank\n",
" self.value = values[rank]\n",
" \n",
" def __str__(self):\n",
" return self.rank + ' of ' + self.suit"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 3: Create a Deck Class**<br>\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"class Deck:\n",
" \n",
" def __init__(self):\n",
" self.deck = [] # start with an empty list\n",
" for suit in suits:\n",
" for rank in ranks:\n",
" self.deck.append(Card(suit,rank))\n",
"\n",
"# def __str__(self):\n",
"# for card in self.deck:\n",
"# return card.__str__() # add each Card object's print string\n",
"\n",
" def shuffle(self):\n",
" random.shuffle(self.deck)\n",
" \n",
" def deal(self):\n",
" return self.deck.pop() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 4: Create a Hand Class**<br>\n",
"In addition to holding Card objects dealt from the Deck, the Hand class may be used to calculate the value of those cards using the values dictionary defined above. It may also need to adjust for the value of Aces when appropriate."
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"class Hand:\n",
" def __init__(self):\n",
" self.cards = [] # start with an empty list as we did in the Deck class\n",
" self.value = 0 # start with zero value\n",
" self.aces = 0 # add an attribute to keep track of aces\n",
" \n",
" def add_card(self,card):\n",
" self.cards.append(card) #card passed in from Deck.deal()\n",
" self.value += values[card.rank]\n",
" if card.rank == 'Ace':\n",
" self.aces += 1 # not value, number of ace cards\n",
"\n",
" def adjust_for_ace(self):\n",
" if self.value > 21 and self.aces == True:\n",
" self.value -= 10 # total value of cards (-10)\n",
" self.aces -= 1 # not value, number of ace cards"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 5: Create a Chips Class**<br>\n",
"In addition to decks of cards and hands, we need to keep track of a Player's starting chips, bets, and ongoing winnings. This could be done using global variables, but in the spirit of object oriented programming, let's make a Chips class instead!"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"class Chips:\n",
" \n",
" def __init__(self):\n",
" self.total = 100 # This can be set to a default value \n",
" self.bet = 0\n",
" \n",
" def win_bet(self):\n",
" self.total += self.bet\n",
" \n",
" def lose_bet(self):\n",
" self.total -= self.bet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 6: Write a function for taking bets**<br>\n",
"Since we're asking the user for an integer value, this would be a good place to use <code>try</code>/<code>except</code>. Remember to check that a Player's bet can be covered by their available chips."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"def take_bet(chips):\n",
" try:\n",
" chips.bet = int(input(\"Amount of bet placed: \"))\n",
" except:\n",
" if chips.bet > chips.total:\n",
" print(\"Not enough chips!\")\n",
" else:\n",
" print(\"Bet placed\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 7: Write a function for taking hits**<br>\n",
"Either player can take hits until they bust. This function will be called during gameplay anytime a Player requests a hit, or a Dealer's hand is less than 17. It should take in Deck and Hand objects as arguments, and deal one card off the deck and add it to the Hand. You may want it to check for aces in the event that a player's hand exceeds 21."
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"def hit(deck,hand):\n",
" hand.add_card(deck.deal())\n",
" hand.adjust_for_ace()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 8: Write a function prompting the Player to Hit or Stand**<br>\n",
"This function should accept the deck and the player's hand as arguments, and assign playing as a global variable.<br>\n",
"If the Player Hits, employ the hit() function above. If the Player Stands, set the playing variable to False - this will control the behavior of a <code>while</code> loop later on in our code."
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"def hit_or_stand(deck,hand):\n",
" global playing # to control an upcoming while loop\n",
" \n",
" while playing:\n",
" x = input(\"Hit or stand? Type 'h' for hit, 's' for stand: \")\n",
" if x == 'h':\n",
" hit(deck, hand)\n",
" elif x == 's':\n",
" playing = False\n",
" else:\n",
" print(\"invalid input\")\n",
" continue\n",
" break\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 9: Write functions to display cards**<br>\n",
"When the game starts, and after each time Player takes a card, the dealer's first card is hidden and all of Player's cards are visible. At the end of the hand all cards are shown, and you may want to show each hand's total value. Write a function for each of these scenarios."
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"# I don't understand this.\n",
"def show_some(player,dealer):\n",
" print(\"\\nDealer's Hand:\")\n",
" print(\" <card hidden>\")\n",
" print('',dealer.cards[1]) \n",
" print(\"\\nPlayer's Hand:\", *player.cards, sep='\\n ')\n",
" print(\"Player's Hand =\",player.value)\n",
" \n",
"def show_all(player,dealer):\n",
" print(\"\\nDealer's Hand:\", *dealer.cards, sep='\\n ')\n",
" print(\"Dealer's Hand =\",dealer.value)\n",
" print(\"\\nPlayer's Hand:\", *player.cards, sep='\\n ')\n",
" print(\"Player's Hand =\",player.value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 10: Write functions to handle end of game scenarios**<br>\n",
"Remember to pass player's hand, dealer's hand and chips as needed."
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"def player_busts(player,dealer,chips):\n",
" print(\"Player loses\")\n",
" chips.lose_bet()\n",
" \n",
"def player_wins(player,dealer,chips):\n",
" print(\"Player wins\")\n",
" chips.win_bet()\n",
"\n",
"def dealer_busts(player,dealer,chips):\n",
" print(\"Dealer loses\")\n",
" chips.lose_bet()\n",
" \n",
"def dealer_wins(player,dealer,chips):\n",
" print(\"Dealer wins\")\n",
" chips.win_bet()\n",
" \n",
"def push(player,dealer):\n",
" print(\"It is a tie.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### And now on to the game!!"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to Blackjack!\n",
"Amount of bet placed: 10\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Eight of Clubs\n",
"\n",
"Player's Hand:\n",
" Seven of Clubs\n",
" Ten of Clubs\n",
"Player's Hand = 17\n",
"\n",
"Dealer's Hand:\n",
" Six of Hearts\n",
" Eight of Clubs\n",
" Two of Hearts\n",
" Seven of Hearts\n",
"Dealer's Hand = 23\n",
"\n",
"Player's Hand:\n",
" Seven of Clubs\n",
" Ten of Clubs\n",
"Player's Hand = 17\n",
"Dealer wins\n",
"Player has 110\n",
"Would you like to play another hand? Enter 'y' or 'n' y\n",
"Welcome to Blackjack!\n",
"Amount of bet placed: 5\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Jack of Spades\n",
"\n",
"Player's Hand:\n",
" Eight of Hearts\n",
" Three of Spades\n",
"Player's Hand = 11\n",
"Hit or stand? Type 'h' for hit, 's' for stand: h\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Jack of Spades\n",
"\n",
"Player's Hand:\n",
" Eight of Hearts\n",
" Three of Spades\n",
" Five of Hearts\n",
"Player's Hand = 16\n",
"Hit or stand? Type 'h' for hit, 's' for stand: h\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Jack of Spades\n",
"\n",
"Player's Hand:\n",
" Eight of Hearts\n",
" Three of Spades\n",
" Five of Hearts\n",
" Queen of Hearts\n",
"Player's Hand = 26\n",
"Player loses\n",
"Player has 95\n",
"Would you like to play another hand? Enter 'y' or 'n' n\n"
]
}
],
"source": [
"while True:\n",
" # Print an opening statement\n",
" print(\"Welcome to Blackjack!\")\n",
"\n",
" # Create & shuffle the deck, deal two cards to each player\n",
" deck_cards = Deck()\n",
" deck_cards.shuffle()\n",
" \n",
" player_hand = Hand()\n",
" player_hand.add_card(deck_cards.deal())\n",
" player_hand.add_card(deck_cards.deal())\n",
" \n",
" dealer_hand = Hand()\n",
" dealer_hand.add_card(deck_cards.deal())\n",
" dealer_hand.add_card(deck_cards.deal())\n",
" \n",
" # Set up the Player's chips\n",
" player_chips = Chips()\n",
" player_chips.total = 100\n",
" \n",
" # Prompt the Player for their bet\n",
" take_bet(player_chips)\n",
" \n",
" # Show cards (but keep one dealer card hidden)\n",
" show_some(player_hand,dealer_hand)\n",
" \n",
" while playing: # recall this variable from our hit_or_stand function\n",
" \n",
" # Prompt for Player to Hit or Stand\n",
" hit_or_stand(deck_cards,player_hand)\n",
" \n",
" # Show cards (but keep one dealer card hidden)\n",
" show_some(player_hand,dealer_hand)\n",
" \n",
" # If player's hand exceeds 21, run player_busts() and break out of loop\n",
" if player_hand.value > 21:\n",
" player_busts(player_hand,dealer_hand,player_chips)\n",
" break\n",
"\n",
" # If Player hasn't busted, play Dealer's hand until Dealer reaches 17\n",
" if player_hand.value <= 21:\n",
" while dealer_hand.value < 17:\n",
" hit(deck_cards,dealer_hand) \n",
" \n",
" # Show all cards\n",
" show_all(player_hand,dealer_hand)\n",
" \n",
" # Run different winning scenarios\n",
" if player_hand.value > dealer_hand.value:\n",
" player_wins(player_hand,dealer_hand,player_chips)\n",
" elif player_hand.value < dealer_hand.value:\n",
" dealer_wins(player_hand,dealer_hand,player_chips)\n",
" elif dealer_hand.value > 21:\n",
" dealer_busts(player_hand,dealer_hand,player_chips)\n",
" else:\n",
" push(player_hand,dealer_hand) \n",
" \n",
" # Inform Player of their chips total \n",
" print(\"Player has \",player_chips.total)\n",
" \n",
" # Ask to play again\n",
" new_game = input(\"Would you like to play another hand? Enter 'y' or 'n' \")\n",
" \n",
" if new_game == 'y':\n",
" playing = True\n",
" continue\n",
" else:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And that's it! Remember, these steps may differ significantly from your own solution. That's OK! Keep working on different sections of your program until you get the desired results. It takes a lot of time and patience! As always, feel free to post questions and comments to the QA Forums.\n",
"# Good job!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Blackjack milestone project!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Game Play\n",
"To play a hand of Blackjack the following steps must be followed:\n",
"1. Create a deck of 52 cards\n",
"2. Shuffle the deck\n",
"3. Ask the Player for their bet\n",
"4. Make sure that the Player's bet does not exceed their available chips\n",
"5. Deal two cards to the Dealer and two cards to the Player\n",
"6. Show only one of the Dealer's cards, the other remains hidden\n",
"7. Show both of the Player's cards\n",
"8. Ask the Player if they wish to Hit, and take another card\n",
"9. If the Player's hand doesn't Bust (go over 21), ask if they'd like to Hit again.\n",
"10. If a Player Stands, play the Dealer's hand. The dealer will always Hit until the Dealer's value meets or exceeds 17\n",
"11. Determine the winner and adjust the Player's chips accordingly\n",
"12. Ask the Player if they'd like to play again"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Playing Cards\n",
"A standard deck of playing cards has four suits (Hearts, Diamonds, Spades and Clubs) and thirteen ranks (2 through 10, then the face cards Jack, Queen, King and Ace) for a total of 52 cards per deck. Jacks, Queens and Kings all have a rank of 10. Aces have a rank of either 11 or 1 as needed to reach 21 without busting. As a starting point in your program, you may want to assign variables to store a list of suits, ranks, and then use a dictionary to map ranks to values."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
"ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
"values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,\n",
" 'Queen':10, 'King':10, 'Ace':11}\n",
"\n",
"playing = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Class Definitions\n",
"Consider making a Card class where each Card object has a suit and a rank, then a Deck class to hold all 52 Card objects, and can be shuffled, and finally a Hand class that holds those Cards that have been dealt to each player from the Deck."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class Card:\n",
" \n",
" def __init__(self, suit, rank):\n",
" self.suit = suit\n",
" self.rank = rank\n",
" self.value = values[rank]\n",
" \n",
" def __str__(self):\n",
" return self.rank + ' of ' + self.suit"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 3: Create a Deck Class**<br>\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"class Deck:\n",
" \n",
" def __init__(self):\n",
" self.deck = [] # start with an empty list\n",
" for suit in suits:\n",
" for rank in ranks:\n",
" self.deck.append(Card(suit,rank))\n",
"\n",
"# def __str__(self):\n",
"# for card in self.deck:\n",
"# return card.__str__() # add each Card object's print string\n",
"\n",
" def shuffle(self):\n",
" random.shuffle(self.deck)\n",
" \n",
" def deal(self):\n",
" return self.deck.pop() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 4: Create a Hand Class**<br>\n",
"In addition to holding Card objects dealt from the Deck, the Hand class may be used to calculate the value of those cards using the values dictionary defined above. It may also need to adjust for the value of Aces when appropriate."
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"class Hand:\n",
" def __init__(self):\n",
" self.cards = [] # start with an empty list as we did in the Deck class\n",
" self.value = 0 # start with zero value\n",
" self.aces = 0 # add an attribute to keep track of aces\n",
" \n",
" def add_card(self,card):\n",
" self.cards.append(card) #card passed in from Deck.deal()\n",
" self.value += values[card.rank]\n",
" if card.rank == 'Ace':\n",
" self.aces += 1 # not value, number of ace cards\n",
"\n",
" def adjust_for_ace(self):\n",
" if self.value > 21 and self.aces == True:\n",
" self.value -= 10 # total value of cards (-10)\n",
" self.aces -= 1 # not value, number of ace cards"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 5: Create a Chips Class**<br>\n",
"In addition to decks of cards and hands, we need to keep track of a Player's starting chips, bets, and ongoing winnings. This could be done using global variables, but in the spirit of object oriented programming, let's make a Chips class instead!"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"class Chips:\n",
" \n",
" def __init__(self):\n",
" self.total = 100 # This can be set to a default value \n",
" self.bet = 0\n",
" \n",
" def win_bet(self):\n",
" self.total += self.bet\n",
" \n",
" def lose_bet(self):\n",
" self.total -= self.bet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 6: Write a function for taking bets**<br>\n",
"Since we're asking the user for an integer value, this would be a good place to use <code>try</code>/<code>except</code>. Remember to check that a Player's bet can be covered by their available chips."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"def take_bet(chips):\n",
" try:\n",
" chips.bet = int(input(\"Amount of bet placed: \"))\n",
" except:\n",
" if chips.bet > chips.total:\n",
" print(\"Not enough chips!\")\n",
" else:\n",
" print(\"Bet placed\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 7: Write a function for taking hits**<br>\n",
"Either player can take hits until they bust. This function will be called during gameplay anytime a Player requests a hit, or a Dealer's hand is less than 17. It should take in Deck and Hand objects as arguments, and deal one card off the deck and add it to the Hand. You may want it to check for aces in the event that a player's hand exceeds 21."
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"def hit(deck,hand):\n",
" hand.add_card(deck.deal())\n",
" hand.adjust_for_ace()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 8: Write a function prompting the Player to Hit or Stand**<br>\n",
"This function should accept the deck and the player's hand as arguments, and assign playing as a global variable.<br>\n",
"If the Player Hits, employ the hit() function above. If the Player Stands, set the playing variable to False - this will control the behavior of a <code>while</code> loop later on in our code."
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"def hit_or_stand(deck,hand):\n",
" global playing # to control an upcoming while loop\n",
" \n",
" while playing:\n",
" x = input(\"Hit or stand? Type 'h' for hit, 's' for stand: \")\n",
" if x == 'h':\n",
" hit(deck, hand)\n",
" elif x == 's':\n",
" playing = False\n",
" else:\n",
" print(\"invalid input\")\n",
" continue\n",
" break\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 9: Write functions to display cards**<br>\n",
"When the game starts, and after each time Player takes a card, the dealer's first card is hidden and all of Player's cards are visible. At the end of the hand all cards are shown, and you may want to show each hand's total value. Write a function for each of these scenarios."
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"# I don't understand this.\n",
"def show_some(player,dealer):\n",
" print(\"\\nDealer's Hand:\")\n",
" print(\" <card hidden>\")\n",
" print('',dealer.cards[1]) \n",
" print(\"\\nPlayer's Hand:\", *player.cards, sep='\\n ')\n",
" print(\"Player's Hand =\",player.value)\n",
" \n",
"def show_all(player,dealer):\n",
" print(\"\\nDealer's Hand:\", *dealer.cards, sep='\\n ')\n",
" print(\"Dealer's Hand =\",dealer.value)\n",
" print(\"\\nPlayer's Hand:\", *player.cards, sep='\\n ')\n",
" print(\"Player's Hand =\",player.value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 10: Write functions to handle end of game scenarios**<br>\n",
"Remember to pass player's hand, dealer's hand and chips as needed."
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"def player_busts(player,dealer,chips):\n",
" print(\"Player loses\")\n",
" chips.lose_bet()\n",
" \n",
"def player_wins(player,dealer,chips):\n",
" print(\"Player wins\")\n",
" chips.win_bet()\n",
"\n",
"def dealer_busts(player,dealer,chips):\n",
" print(\"Dealer loses\")\n",
" chips.lose_bet()\n",
" \n",
"def dealer_wins(player,dealer,chips):\n",
" print(\"Dealer wins\")\n",
" chips.win_bet()\n",
" \n",
"def push(player,dealer):\n",
" print(\"It is a tie.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### And now on to the game!!"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to Blackjack!\n",
"Amount of bet placed: 10\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Eight of Clubs\n",
"\n",
"Player's Hand:\n",
" Seven of Clubs\n",
" Ten of Clubs\n",
"Player's Hand = 17\n",
"\n",
"Dealer's Hand:\n",
" Six of Hearts\n",
" Eight of Clubs\n",
" Two of Hearts\n",
" Seven of Hearts\n",
"Dealer's Hand = 23\n",
"\n",
"Player's Hand:\n",
" Seven of Clubs\n",
" Ten of Clubs\n",
"Player's Hand = 17\n",
"Dealer wins\n",
"Player has 110\n",
"Would you like to play another hand? Enter 'y' or 'n' y\n",
"Welcome to Blackjack!\n",
"Amount of bet placed: 5\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Jack of Spades\n",
"\n",
"Player's Hand:\n",
" Eight of Hearts\n",
" Three of Spades\n",
"Player's Hand = 11\n",
"Hit or stand? Type 'h' for hit, 's' for stand: h\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Jack of Spades\n",
"\n",
"Player's Hand:\n",
" Eight of Hearts\n",
" Three of Spades\n",
" Five of Hearts\n",
"Player's Hand = 16\n",
"Hit or stand? Type 'h' for hit, 's' for stand: h\n",
"\n",
"Dealer's Hand:\n",
" <card hidden>\n",
" Jack of Spades\n",
"\n",
"Player's Hand:\n",
" Eight of Hearts\n",
" Three of Spades\n",
" Five of Hearts\n",
" Queen of Hearts\n",
"Player's Hand = 26\n",
"Player loses\n",
"Player has 95\n",
"Would you like to play another hand? Enter 'y' or 'n' n\n"
]
}
],
"source": [
"while True:\n",
" # Print an opening statement\n",
" print(\"Welcome to Blackjack!\")\n",
"\n",
" # Create & shuffle the deck, deal two cards to each player\n",
" deck_cards = Deck()\n",
" deck_cards.shuffle()\n",
" \n",
" player_hand = Hand()\n",
" player_hand.add_card(deck_cards.deal())\n",
" player_hand.add_card(deck_cards.deal())\n",
" \n",
" dealer_hand = Hand()\n",
" dealer_hand.add_card(deck_cards.deal())\n",
" dealer_hand.add_card(deck_cards.deal())\n",
" \n",
" # Set up the Player's chips\n",
" player_chips = Chips()\n",
" player_chips.total = 100\n",
" \n",
" # Prompt the Player for their bet\n",
" take_bet(player_chips)\n",
" \n",
" # Show cards (but keep one dealer card hidden)\n",
" show_some(player_hand,dealer_hand)\n",
" \n",
" while playing: # recall this variable from our hit_or_stand function\n",
" \n",
" # Prompt for Player to Hit or Stand\n",
" hit_or_stand(deck_cards,player_hand)\n",
" \n",
" # Show cards (but keep one dealer card hidden)\n",
" show_some(player_hand,dealer_hand)\n",
" \n",
" # If player's hand exceeds 21, run player_busts() and break out of loop\n",
" if player_hand.value > 21:\n",
" player_busts(player_hand,dealer_hand,player_chips)\n",
" break\n",
"\n",
" # If Player hasn't busted, play Dealer's hand until Dealer reaches 17\n",
" if player_hand.value <= 21:\n",
" while dealer_hand.value < 17:\n",
" hit(deck_cards,dealer_hand) \n",
" \n",
" # Show all cards\n",
" show_all(player_hand,dealer_hand)\n",
" \n",
" # Run different winning scenarios\n",
" if player_hand.value > dealer_hand.value:\n",
" player_wins(player_hand,dealer_hand,player_chips)\n",
" elif player_hand.value < dealer_hand.value:\n",
" dealer_wins(player_hand,dealer_hand,player_chips)\n",
" elif dealer_hand.value > 21:\n",
" dealer_busts(player_hand,dealer_hand,player_chips)\n",
" else:\n",
" push(player_hand,dealer_hand) \n",
" \n",
" # Inform Player of their chips total \n",
" print(\"Player has \",player_chips.total)\n",
" \n",
" # Ask to play again\n",
" new_game = input(\"Would you like to play another hand? Enter 'y' or 'n' \")\n",
" \n",
" if new_game == 'y':\n",
" playing = True\n",
" continue\n",
" else:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And that's it! Remember, these steps may differ significantly from your own solution. That's OK! Keep working on different sections of your program until you get the desired results. It takes a lot of time and patience! As always, feel free to post questions and comments to the QA Forums.\n",
"# Good job!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment