Question

I'm trying to make a very simple Black Jack game, and I'm having problems drawing cards from an Array List and putting them into a players hand, and a dealers hand...

Here's what I have so far, anyone willing to help will be greatly appreciated.

I also realize that I many not be doing it in the most efficient way...

CARDS ARRAY LIST

import java.util.ArrayList;

public class deck {
    ArrayList<card> deck = new ArrayList<card>();

public deck () {

    deck = new ArrayList<card>();

    //Spades
    deck.add(new card ("2", "Spades"));
    deck.add(new card ("3", "Spades"));
    deck.add(new card ("4", "Spades"));
    deck.add(new card ("5", "Spades"));
    deck.add(new card ("6", "Spades"));
    deck.add(new card ("7", "Spades"));
    deck.add(new card ("8", "Spades"));
    deck.add(new card ("9", "Spades"));
    deck.add(new card ("10", "Spades"));
    deck.add(new card ("2", "Spades"));
    deck.add(new card ("J", "Spades"));
    deck.add(new card ("Q", "Spades"));
    deck.add(new card ("K", "Spades"));
    deck.add(new card ("A", "Spades"));
    //Clubs
    deck.add(new card ("2", "Clubs"));
    deck.add(new card ("3", "Clubs"));
    deck.add(new card ("4", "Clubs"));
    deck.add(new card ("5", "Clubs"));
    deck.add(new card ("6", "Clubs"));
    deck.add(new card ("7", "Clubs"));
    deck.add(new card ("8", "Clubs"));
    deck.add(new card ("9", "Clubs"));
    deck.add(new card ("10", "Clubs"));
    deck.add(new card ("2", "Clubs"));
    deck.add(new card ("J", "Clubs"));
    deck.add(new card ("Q", "Clubs"));
    deck.add(new card ("K", "Clubs"));
    deck.add(new card ("A", "Clubs"));
    //Hearts
    deck.add(new card ("2", "Hearts"));
    deck.add(new card ("3", "Hearts"));
    deck.add(new card ("4", "Hearts"));
    deck.add(new card ("5", "Hearts"));
    deck.add(new card ("6", "Hearts"));
    deck.add(new card ("7", "Hearts"));
    deck.add(new card ("8", "Hearts"));
    deck.add(new card ("9", "Hearts"));
    deck.add(new card ("10", "Hearts"));
    deck.add(new card ("2", "Hearts"));
    deck.add(new card ("J", "Hearts"));
    deck.add(new card ("Q", "Hearts"));
    deck.add(new card ("K", "Hearts"));
    deck.add(new card ("A", "Hearts"));
    //Diamonds
    deck.add(new card ("2", "Diamonds"));
    deck.add(new card ("3", "Diamonds"));
    deck.add(new card ("4", "Diamonds"));
    deck.add(new card ("5", "Diamonds"));
    deck.add(new card ("6", "Diamonds"));
    deck.add(new card ("7", "Diamonds"));
    deck.add(new card ("8", "Diamonds"));
    deck.add(new card ("9", "Diamonds"));
    deck.add(new card ("10", "Diamonds"));
    deck.add(new card ("2", "Diamonds"));
    deck.add(new card ("J", "Diamonds"));
    deck.add(new card ("Q", "Diamonds"));
    deck.add(new card ("K", "Diamonds"));
    deck.add(new card ("A", "Diamonds"));
}

public ArrayList<card> getCards(){
    return deck;
}

public card deal(){
    card one = deck.get((int) (Math.random() * deck.size()));
    deck.remove(one);
    return one;
}

}

PLAYER & DEALER HAND CLASS

Here is where I'm looking to fill dealPlayer() & dealDealer() with two random cards from the array, I'm just in a stump right now and cant seem to figure out anything that will work... Thanks in advance for help, if theres anything else i need to post, please let me know!

import java.util.ArrayList;

public class dealer {

    deck deck = new deck();
    String dealerTopCardString = "";
    card playerHandCard;
    card dealerHandCard;
    ArrayList<card> dealerHand = new ArrayList<card>();
    ArrayList<card> playerHand = new ArrayList<card>();

public void start() {
    dealerHand = new ArrayList<card>();
    playerHand = new ArrayList<card>();
}

public void dealPlayer() {
    //LOOKING FOR HELP HERE
}

public void dealDealer() {
    //LOOKING FOR HELP HERE
}

public String getPlayerHand() {

    String playerHandString = "";
    for (int i = 0; i < playerHand.size(); i++)
        playerHandCard = playerHand.get(i);
        card card = new card(playerHandCard.getValue(), playerHandCard.getSuit());
        playerHandString += "\n";
        playerHandString += card.getValue();
        playerHandString += "\t";
        playerHandString += card.getSuit();

        return playerHandString;

    }

public String getDealerHand() {

        String dealerHandString = "";
        for (int i = 0; i < dealerHand.size(); i++)
            dealerHandCard = dealerHand.get(i);
            card card = new card(dealerHandCard.getValue(), dealerHandCard.getSuit());
            dealerHandString += "\n";
            dealerHandString += card.getValue();
            dealerHandString += "\t";
            dealerHandString += card.getSuit();

            return dealerHandString;
    }

public String getDealerTopCard(){

    return getDealerTopCard;
}

public int getPlayerHandValue(){

    return getPlayerHandValue;
}

public int getDealerHandValue(){


    return getDealerHandValue;
}

public void resetDealerHand(){
    dealerHand.clear();
}

public void resetPlayerHand(){
    playerHand.clear();
}

}
Was it helpful?

Solution

When you post a question, it's usually best practice to say what you have tried. That way others can help you see where you might have gone wrong.

I'm just going to give you some general advice on how you should go about solving the problem. If you have specific problems with implementing it you can ask a new question (or update this one depending on how related it is).

In a real game of blackjack, you'd shuffle the deck and then remove the top 4 cards. You can do the same thing here:

  1. Shuffle the ArrayList
  2. Remove the top 4 cards to put in the player's/dealer's hands. (Maybe you could use the remove method to remove the first or last element in the deck.)

This looks like it might be homework, so you might be required to use an ArrayList. That's really not the best data structure to use here though. If you have a choice, I'd use an ArrayDeque instead. It has efficient pop() and remove() methods for taking the first or last item out of the collection.

(Hint: "deque" stands for double-ended-queue, but it's pronounced "deck". This is because it's conceptually like a deck of cards...)

OTHER TIPS

Just shuffle your deck of cards using Collections#shuffle

Collections.shuffle(deck);

Then just get the next card from the top of the deck using List#remove, so something like this for dealing the starting hands

playerHand.add(deck.remove(0));   // top card to player
dealerHand.add(deck.remove(0));   // top card to dealer
playerHand.add(deck.remove(0));
dealerHand.add(deck.remove(0));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top