Codecademy CS101 Final Project: Blackjack

Introduction

I have been taking the Codecademy CS101 course in the past month or so. Prior to this, I had no background in Python.

Codecademy requires us to make a basic terminal program using Python in order to complete its CS101 course. The objectives of the project are as follows:

* Build a terminal program using Python
* Add at least one interactive feature using input()
* Use Git version control
* Use the command line and file navigation
* Write a technical blog post on the project

Blackjack was the first suggestion listed by Codecademy in the project write-up. I was leaning towards an original idea, but my mind started wondering what kind of logic I would write for a Blackjack program. I ended up writing the code in about a day.

The code can be found here at Github.

Design

I made a Card class that takes in the following parameters for its constructor: self, id, name, suit, value, visibility = True. Using a for loop, I generated a deck of 13 cards for each of the four suits (Diamond, Club, Spade, and Hearts).

The deck of cards is randomly shuffled using random.sample().

When the game starts up, the user is prompted to play by typing "Hit me" into the terminal. The user's input prompts
a deal_card function that pops the first card of the deck into the user's hands.

The user has two options from then on: to continue receiving cards ("Hit me") or to stand ("stand").

Adding the Dealer

After writing the basic logic of the game for a single user, I added a Dealer, which is played by the computer. The Dealer will be dealt a single card after the player's initial choice to be hit. The next card the Dealer receives is the Hole--it is hidden from the Player. Only at the end of the game does the Dealer reveal the Hole.

The logic behind the Dealer's decision to deal to itself or to stand is very simple--no card counting involved! The Dealer simply calculates if its hand is equal to or more than 18--if so, the Dealer stands.

The Rules of the Game

I personally have never played "official" Blackjack played in casinos. For the purposes of this game, I have stuck to the following very simple rules:

  • The user/Dealer is dealt a card, and can choose to continue playing and having a card dealt to them, or stand, which holds on to the current hand.
  • The value of the card follows the number on the card (ie 8 of hearts has a value of 9=8, and 5 of spades have a value of 5). Face cards (Jack, Queen, King) all have a value of 10. Ace has a value of either 1 or 11.
  • If the value of the user's or Dealer's hand sums up to 21, the user or Dealer wins.
  • If both players stand, the player with the bigger hand value wins.

Future improvements

There are many different outcomes that currently are somewhat clumsily rendered in a dozen or so if statements. This should be refactored.

It would be great to have a more intelligent AI for the Dealer. This is however out-of-scope in my study syllabus for now and the near-future.

26