Tackling a Problem From a Beginner's Perspective

rduggan

Robert Duggan

Posted on November 22, 2021

Tackling a Problem From a Beginner's Perspective

What do I do?

We often see technical blog posts from experienced developers sharing their insights from years of coding, hardly ever from someone just starting out. The hardest part of beginning a project is actually imagining a project to start, at least for me. So, I sought inspiration from the internet. (codecademy's final project)

Codecademy's Final Project in python was to create some terminal program. This was my first project completely from scratch. Of the options at codecademy I decided on the blackjack game. Making a blackjack game seemed fairly challenging but within my grasp. So I got to work. BTW, you can find the source code here

What do I do, now?

I began by establishing what I would actually need to play blackjack in real life. That being a deck of cards and some people to play with. I didn't have people to play with so I had to settle for some computer opponents of which I have not implemented any kind of AI for yet.

To create the players I took an input from the user about how many people were playing. I then created an empty dictionary of players and filled it with players and an empty hand. ({player 1: [], player 2: []})

I read that blackjack at casinos uses upwards of 6 decks! This is the bulk of the problem that I made for myself. If I had just used 1 deck I could have made it work, but I'm stubborn and wanted to make it as much like the real thing as I could.

This is where the fun begins!

I started this off by creating an empty list and then filling it with numbers from 1 to 52 * 6 + 1. There may be other ways to do this, but I wanted to pop cards out so I didn't use the same card twice. However, I knew that I wouldn't be able to do anything with any numbers above 52, as that's all that's in a deck. So I created a function to reduce the cards into a usable format to use for later.

Now that I have my decks and my fake people, I could start working out how to get fake cards into fake people's hands. I did this by using the function I created that reduces the cards input values to 1 - 52 on a random number generated from the length of the cards list - 1, and then pulling it out and appending it to the players dictionary.

After I got cards into people's hands, I began to work on figuring out just what those cards were (as of right now they are just some number between 1 and 52). My strategy here was to first define the suits, then figure out the faces. It actually took me quite a while to land on a method I liked (the simplest). I just assigned 1 - 13 as diamonds, 14-26 clubs, and so on. As for the face cards, I created a function to reduce the cards given to it to land between 1 and 13, that way I didn't have to have 16 if statements. Although, in retrospect the time I spent making that work was probably much longer than it would have taken to just manually line them out.

In blackjack, the cards numbered 1 - 10 are worth their respective value, however each face card is worth 10 and the ace is worth 11. All I have right now in the players' hands are 2 numbers between 1 and 52. This isn't helpful. Luckily, I made a function before to reduce my if statements on finding the card faces. I utilized it here to loop through each hand and condense it to a value between 1 and 11 to get added up later to decide the winner.

Obviously, we would want the user to know what cards are in their hand, otherwise how can they make smart decisions? I created a little function to list the cards from the dictionary, combined with the other functions I made to reduce the values and find the faces, to output to the user what was in their hand. I then gave him the option to hit or to stand.

If the user selected hit, I pulled another random card from my card list and did my magic to it and put it in the users hand. If the sum of the cards in their hand was greater than 21, I let him know that he lost. Then I rubbed it in his face by telling him who won. By this point, I'd gotten lazy. The user only gets 2 chances to hit, and the computer players aren't making any decisions. I summed everyone up and compared it to the highest known so far and output that to the console.

Conclusion

Thanks for coming to my TED talk about spending 4 days on a blackjack game that takes 4 seconds to play.

💖 💪 🙅 🚩
rduggan
Robert Duggan

Posted on November 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related