Hello, I am rather new to this so bare with me!

I am currenlty creating a BlackJack game in Java and attempting to use the MVC design when creating it. I have so far created the following classes:
- Card: To hold create a card
- Deck: To create a deck filled with all 52 cards
- Hand: To hold a 'Hand' of Cards
- Player: To create a Player for game use
- BlackJackTest: Here I am testing all the functions of the game trying to get something working!

I have currently got to the point where I can show all 52 cards, and also deal 2 cards to a Hand and show this Hand. But I'm having a lot of trouble with the next steps. Ideally within the "BlackJackTest" class I would want to use this as a Game Engine where I would enter X amount of players and their names, and it would create the players, and loop through the system to play the game.

My problem is that I am completely stuck on what I should be doing next, or if I have done something wrong. My next task should be to implement a feature so that I can put up to 3 human players against a dealer, but am unsure of how to do this....and I should also implement hit/stand features which I'm not sure where or how I should be coding these.

Any help is appreciated!

Here is my code:

import java.util.Scanner;

public class BlackJackTest {

    public static void main(String[] args) {

        /*System.out.println(deck.getCard(0).toString());
        System.out.println(deck.getCard(0).getSuit());
        System.out.println(deck.getCard(0).getSuitCode());
        System.out.println(deck.getCard(0).getValue());
        System.out.println(deck.getCard(0).getValueCode());
        System.out.println();
        System.out.println(deck.cardsLeft());
        hand.addCard( deck.dealCard() );
        hand.addCard( deck.dealCard() );
        hand.showHand();
        System.out.println(deck.cardsLeft());
        System.out.println(hand.getHandSize());
        System.out.println(hand.getCard(0));
        System.out.println(hand.getCard(1));
        System.out.println(hand.getBlackJackValue());
        System.out.println();
        */

        Deck deck = new Deck();

        Hand p1hand = new Hand();
        Player player = new Player();

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name: ");
        player.setName(sc.nextLine());

        System.out.println("Welcome " + player.getName() + "!");

        player.setHand(p1hand);

        p1hand.addCard( deck.dealCard() );
        p1hand.addCard( deck.dealCard() );

        System.out.println(player.getName() + ": ");
        p1hand.showHand();
        System.out.println("Score: " + p1hand.getBlackJackValue());
        System.out.println();
    }
}

************

public class Card {

    public enum Value {

    public enum Value {
        TWO(2),
        THREE(3),
        FOU TWO(2),
        THREE(3),
        FOUR(4),
        FIVE(5),
        SIX(6),
        SEVEN(7),
        EIGHT(8),
        NINE(9),
        TEN(10),
        JACK(10),
        QUEEN(10),
        KING(10),
        ACE(1);

        public final int value;

        Value(int c) {
            value = c;
        }
    }

    public enum Suit {
        CLUBS('C'),
        SPADES('S'),
        DIAMONDS('D'),
        HEARTS('H');

        public final char symbol;

        Suit(char c) {
            symbol = c;
        }
    }

    private Suit suit;
    private Value value;

    public Card(Suit suit, Value value) {
        this.suit = suit;
        this.value = value;
    }

    public Suit getSuit() {
        return suit;
    }

    public Value getValue() {
        return value;
    }

    public char getSuitCode() {
        return suit.symbol;
    }

    public int getValueCode() {
        return value.value;
    }

    public String toString() {
        return value + " of " + suit;
    }
}

**************

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Deck {

    private List<Card> deck = new LinkedList<Card>();

    public Deck() {
        for(Card.Suit suit : Card.Suit.values()) {
            for(Card.Value value : Card.Value.values()) {
                deck.add(new Card(suit,value));
            }
        }
        shuffle();
    }

    public void shuffle() {
        Collections.shuffle(deck);
    }

    public int cardsLeft() {
          // As cards are dealt from the deck, the number of cards left
          // decreases.  This function returns the number of cards that
          // are still left in the deck.
        return deck.size();
    }

    public Card dealCard() {
          // Deals one card from the deck and returns it.
        if (deck.size() == 52) {
           shuffle();
        }
        Card temp;
        temp = deck.get(0);
        remove(0);
        return temp;
    }

    public Card getCard(int i) {
        return deck.get(i);
    }

    public Card remove(int i) {
        Card c = deck.get(i);
        deck.remove(i);
        return c;
    }   

}

*************

import java.util.Vector;

public class Hand {
    private Vector<Card> hand;

