Final Project: Blackjack in Python | Codecademy CS101 course.

Whilst undergoing my computer science path in Codecademy, I was tasked to complete a final project for the first half of the CS101 course. It could be any text-based game of my choosing, and so I underwent the challenge of building a blackjack game… “how difficult could it be.” I said to myself. Turns out I was a fool to think such things, but that wouldn’t be a first for me. 😅

My first challenge was to first write up pseudocode to anyone who doesn’t know what that is, to put it simply it’s basically deconstructing into words what an idea is, so you have an overall idea of what comes first and the rest of the path you need to take.

Pseudocode:

The rules of a simple blackjack game are as follows:

  • The goal of blackjack is to beat the dealer’s hand without going over 21.
  • Face cards are worth 10. Aces are worth 1 or 11, whichever makes a better hand.
  • Each player starts with two cards, one of the dealer’s cards is hidden until the second round.
  • To ‘Hit’ is to ask for another card. To ‘Stand’ is to hold your total and end your turn.
  • If you go over 21 you bust, and the dealer wins regardless of the dealer’s hand.
  • If you are dealt 21 from the start (Ace & 10), you got a blackjack.
  • The dealer will hit until his/her cards total 17 or higher.
  • Players can hit till their cards reach 20.
  • If the cards land on the same value number and no other hits are taken, then it’s a tie, also known as a push.
  • In a poker game you can double down, but I won’t be including it for now.
  • The player may also place chips to bet and win more or lose a bet on the game result.

Hmmm, already this is sounding a bit more difficult than I expected! But the hell with it, let’s get down to business.

Building the foundations:

For simplification, I’m just going to discuss the base mechanics of how I began building the game and briefly mention the added extra features later. If I discuss everything this blog would be way too long and probably bore you tears.

So to begin, I first built the arrays that would contain the suits, ranks, and values:

suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {
'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8,'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11,
}

Then I began building the classes, the first would handle the suit and rank of each card inside of a deck.

class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return self.rank + ' of ' + self.suit

The second class would be placing the cards we’ve made into the said deck and creating a shuffle function within the class.

class Deck:
def __init__(self):
self.deck = []
for suit in suits:
for rank in ranks:
self.deck.append(Card(suit, rank))
def __str__(self):
deck_comp = ''
for card in self.deck:
deck_comp += '\n '+card.__str__()
return 'The deck has:' + deck_comp
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
single_card = self.deck.pop()
return single_card

Next, we needed a class that could build a hand for the player and the dealer, whilst also keeping track of the value of the cards and the aces within the hand. If an ace does appear it would then need to adjust the value from 1 & 11 depending on the current overall value of the hand. If 11 would cause the hand to bust it would then shift to 1.

class Hand:
def __init__(self):
self.cards = []
self.value = 0
self.aces = 0
def add_card(self, card):
self.cards.append(card)
self.value += values[card.rank]
if card.rank == 'Ace’:
self.aces += 1
def adjust_for_ace(self):
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1

Now for the bets, another class would be made to hold the value of the total chips available and the amount of how much the player wishes to bet, whilst also handling the 1.5x winnings amount and the losing bet amount for each game result.

class Chips:
def __init__(self):
self.total = 100
self.bet = 0
def win_bet(self):
self.total += round(self.bet * 1.5)
def lose_bet(self):
self.total -= self.bet

Planning:

Once I had the base mechanics built, I could then devise how the game could be organized.

  1. A greeting to the player, depending on the current time of day. Asking them for their name and including it throughout the game.
  2. Building a function that would ask how many chips they would like to bet, it all began pretty simple a first and became a lot more complex once I started adding an additional feature into the game.
  3. Building almost a dozen separate functions for each of the following issues:
  • Hit the player or dealer with a card
  • A prompt to stand or hit
  • A prompt to restart the game when the player won or lost
  • Two functions that could show all the cards and the other hiding one of the dealers' cards.
  • Two functions to prompt when the player or dealer has busted.
  • Two functions to prompt the winner.
  • And another to tie if the values result as the same amount.

Finishing Up:

  1. From this point on, all that was left was to build the main body of how the game will begin and end, built within a while loop that would allow the player to restart when prompted.
  2. After the framework of the game was built, I then began to debug, which if you didn’t know requires you to run the code and play the game in every different scenario possible to make sure everything is working correctly and then fixing it if it didn’t.
  3. And lastly, after the base game was working, I began to add the additional features I mention in the beginning, the list is as follows:
  • On losing all of the player's chips, you will now be prompted to ask whether or not you would like to take out a small loan. Basically, giving the player a hidden extra life.
  • The debt would then be tracked and paid off gradually from the player's winnings.

To be honest, these features were pretty pointless on reflection because you can basically just restart the whole game and start with the 1000 chips 😂, but I wanted to see if it could be done, also it was pretty fun. Plus, it made for great practice when it comes to more challenging projects in the future.

And so, to conclude, this was a lot more challenging than I originally expected but working on something from scratch, felt extremely rewarding. I’m like “get a load of this game I’ve built!” to everyone I meet now. It may be only text-based, but it was, sure enough, a challenge that I overcame, and I’m very proud of that fact.

It gave me a whole new outlook on the struggles game developers face, but on a one thousand times difficulty setting, kind of made me think a bit more differently on which path I’d like to take. And most importantly, it gave me the confidence to go forth and thrive in becoming a software developer, as long as don’t give up and my brain continues to work, I should be golden. Although there were times through this whole process I felt like my brain had literally melted! So, the possibility of losing brain function is very real, so I best be taking my B12 vitamins from now on.

Thank you for taking the time to read my first blog. Please let us know your thoughts and feedback on my writing or code.

To any experienced coders out there reading this, I’d like a review of my code if possible on my GitHub repo. I’ve also set up a discussion section if you wish to ask any questions or point out any alterations in my code. My GitHub: here

PS. If you would like to play the game for free, follow my walkthrough guide: Blackjack Download & Quick Python Install Tutorial

Blackjack Competition Details

The first person who gets to £100,000 and gives me feedback on either my GitHub or Blog wins £50 Bitcoin! see full details below…

₿₿₿ Bitcoin to be WON!! ₿₿₿

Over the weekend I shall be releasing a blog post of the text-based Blackjack game I’ve built. It took me well over 40hrs to code, definitely the most challenging task I’ve ever undergone.

I will be including a link that will allow you all to download the files and keep a copy to play for free!

To anyone who knows how to run Python files, they should have no problem playing it, but for those that do not, I will include a quick guide on how to download python for free to your computer, followed by how to run a python file. Takes about 5 minutes at most.

If you do me the solid of playing this game, I challenge you to find any bugs and to reach over £100,000 (not real money) on the game! if you screenshot the amount and sent it to me, followed by a review in the discussion section of the games GitHub page. I will reward the first person to do so with £50 worth of Bitcoin as a thank you!

If you want any additional features to make the game more fun, let me know and I’ll try to customize it to your needs if I think it would make a good addition to the game.

I’m hoping to have the blog page done by Sunday, so keep an eye open.

If it’s any later I’ll let you know.

The winner must have a BTC wallet address to transfer the coin. They’re free to download usually.

The winner will be announced shortly after the challenge is complete.

Programmer & problem solver: Career interests in data science and full-stack development, personal interests in spare time on game development and AI learning.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store