HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

How is my BlackJack game design?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
designgameblackjackhow

Problem

This is a command line BlackJack game created in Java as my final project for an advanced programming class.

  • What do you think about it?



  • Have I used OOP correctly?



  • What grade should I get for this? :D



  • Any concept in game that could be improved?



I used JRE 1.7. And to make Unicode characters work you must use Unicode in Eclipse.

This is my Class Diagram:

BlackJack.java

```
import java.io.*;

public class BlackJack
{
private static int BLACKJACK = 21;
private static int DECKSIZE = 52;
private static boolean isPlayerDone;

public static void main(String[] args) throws IOException
{
Deck deck = null;
Hand playersHand = null;
Hand splitHand = null;
Hand dealersHand = null;

System.out.println("--------------------------------------------------------");
System.out.println("- BLACK JACK -");
System.out.println("--------------------------------------------------------\n");
boolean runGame = true;
while(runGame)
switch(options())
{
case "deal":
dealersHand = new Hand("Dealer");
playersHand = new Hand("Player");
splitHand = null;

isPlayerDone = false;

deck = initialDraw(deck, playersHand, splitHand, dealersHand);

if (playersHand.getHandTotal() == BLACKJACK)
{
System.out.print("Player has BLACKJACK!\n\n");
isPlayerDone = true;
System.out.print("Dealer uncovers card...\n\n");
showHands(playersHand, splitHand, dealersHand);
System.out.print("Dealer's move...\n\n");
deck = dealerDraw(deck, playersHand, splitHand, dealersHand);
showHands(playersHand, splitHand, dealersHand);

Solution

A few notes:

-
I don't think you're utilizing OOP to its full potential in your BlackJack class; all its methods are static and you're passing around too many variables. A cleaner alternative would be to make deck, playersHand, splitHand, and dealersHand class-level variables, change the methods to be non-static, and then you won't have to pass them all around. So something like this:

public class BlackJack {

  ...

  private Deck deck;
  private Hand splitHand;
  private Hand playersHand;
  private Hand dealersHand;
  private boolean isPlayerDone;

  public static void main(String[] args) throws IOException {
    BlackJack blackjack = new BlackJack();
    blackjack.start();
  }

  public void start() {
    while(runGame) {
      switch(options()) {
        case "deal":
          deal();
          break;
        case "hit":
          hit();
          break;
        case "stand":
          stand();
          break;
        ...
      }
    }
  }

  public void deal() {
    playersHand = new Hand("Player");
    dealersHand = new Hand("Dealer");
    splitHand = null;
    isPlayerDone = false;

    ...
  }

  ...

}


-
The Hand class doesn't really need a name because there are only 2 types of hands: dealer and player. So you can just pass in a boolean for drawFromDeck():

private Deck drawFromDeck(boolean drawForPlayer) {
  Hand hand = drawForPlayer ? playersHand
                            : dealersHand;

  ...
}


-
You have several different places where you're checking for blackjack, and I can't easily follow the logic. compareHands() checks for blackjack, but there are a couple other places with some checks too. These might be necessary (I don't know the rules of Blackjack that well), but you should try to minimize duplicate logic as much as possible. For example, this block of code is in both main() and hit():

if (player.getHandTotal() == BLACKJACK)
{
    System.out.print("Player has BLACKJACK!\n\n");
    isPlayerDone = true;
    System.out.print("Dealer uncovers card...\n\n");
    showHands(player, split, dealer);
    System.out.print("Dealer's move...\n\n");
    deck = dealerDraw(deck, player, split, dealer);
    showHands(player, split, dealer);
    compareHands(player, split, dealer);
} // end if()


I think fixing those (mostly points 1 & 3) would go a long way to making the code easier to read and maintain. I skimmed over your other classes and they seemed fine at a glance, having good separation of concerns.

Code Snippets

public class BlackJack {

  ...

  private Deck deck;
  private Hand splitHand;
  private Hand playersHand;
  private Hand dealersHand;
  private boolean isPlayerDone;

  public static void main(String[] args) throws IOException {
    BlackJack blackjack = new BlackJack();
    blackjack.start();
  }

  public void start() {
    while(runGame) {
      switch(options()) {
        case "deal":
          deal();
          break;
        case "hit":
          hit();
          break;
        case "stand":
          stand();
          break;
        ...
      }
    }
  }

  public void deal() {
    playersHand = new Hand("Player");
    dealersHand = new Hand("Dealer");
    splitHand = null;
    isPlayerDone = false;

    ...
  }

  ...

}
private Deck drawFromDeck(boolean drawForPlayer) {
  Hand hand = drawForPlayer ? playersHand
                            : dealersHand;

  ...
}
if (player.getHandTotal() == BLACKJACK)
{
    System.out.print("Player has BLACKJACK!\n\n");
    isPlayerDone = true;
    System.out.print("Dealer uncovers card...\n\n");
    showHands(player, split, dealer);
    System.out.print("Dealer's move...\n\n");
    deck = dealerDraw(deck, player, split, dealer);
    showHands(player, split, dealer);
    compareHands(player, split, dealer);
} // end if()

Context

StackExchange Code Review Q#19760, answer score: 8

Revisions (0)

No revisions yet.