BlackJack Project
I decided to do something different for this post. As much fun as it is to research and study programming concepts, sometimes you just feel like coding something up that does something! In my case, I was inspired by a card dealing method that appeared in one of my previous blog posts and I decided to make the simple game of BlackJack.
For those unfamiliar, BlackJack is the casino game of 21, where you play against the dealer to try and get as close as you can to the card value 21 as possible without going over. Originally, you start out with two cards, and you may draw a card or stay with your current cards. The dealer must play with one card up and one card down to begin with. I decided to go with hard 17, where the dealer must draw until he has 17 or more card value. To win money, you must beat the dealer, without busting(going over 21). Face cards are worth 10, while Aces can be 1 or 11. I purposely left out splitting and the gambling aspect, which I will return to in the future.
Enough about how the game works, its time to get down to some coding. First thing that I had to do before I started coding was to visualize the set-up of your stereotypical casino. Some objects I came up with included the Dealer, the Players, the Playing Cards. Then I had to think of what brought all these objects together, which I concluded would be the GameTable which everyone sits down on. So I made the GameTable my main class.
Next I would make my objects, Dealer, Players, and Playing Cards. Playing cards have a name and a value associated with it. Players will be able to hold cards. Dealer should be able to deal cards.
I decided that it would be best to create subclasses for player that deal with the different types of card games that are associated with cards(Texas Hold'em, Five Card Draw, etc.). With that I made the subclass "BlackJackPlayer" which had the method to return the current value of their hand. I later realized that Dealer was also included in this same umbrella. In terms of what he must do, he must have a hand, he must be able to calculate his own value of his cards, but has an additional task of dealing cards to players. So I made "Dealer" a subclass of "BlackJackPlayer". At this point, I'm not sure if that was the right decision to make them subclasses, rather than something being an interface, but it is fine for the current state of the program.
One design decision I decided to make was that the Game Table would control the deck, but the dealer would be the entity to actually deal the cards to the players. Although have the cards move directly from the deck to the player would be created a simpler method, this decision was in order to properly replicate the real events of how the game is actually played.
The implementation of the code was pretty easy for this project. The few bugs I ran into during this project included:
- Forgetting break statements for my switch statement - which led to all of the card types to be spades
- A bug where the dealer would draw the entire deck until it was empty - throwing an outofbounds exception
Some of the things I thought were fun:
- Visualizing and implementing the program to model real life interaction.
- Getting to write code that actually does something cool, in this case a game.
- Using new stuff I learned from the past week, including built in Collection shuffling and "? :" if/else syntax
Overall I'm pretty happy about finishing basis functionality for the BlackJack game, 1 round, no money, no splitting. I plan on revisiting the code later on this summer after I have went over a few more topics and see the difference in how I am able to improve the structure of the code itself, along with the added features.
As usual you can download my implementation here.
Comments
Post a Comment