Create variables used by the Game class These can be private ints, as we will only want to use them within the scope of this class. So declare the ints wins, losses, and pushes to store this information. The only variables we're going to create to start off with are ints to store the score. So create a new class within the same package and name it Game. ("Welcome to Blackjack") Īll the main method is going to do is create a new Game object for us to start the game. I've started with a console template and have added a line that prints out the message "Welcome to Blackjack." Main.java package com.kevinsguides You may start with a command line template, or create the main method yourself. You may call it whatever you would like, but I'm calling mine Blackjack. We can always add code, test it, and remove or modify methods as needed during the development process.īegin by creating a new Java project in your favorite IDE. You may not know exactly how you're going to implement each class and method at the start, and that's okay. Game class - to handle the bulk of the game logic, starting new rounds, keeping track of score, etc.Īs you practice more, you'll get better at determining the structure of your program beforehand.Hand class - to represent the cards the player and the dealer are holding, and get the total value of the cards in their hand.Deck class - to hold multiple cards and perform operations such as shuffling the deck.Suit enum - to hold the suits such as Club, Diamond, Heart, and Spade.Rank enum - to hold the ranks, such as 1 through 10, Jack, King, Queen, and Ace, along with their numerical values (1 through 11).Card class - to represent a card and handle card operations.They both need a set of cards, a name, a way to print their hands to the console, etc. Person class - to act as a parent class of Dealer and Player.Dealer class - to handle dealer specific operations, such as drawing cards to 16 and standing on 17 or above.Player class - to handle player specific operations, such as deciding to hit or stand.The dealer gives you a card from the deck. In this example round, it looks like I have won. This is what the program looks like when you run it. This project touches on: Object Oriented Design, ArrayLists, Collections, Algorithms, and ENUMs. If we run out of cards, we shuffle the deck and start over. If neither player nor dealer busts or gets BlackJack, the player with the highest score wins the round.If they started with a value of 10, and then drew a 3, they would keep drawing more cards until they get to 17 or higher. The total value of their hand is now 20, so they stand. Example: The dealer draws a 3 and a 7 to start, for a value of 10, then they draw a King.The dealer will keep drawing cards until they reach a hand valued at 17 or higher.When the player stands, it's the end of their turn, and the dealer begins drawing their third, fourth, fifth card, and so on. The player may stand (stop drawing) at any time.If they both get 21 in the end, it's a push (a draw or tie). If the dealer starts with 21, the player loses automatically, before they even get a chance to hit or stand. If the Player starts with 21, they automatically get BlackJack and win.So, for example, a King and an ace would be 21, but one King, a 9, and an Ace would be valued at 20 (10+9+1=20). If the value of the hand with an ace puts it over 21, the ace is valued at 1.Number cards are valued at their number, Face cards (Jack Queen and King) are valued at 10, and aces are valued at 1 or 11 depending on the situation.Players start with two cards and have an option to hit (draw another card) or stand (end their turn).It will be played with a single, standard Deck of 52 cards, though you could easily change it to accommodate larger deck sizes.Note that there are many variations of blackjack. This will be a very basic version with only hit and stand functionality. I'd encourage you to try building the console version, then check out the GUI version (bottom of page). I have a new version of this same project, but with a GUI. Starting from the ground up with creating the cards, then the deck, then the game logic, and so on. We will be jumping between different files and adding methods as we need them. Rather than going through code individually class by class, I'll be explaining the processes behind how I would logically develop certain parts of the program. You will practice fundamental Java concepts including object oriented design, inheritance, basic algorithm design, ArrayLists, and more. In this exercise, you will create a console version of Blackjack using Java.
0 Comments
Leave a Reply. |