Making with Code

Blacjack Lab #

In this lab, you are going to go behind the scenes of the classic card game, Blackjack.

[0] Let’s Play Uno #

๐Ÿƒ Let’s start by playing the classic card game, Blackjack. As you’re playing, consider the different components and mechanics of the game.


[0] Setup #

๐Ÿ’ป Start by going into your unit02_games folder.

cd ~/desktop/making_with_code/unit02_games

๐Ÿ’ป Clone your starter code. Be sure to change YOUR-GITHUB-USERNAME to your actual Github username.

git clone https://github.com/the-isf-academy/lab_blackjack_YOUR-GITHUB-USERNAME
๐Ÿ’ป cd into the lab
cd lab_blackjack_YOUR-GITHUB-USERNAME
๐Ÿ’ป Enter the Poetry shell and install the requirements:
poetry shell
poetry install

This is the largest software package you have encountered and includes the following files:

  • card.py
  • deck.py
  • blackjack.py
  • game.py
  • view.py
  • cards.csv

[2] Card & Deck #

Here is the UML diagram for the Card class.

  • the __str__() method decides how a class is represented when printed

classDiagram class Card { +suit: str +rank: str +__str__() str }

Here is the UML diagram for the Deck class. The Deck is mainly composed of instances of the Card.

classDiagram class Deck { +cards: [] Card +read_cards_from_file(filename: str) []Card +add_card(card) Card +shuffle_deck() +get_top_card() Card +get_num_cards() int }

๐Ÿ’ป Test the Card in card.py at the bottom of the file in the if __name == "__main__": section. Be sure you understand each attribute and method.

if __name__ == "__main__":
    c1 = Card('Hearts',4)
    print(c1)

    # ๐Ÿ’ป try making another instance of a Card

    c2 = Card('Spades','Ace')
    print(c2)

๐Ÿ’ป Test the Deck in deck.py at the bottom of the file in the if __name == "__main__": section. Be sure you understand each attribute and method.

if __name__ == "__main__":
    deck1 = Deck('cards.csv')

    # ๐Ÿ’ป test the methods

    print(deck1.get_num_cards())

[3] Blackjack #

Here is the UML diagram for the Blackjack class.

classDiagram class Blackjack { +deck: Deck +hands: dict +current_player_index: int +deal_n_cards(n: int) [] Card +deal_initial_cards() +hit(player_index) +get_hand_value(player_index) int +check_bust(player_index) bool +get_player_type() str +current_player_hand() [] Card +increment_player_num() +computer_turn() +get_winner() str +check_end() bool }

๐Ÿ’ป Test the Blackjack in blackjack.py at the bottom of the file in the if __name == "__main__": section. Be sure you understand each attribute and method.

  • Be sure to read the comments to understand the purpose of each method
if __name__ == "__main__":
    blackjack = Blackjack()

    # ๐Ÿ’ป experiment with the attributes and methods
    # ensure you understand how the game works

    blackjack.deal_initial_cards()
    for card in blackjack.current_player_hand():
        print(card)

[4] Game Logic #

๐Ÿ’ป It’s up to you to write the game logic in game.py by implementing your flow chart. You will use the Blackjack and TerminalView classes.

Functions should include to:

  • hit
  • stand
  • bust
  • view hand
  • view winner

If you’re interested, you can find the rules of Blackjack here

๐Ÿ’ป Test your game as often as possible.

python game.py

[3] Computer Strategy #

As you’ve experienced, you are playing against a computer. At the moment, the computer always hits once as their turn. However, the current computer is not very intelligent.

Let’s make a slightly more competitive computer by considering the best strategy for blackjack.

๐Ÿ’ป Write a slightly more competitive computer by changing the computer_turn() method in the Blackjack class.

๐Ÿ’ป Test your strategy by playing the game!


[5] Deliverables #

โšกโœจ

Once you’ve successfully completed the game be sure to fill out this Google form.

๐Ÿ’ป Push your work to Github:

  • git status
  • git add -A
  • git status
  • git commit -m “describe what you did”

    be sure to customize this message, do not copy and paste this line

  • git push


[6] Extensions #

Ideas include:

  • create a Player class
    • player name
    • hand
    • top card
  • betting mechanic
  • ability to always view other player’s top card