Blackjack Terminal Game

I decided to go with the Blackjack game. It seemed challenging but doable. My source code is here

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.

5 Likes

Very cool and fun to play!

great job!!! for me and others beginners in code it’s a good way to learn and have fun!