What must I do now that I have had my first attempt at a blackjack java game?

282    Asked by sahin khan in Java , Asked on Oct 14, 2022

 I  just completed my first multi class program, Blackjack, and it works! It allows the user to play Blackjack against a single dealer, with no other players at the table. I was wondering, seeing as it is my first multi class program, if you could help me optimize it and/or give me any advice. I want to implement insurance and splitting, so any advice to help prepare the code for eventually adding those features would be really helpful! Finally, my main method is pretty long — I was wondering if this is typical of Java programs and, if not, how I can fix that.

Card

package Blackjack; class Card { /* * Creates a playing card. */ private int rank;//represents the rank of a card private int suit;//represents the suit of a card private int value;//represents the value of a card private static String[] ranks = {"Joker","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; private static String[] suits = {"Clubs","Diamonds","Hearts","Spades"}; /* * Created with an integer that represents a spot in the String array ranks and the String array suits. This represents * the rank and suit of an individual card. */ Card(int suit, int values) { this.rank=values; this.suit=suit; } /* * Returns the string version of a card. */ public String toString() { return ranks[rank]+" of "+suits[suit]; } /* * Returns the rank of a card. */ public int getRank() { return rank; } /* * Returns the suit of a card. */ public int getSuit() { return suit; } /* * Returns the value of a card. If a jack, queen, or king the value is ten. Aces are 11 for now. */ public int getValue() { if(rank>10) { value=10; } else if(rank==1) { value=11; } else { value=rank; } return value; } /* * Sets the value of a card. */ public void setValue(int set) { value = set; }

Deck

package Blackjack; import java.util.ArrayList; import java.util.Random; /* * Creates and shuffles a deck of 52 playing cards. */ class Deck { private ArrayList deck;//represents a deck of cards Deck() { deck = new ArrayList(); for(int i=0; i<4 j=1; j<=13; xss=removed i=0; xss=removed xss=removed xss=removed>

Dealer

package Blackjack; import java.util.ArrayList; import java.util.Arrays; /* * Creates a dealer that the user plays against. */ class Dealer { ArrayList hand;//represents the dealer's hand private int handvalue=0;//value of the dealer's hand (starts at 0) private Card[] aHand;//used to convert the dealer's hand to an array private int AceCounter;//counts the aces in the dealer's hand Dealer(Deck deck) { hand = new ArrayList<>(); aHand = new Card[]{}; int AceCounter=0; for(int i=0; i<2 xss=removed i=0; xss=removed xss=removed>0 && handvalue>21) { handvalue-=10; AceCounter--; } } } /* * Prints the dealer's first card (the card face up at the beginning of a blackjack game). */ public void showFirstCard() { Card[] firstCard = new Card[]{}; firstCard = hand.toArray(firstCard); System.out.println("["+firstCard[0]+"]"); } /* * Gives the dealer another card and updates the value of his hand. Takes into account the value of aces. */ public void Hit(Deck deck) { hand.add(deck.drawCard()); aHand = hand.toArray(aHand); hand value = 0; for(int i=0; i0 && handvalue>21) { handvalue-=10; AceCounter--; } } } /* * Determines if the dealer wants to hit according to classic Blackjack rules. */ public boolean wantsToHit() { if(handvalue<17 xss=removed xss=removed>21) { System.out.println("The dealer busted!"); return true; } return false; } /* * Takes the turn for the dealer and returns the value of his hand. */ public int takeTurn(Deck deck) { while(wantsToHit()) { System.out.println("The dealer hits"); Hit(deck); if(busted(hand value)) { break; } } if(handvalue<=21) { System.out.print("The dealer stands."); } return hand value; }

Main

package Blackjack; import java.util.*; public class Blackjack { private static int cash;//cash the user bets with private static int bet;//how much the user wants to bet private static int AceCounter;//how many aces are in the user's hand private static ArrayList hand;//represents the user's hand private static int handvalue;//the value of the user's hand private static String name;//name of the user public static void main(String[] args){ System.out.println("Hi! What is your name?"); Scanner scan = new Scanner(System.in); name = scan.nextLine(); System.out.println("Hello, "+name+", lets play some BlackJack!"); System.out.println("How much cash do you want to start with?"); Scanner money = new Scanner(System.in); cash = money.nextInt(); System.out.println("You start with cash: "+cash); while(cash>0){ Deck deck = new Deck();//initialize deck, dealer, hands, and set the bet. deck.shuffle(); AceCounter=0; Dealer dealer = new Dealer(deck); List hand = new ArrayList<>(); hand.add(deck.drawCard()); hand.add(deck.drawCard()); System.out.println("How much would you like to bet?"); bet=Bet(cash); System.out.println("Cash:"+(cash-bet)); System.out.println("Money on the table:"+bet); System.out.println("Here is your hand: "); System.out.println(hand); int hand value = calcHandValue(hand); System.out.println("The dealer is showing: ");


Answered by Saito Ito

There's quite a lot to improve on this. Here are a couple of tips to get your started in blackjack java.

Excessive comments

Do these comments add anything new that isn't clear already?

private int rank;//represents the rank of a card private int suit;//represents the suit of a card private int value;//represents the value of a card

They don't. In fact most of the other comments in the code don't add value either. The best code doesn't need comments. Look through all the comments in your code, if they are not needed, then remove them, if they are needed, then try to change the code in a way to not need comments.

Making Card immutable

Will it make sense for rank, suit and value to change in the lifetime of a Card instance? Probably not. So make these fields final. There is a setValue method, which you don't need either.

Review the other classes too. Make everything final that doesn't need to change or doesn't make sense to ever change. This practice can help you spot some design bugs.

Refer to types by interfaces

You declare several lists like this:

ArrayList hand;

Use the interface type instead:

List hand;

Consider enums

These variables are good candidates for enums:

  private static String[] ranks = {"Joker","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; private static String[] suits = {"Clubs","Diamonds","Hearts","Spades"};

Like this:

enum Suit { Clubs, Diamonds, Hearts, Spades }

If you want to iterate over the possible suits, you can do for (Suit suit : Suit.values()) { ... }

Magic numbers

There are too many magic numbers in the code. 17, 10, 11, 4, ... It would be better to put these in public static final variables with descriptive names, to clarify the purpose of these values, and have them together near the top of the code for easier control and flexibility to play with.

Formatting

The code doesn't follow the common formatting generated by the auto-format option of common IDEs like Eclipse and IntelliJ. I suggest reformatting the entire thing, to make the code look more familiar and easier to read for the majority of Java coders. In Eclipse the keyboard shortcut is Control-Shift-f



Your Answer

Interviews

Parent Categories