Back to Parent


The game of blackjack has a variety of different inputs that happen at specific stages of the game, so I had to design an internal structure for the game's code that could parse appropriate inputs at appropriate times while rejecting inputs that might be appropriate at other times, just not at that point (ie trying to bet when you are in the process of hitting or staying).

In order to do this, I implemented a state machine using a large case statement. 

When: Start                      #welcomes the user to the game, giving the options: "play" or "rules"

When: play_or_rules     #explains the rules or starts the game

When: hand_init             #betting happens here, and the hand is generated

When: hit_or_stay          #the user decides progressively whether to hit or to stay

After the player busts or decides to stay, the outcome is determined and relayed to the player. If the player still has money, they can bet again (or they win if they reach $500). If the player runs out of money, they lose, and can play again. 

I used the DeckOfCards API in order to generate the hands for the player and for the house. I programmed all the betting and predicting and calculations by hand. 

One counterintuitive aspect of the code is how it calculates the next hit. In order for the house to taunt the player and tell it whether or not the next card will bust, it has to know what the next card is. Therefore, on the first deal, the API gets pulled for not just the first two cards in the player's hand, but also the next card after that. That next card is held in code limbo until the player decides whether to hit or stay. If hit, then the card is "officially" revealed and added to the pile. Otherwise it is not saved.

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!