I am working on building a blackjack game for my first programming class. I have zero programming experience other than the bit I've had in this class. Our major project is to build and run a blackjack program using java.
I have built my deck, shuffled it and dealt a card. Where I am at a stand still is assigning a value to the card that was pulled. I am not sure how to implement this. Can anyone provide any direction for me? Any help would be greatly appreciated!
Below are my Card and Deck class:
public class Card
{
private int suit;
private int rank;
private int value;
private String [] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};
private String [] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
public Card (int cardSuit, int cardRank)
{
suit = cardSuit;
rank = cardRank;
}
public int getSuit()
{
return suit;
}
public int getRank()
{
return rank;
}
public String toString()
{
return ranks[rank] + " of " + suits[suit];
}
}
import java.util.ArrayList;
import java.util.*;
public class Deck
{
private ArrayList<Card> deck;
private int suitCounter;
private int rankCounter;
private int randomIndex;
private Card card;
private ArrayList<Card> temp;
private Card removeCard;
public Deck() //building deck with 52 cards
{
deck = new ArrayList<Card>();
for(suitCounter = 0; suitCounter < 4; suitCounter++)
{
for(rankCounter = 0; rankCounter < 13; rankCounter++)
{
deck.add(new Card(suitCounter, rankCounter));
}
}
}
public void shuffle() //shuffling the deck
{
Collections.shuffle(deck);
}
public Card deal() //picking the first card out of the deck
{
card = deck.get(0);
return card;
}
public void remove() //removing the dealt card from the deck and putting it in a new temporary deck
{
temp = new ArrayList<Card>();
removeCard = deck.remove(0);
temp.add(removeCard);
}
public String toString() //display the choosen card
{
return "Card: " + card;
}
}
I do not have an official blackjackgame class with a main method as of yet. My hope was to get the deck built, deal a card and get the value and then I'd start building the main method. But here is what I have to help to know if I printed out the card with a value or not.
public class Game
{
public static void main(String[] args)
{
Deck playingDeck = new Deck(); //creating deck object
playingDeck.shuffle(); //shuffling deck
playingDeck.deal(); //dealing card 1 of player's hand
playingDeck.remove(); //removing card 1 from deck
System.out.println(playingDeck); //printing out card 1
playingDeck.deal(); //dealing card 2 of player's hand
playingDeck.remove(); //removing card 2 from deck
System.out.println(playingDeck); //printing out card 2
}
Thank you in advance!
Some issues
Although you are generally on the right track, your implementation has some design decisions that you should reconsider. This is part of the reason why you find it hard to assign a value to the card that is drawn. In your design a card has a
valueattribute but if you think about it, a card's value in BlackJack is determined by its rank and its rank only. Additionally, it will not be changing over time, that is why you should keep the value closely coupled to the rank. Another thing to consider for both, ranks and suits, is there is only a limited set of allowed and well-defined values which is far smaller than the range of values anintorStringcould take so I would propose you use an enum to type those values. Always keep in mind enums are just special classes so they can have attributes as well.Here how I would implement
RankandSuit.Rank.javaSuit.javaThis has some nice benefits for the
Cardclass. A card only needs a Suit and a Rank and that's it. With it's rank it will automatically have a value assigned as well now. The problem with your implementation of the card is that you had an array of all possible values for ranks and suits in every card. Again, think of the real world. A card certainly does not hold all values of possible ranks and suits, but only has one rank and one suit. Your classCardis a somewhat simplified model of the real-world card so it should have the properties of a real-world card. Designing it this way will also help you staying on top of your program because your design will be easier to comprehend.Here my implementation of the card.
Card.javaNow, let's move on to the
Deckclass. This war actually quite well designed by you as a deck is just a collection of cards. But there is a issue with your methoddeal()which hinders you from knowing which card was drawn. In your implementation you just remove the card on the top of the deck but you don't return it, so you're essentially just wiping a card from memory and it is lost and cannot be played in the game. Theremove()method of anArrayListdoes return the removed value by default so use that. Also, that's maybe splitting hairs, but I think you should renamedeal()todrawTop()as dealing in the real-world involves first drawing a card then giving it to a player, who then accepts that card and puts it in his hand.This is my
Deckimplementation. There are some changes required due to the previous changes I've made but this is essentially your Deck with the change described above.Deck.javaYou are now able to call
var topCard = new Deck().drawTop()and it will remove a card from the top of the deck and this card will be stored intopCard. You can just simply callSystem.out.println(topCard)or usetopCard.getRank()andtopCard.getSuit()to see which card was drawn. Remember with the rank you also will get the value of the card.See this
mainmethod on how you can now interact with all those classes.Expected output:
The next steps
Now, this is it as far as your first question goes. But as you are a beginner and you wanted some pointers I've decided to give you some pointers on what to do next and how you could do it :) I know it's quite a lot to take in, but I am sure you will get your head around it. In the next few sections, I will show you can you can actually draw cards from the deck and deal them to players.
A few sentences ago I mentioned
The nouns in this sentence should be your next classes and the verbs should be your next methods. So that would be:
HandPlayerHandwhich is again a collection ofCardsCroupier(for the dealing cards part in the sentence)Deckof cards which he/ she can deal to playersDeckand deal it to a playerHand
A hand is a
set of cards(I am using the Java data structure HashSet here) and there is atotal valueof all cards, so this should be the attributes. Additionally aHandshould allow to add and remove cards to it, this will be our methods. When adding and removing we need to adjust thetotal valueof the hand accordingly.Hand.javaPlayer
Let's move on to the player. A player will have a
nameand aHand. Additionally he/ she (a gender is also be possible if you want to assign one btw) should have the ability to accept a card and put it in their hand when a card is dealt to them. A player will also have other abilities like making a decision whether to take on another card or not and other stuff, but that is up to you to extend on that, I will just focus on how accepting/ receiving a card.Player.javaCroupier
Now it gets interesting as a croupier is a player with all the abilities and properties of a player but he/ she also has more abilities and properties. To reflect that in Java we use a feature called inheritance. So a
Croupierhas everything aPlayerhas and more so itextendsthePlayer. Inheritance will allow us to simply define that and we don't have to re-implement everything we have done for thePlayeragain for theCroupier. So we only need to focus on the additional properties and abilities of aCroupieri. e. he/ she gets a deck of cards which he/ she can shuffle and deal to players. To shuffle we just use theshuffle()method of theDeck. To deal a card we use thedrawTop()method of theDeckto draw a card from the deck (and get that card returned) and thereceiveCard()method of thePlayerwhich we pass that drawn card to. This way the top card from the deck will be removed and added to the hand of the player. All this happens in thedealCardTo()method.Croupier.javaPutting it all together
Now we have all necessary features to deal cards to players so here an quick example of how we would do that.
Application.javaExpected output:
What to do next?
Now that should give you a good skeleton to implement the rest of the game. There is still a lot to do. The next thing should probably be to create a
Gameclass which represents one game of BlackJack. Again start from the real-world and keep in mind that your design should be a model of that. Always think in terms of abilities (= methods) and properties (= attributes) an instance has. AGamewill certainly have a collection of players, a croupier and the ability to start the game. Think about what should happen on the start of the game: The croupier will have to deal cards (we've seen how to do that already) but it won't be just one and there will be decisions being made by the players involved whether they would like to take on another card. And you will need to implement the rules of BlackJack as well as win conditions to determine a winner. You could also implement some stakes for the players and so on and so on.I hope this has helped you understand what and how you can start and what to do next :) Happy coding :)