    public Hand() {
      hand = new Vector<Card>();
    }

    public void addCard(Card c) {
        if (c != null)
            hand.addElement(c);
    }

    public void showHand() {
        for(int i = 0; i < hand.size(); i++){
            System.out.println(hand.elementAt(i));
        }
    }

    public int getHandSize() {
        return hand.size();
    }

    public Card getCard(int position) {
        if(position >= 0 && position < hand.size())
            return (Card)hand.elementAt(position);
        else
            return null;
    }

    public int getBlackJackValue() {
        int val = 0;
        boolean ace = false;

        for(int i = 0; i < hand.size(); i++) {
            Card card;
            card = getCard(i);
            val += card.getValueCode();

            if(card.getValueCode() == 1) {
                ace = true;
            }
        }

        if(ace == true && val + 10 <= 21) {
            val += 10;
        }

        return val;
    }
}

***************

public class Player {
    private String name;
    private Hand hand;

    public Player() {
        this.hand = null;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setHand(Hand h) {
        this.hand = h;
    }

    public void getHand(){
        hand.showHand();
    }
}

That looks like a very good start.
Just keep adding stuff one bit at a time. Eg prompt for numger of Players and create them with theor Hands. You could consider a Game class that keeps track of whose turn it is next, who is bust etc. Hit and stand are decisions that a Player has to make, so that logic belongs in the Player class. The Game instance can deal a hand to each Player then poll them in turn to ask whether they chose to hit or stand, then see who wins. As always with MVC the Game class should not implement the U.I. The UI can stay in your "BlackJackTest" class until you have the logic working and are ready to do a proper GUI.

Hello JamesCherril, thanks for your reply.

When you say you should put the Hit/Stand decisions in the Player class, I have thought about doing this too.

I would like to do something like this:

    public void hit(Deck deck) {
        getHand().addCard( deck.dealCard() );
    }

After this of course I would then add the code to check if they have bust and what not. But my problem here is that when I then go on to create other classes it would create other problems would it not? Is there a much easier solution than this do you think.

I would like to be able to simply write the following when making the game run:

playername.hit();

Instead of having to assign a deck for it to hit from as with the above code.

Any thoughts?

Here is what I would like to add to the code, and I would add nextTurn(); onto the end of the newGame() method. But the problem is....I have initialized all of the players and their hands inside the newGame method, so how would I refer to them in this nextTurn method?

        //after initializing everything setup for a new game of blackjack
        //move on with the game
        nextTurn();
    }

    public void nextTurn() {
        //create a flag to keep looping through this until there is a winner
        //something like gameState

        //loop through each player who is playing
        //ask each player whether they want to hit or stand?
        //player.hit(Deck deck)
        //player.stand()
        //after loop, Dealer should also move, but I can code this later, get the Players done first

        //should also account for the fact more than 1 person may be set to "WIN" after 1 round of playing
        //at which point whoever has the better Score will be the winner
    }

You are heading in a good direction.
Your outstanding questions are all clearer when you sketch the Game class out a bit more, eg

class Game {
   private Deck theDeck, List<Player> thePlayers;
   public Game(int noOfPlayers) {
      //initialise everything
      // deal hands
      while (! gameOver) {
          for each Player
              ask hit or stand
              etc etc as above
          }
      }
  }

}

The Game has the Deck, so it can take the next Card and pass it to the Player's hit method as a parameter.
I showed the sketch as one block of code, but obviously you would split it into convenient methods.

This article has been dead for over six months. Start a new discussion instead.