Python Terminal Project: Bringing Python to the Blackjack tables
raymndh93
Posted on May 25, 2023
This was a my first project in the Code Academy python course and while the prompt was simple and open ended, brainstorm and create a terminal program that:
Uses Python
Contains interaction through the input() feature
Uses command line
Has a technical blog post to accompany it (this here)
In the brainstorming process what stuck out to me about blackjack is the repeated prompting to the user for input throughout the game. Also, because the game is all of the players vs the dealer, there is an easy opportunity to add in multiplayer.
But I'm getting a bit ahead of myself. In the first version of the program, my focus was on setting up the basic functions of the blackjack game. The player enters their name and is dealt a hand from a shuffled deck. They are then shown their hand with a calculation of their hands value and asked if they would like to hit or stay. This repeats until the player either goes over 21 or chooses to stay. The dealer then takes their turn and adds cards until they beat the player with over a 17 or they go over 21. The program then determines the winner
In this version the challenge I faced was getting the dealer to correctly determine who the winner was for each hand that was played. Because of the randomness of each hand, this just became a time consuming process of playing the game until i had checked that all of the possible outcomes were correct. After a couple rewrites of this method I found an order that allowed me to check through every outcome without any falling through the cracks. It was also in this process that I discovered the need for a reset method which returned all of the conditional variables to their original states so that win results were not affected by a previous hand.
In the final version of the code I added the ability to play with multiple people as well as added the ability to bet on each hand of blackjack. In this version's Welcome() method the user is asked for a number of players and then after each player gives their name a Player object is created with 10,000 chips. This list is then passed to the gameplay loop where the players are asked for the rounds bet and cards are dealt. Each player is then individually called to their turn and plays until they stay or go over 21. After all the players have gone, the dealer plays its hand, winners are determined, and chips are paid out at a 2 to 1 rate.
The issue I had with this version was an error where all players were dealt the same cards and adding cards to the hand would add them for everyone. This was a hard learned lesson about naming variables. The variable i was using to reference each player object in the for loop was of course “player” so dealing cards for each object was instead changing every player object in the list. After changing the reference variable and moving the "game_hand" variable to inside of the constructor for the player the program finally worked as designed.
Here is a link to the code on GitHub:
https://github.com/raymndh93/Code_Academy_python_final.git
I had a blast coding this terminal game and I look forward to learning ways to make this even bigger and better!
Posted on May 25, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.