Project: Write a program that allows a human user to play a simplified version of Blackjack against a computer opponent. Make it as cool as you can. Programming by Doing – http://programmingbydoing.com/a/project-blackjack.html
Blackjack is a card game of probability between the player and the dealer. The objective is to beat the dealer. Get 21 points on the player’s first two cards without the dealer Blackjack and you win. If you reach a higher sum than the dealer without exceeding 21 then you win. If you exceed 21 or have a lower total than the dealer then you lose.
This project was a starting point to learn about classes and objects in an object-oriented language. It was a fitting first project, who doesn’t love a good game of Blackjack living in Las Vegas. I’m going to trace my approach, explain where to start, and the basics to make sure your code is “playing with a full deck”.
- Create a new classes: Blackjack, Card, Game, Dealer, Player
- Current Card – This is an instance variable holding the current card.
- Total – Instance variable
- Dealer Total – Static methods will hold the dealer or the player, the program doesn’t care which.
- Player Total – This static will hold the player total to compare dealer vs player at the end.
- Random – New random to generate a random card.
- Scanner – Input whether you want to hit or stay
- Constructor – Chip bet goes right into the game
- Trace Logic – Deal, Display First Card, Generates Random number 2-11, Display Second Card, Generates Random number 2-11, If Thens on Dealer and Player Turns
- Who Won – Prints outcome using If and Else Statements
- The dealer has infinite amounts of chips.
- Cards are drawn randomly from multiple decks.
- The player can continue to play only when at least 1 chips in hand.
- Splitting or doubling rules are not implemented.
Real-world game sequence:
- I created the elements that are important to the game in the code as a complete beginner.
- You sit down at the table, a new game begins. You put your chips on the table, dealer begins the game.
- 2 cards are dealt to you. I used while, if, else here to check if any Aces are dealt. These are methods from the If Statement section.
- Dealer deals 2 more cards, with one facing up and the other down.
- Player inputs signal to the dealer whether to Hit or Stand. The game follows outcome logic from there on.
- It’s commonplace to play several hands of Blackjack in one sitting, but based on your bankroll. So this is taken into account by asking the user if they want to keep playing.
What’s going on in the app:
What made sense for this project was to make a diagram to map out the Blackjack app. When making classes I can always ask , what is the data I want my class to hold and do I want command will be used to print the next step on the screen? Here is the link to my Blackjack project on gitub.
import – Used at the beginning of a source file to specify classes or entire Java packages to be referred to later without including their package names in the reference.
Player Bets – “How many chips do you want to bet?”
Check Number of Player Chips – “You only have: newGame.player.chips + Chips.”
Deal Cards to Player and Dealer – Check for Aces
Game Score – A new scanner to select if you want to hit or stay.
The instance variable holds the current hand’s bet and the instance variable holds the card and the dealer values. This part of program doesn’t care whether it’s a dealer or a player.
What defines a card? Without getting complicated, it’s the value, generated by the random number generator. This data is only available within the card and making a constructor is the method we use to create an instance of our class card when we use the new command in the game.
You can use a switch statement to return the name of the card, but I chose just return the value and not to implement names. A switch statement is pretty simple, it gives you the option to test for a range of values for your variables. They can be used instead of long, complex if statements.
We’ve got the class for Dealer here , which defines the number of chips the dealer has and creates an instance of the dealer score.
We’ve got the class for the new game here, which defines each new game hit, stand, and points for both the human Player and the Dealer.
Create a boolean command to declare a points variable that can store a boolean value; that is, either true or false.
We make one more class called Player, this represents the “human player” and the Dealer. The other information relevant to the player is the hand (cards) and the chips in the bet.
There are two instances tell the program how many chips the player has and how much should be allocated in one particular sitting of the game.
Similar to the create your Adventure1.java exercise, the card game’s logic is core to what you actually build. Example: Your dog wakes you up in the morning (A) roll over (B) take him for a walk. Selecting (A) roll over begets further logic. Your dog feels ignored then chews your favorite shoes (A) roll over (B) buy new shoes on Zappos. And so on…
This is basically what a shell of a Blackjack game looks look like and there are many different ways to build it. This is a beginner project, the next step is to make it graphical. This project is a part of a Java development boot camp at Zappos.
Thanks for reading!