Black Jack Game: Part 1 | Python

To use verbiage from the game of Black Jack, today’s project just may have been a bust. The goal for today’s project was to build a Black Jack game played against a computer. However, like playing in a casino, the odds may have been stacked against me with the hand that was dealt.

I ran into an error that I just couldn’t seem to shake and to prevent myself from feeling like too much of a joker, I decided to put a pause on this project until tomorrow. Here is an overview of the status of this project’s code so far…

First, I imported the ‘random’ module for use in a function covered below as well as imported an ASCII logo to use as the program’s header.

I then created a list called ‘cards’, which holds each possible value of a card pulled in a standard card deck. Two things to note are that the ace is represented by the value 11 and the value 10 is listed four separate times. Later in the logic, I handle the possibility of using the ace with a value of 1; however, by default, the value is set to 11 for easier handling. Additionally, the value 10 is listed four times to simulate the same odds of pulling a 10, Jack, Queen, or King in a real deck.

After creating this list, the variable ‘end_game’ is set to the Boolean value False and two new lists (‘user_cards’ and ‘computer_cards’) are declared for use later in the code.

The first and easiest function defined is called deal_card(), which assigns the variable ‘card’ with a random selection from the ‘cards’ list. The value of ‘card’ is then returned for use later.

Next, I defined the function which is the problem child of this project currently. The calculate_scores() function takes an argument of cards and when called, calculates the value of the cards dealt.

The first ‘if’ statement takes the sum of cards and checks whether the value is equal to 21 as well as if the total number of cards in the hand is equal to 2. If so, this would be considered ‘Black Jack’ and will return a value of 0.

It is this ‘if’ statement, marked with the FIXME tag, which has me at a standstill. When the statement checks whether the length of the cards argument is 2, I receive a TypeError which stops the program in it’s tracks. During debugging, I uncovered that len(cards) is being returned as 3 when 2 is expected. At the time of writing, I do not have an answer as to why this is happening. However, when stuck on a coding project, one of the best things you can do is to step away and come back with a fresh mind.

I am positive that the solution is simple, but it’s just not apparent to me currently. I will be sleeping on it and am excited for tomorrow to bring the promise of a fresh perspective.

If the card argument does not pass the Black Jack check, it is then time to handle the logic for determining whether a dealt ace should be valued at 1 or 11. This ‘if’ statement first checks whether there is an 11 in the cards list and if so, whether the sum of the cards is greater than 21. Since any total above 21 is an instant bust and indicates a loss, if the value 11 pushes the user to bust, it is removed and then replaced with the value of 1. A new sum is then calculated to update the current score.

Next, it’s time to actually assign the two randomly generated cards to both the user and the computer. This is accomplished by appending the result of the deal_card() function to the ‘user_cards’ list and then to the ‘computer_cards’ list. Since these are contained within for _ in range(2), this will repeat for a total of two appends per list (one for each card deal).

Finally, the gamification of these defined functions begins. First, the ASCII header is printed, and a ‘while’ loop is created. So long as ‘end_game’ is False, the following code will run:

Both ‘user_score’ and ‘computer_score’ variables are assigned their respective values. After this, two print statements are triggered. The first tells the user which cards are in their hand and the total value of said cards. The next print statement reveals the first card from the computer’s hand.

Next, an if statement checks whether the value of ‘user_score’ is equal to 0 or greater than 21 as well as if ‘computer_scrore’ is equal to 0. If any of these are true, ‘end_game’ is set to the Boolean value True, causing the loop to end along with the game. Otherwise, the user is prompted to choose whether they would like to draw another card. If so, the new card value is added to the ‘user_cards’ list and a new score is calculated. If not, ‘end_game’ is set to the Boolean value True, causing the loop to end along with the game.


Along with the TypeError troubleshooting needed, there is plenty of other functionality I need to add, in order for this project to be complete. While the project is not finished, you can view and download the current state of the source code for this project on my GitHub by clicking here. Thank you for your time and I will see you here next time with a finished Black Jack game!

Previous
Previous

Black Jack Game: Part 2 | Python

Next
Next

Calculator | Python