Question

I have a hand class that has a number of cards. The dealer deals a card to my hand and I want my game to display the cards that the user currently has. Here is the game code where in my GameEngine class I print out the cards in the hand. The problem is that it prints out the whole deck! This is the print out:Your hand: [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King] of Spades. I only want it to print the one card, not all of them, so what is messed up in my code?

public void beginCardDeal () {
    for (int x = 0; x < 2; x++) {
        System.out.print("\nDealer is dealing a card. . . \n");
        d.dealCard();
        System.out.println("\nYour hand: " + pl.getMyCards());
    }
}

pl is an instance of the Player class since Players have a hand. d is an instance of Deck.

public class Player extends Person {

private Hand myCards = new Hand();
private int playerCashAmount = 100;

public Player() {

}

public Player(String name) {
    this.name = name;
}

public String getName() {
    return super.getName();
}

public int getPlayerCashAmount() {
    return playerCashAmount;
}

public void setPlayerCashAmount(int playerCashAmount) {
    this.playerCashAmount = playerCashAmount;
}

public Hand getMyCards() {
    return myCards;
}

public void setMyCards(Hand myCards) {
    this.myCards = myCards;
}

Also, here is my Deck class:

public class Deck {

private ArrayList<Card> deck = new ArrayList<Card>();
private List<Card> cardUsed = new ArrayList<Card>();

int numCards = 0;

public Deck(int numCards) {
    this.createDeck(numCards, 4);
}

private void createDeck(int numCards, int numSuits) {
    deck = new ArrayList<Card>();
    cardUsed = new ArrayList<Card>();
    if ((numCards % numSuits) > 0) return;
    for (int i=0; i < numSuits; i++) {
        for(int j=1; j <= (numCards / numSuits); j++) {
            deck.add(new Card(new Suit(i), j));
        }
    }
}

public Card dealCard( ) {
    Card dealtCard = null;
    if (deck.size() == 0){
        deck.addAll(cardUsed);
        this.shuffle();
        cardUsed = new ArrayList<Card>();
    }

    dealtCard = deck.get(0);
    deck.remove(0);
    cardUsed.add(dealtCard);

    return dealtCard;
}

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

public ArrayList<Card> getDeck() {
    return deck;
}

public void setDeck(ArrayList<Card> deck) {
    this.deck = deck;
}

public int getNumUsedCards() {
    return cardUsed.size();
}

public List<Card> getCardUsed() {
    return cardUsed;
}

public void setCardUsed(List<Card> cardUsed) {
    this.cardUsed = cardUsed;
}

EDIT:

public class Hand {

int total = 0;
private ArrayList<Card> hand = new ArrayList<Card>();

public Hand(){

}

public String toString(){
    //Suit s = new Suit(total);
    //Card c = new Card(s, total);
    //return (Arrays.toString(c.getCardRanks())) + " of " + s.getSuitName();
    return Arrays.toString(hand.toArray());
}

public boolean discardHand(){
    if (hand.isEmpty()) {
        hand = new ArrayList<Card>();
    }
    return false;
}

public void addCard(Card c) {
    this.hand.add(c);
}

public Card getPosition(int index){
    return hand.get(index);
}

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

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}

public ArrayList<Card> getHand() {
    return hand;
}

public void setHand(ArrayList<Card> hand) {
    this.hand = hand;
}
Was it helpful?

Solution 2

Your objects need to better reflect the nature of what they represent: a deck and a hand are each just an ordered set of cards, so using an underlying array list (or a simple array) for them is fine. "Dealing" a card is the act of moving a card from the deck to a hand, so it should either be a method of the deck object taking the hand to deal to as an argument, e.g. deck.dealTo(hand), or else return a card and use an assignment, e.g. hand.add(deck.deal()). Your present d.dealCard() removes a card from the deck and drops it on the floor. It also deals in a strange way--I can't imagine any game in which when the deck is exhausted you just shuffle a new deck and keep dealing!

Likewise, the player object should contain a reference to a hand object (or maybe none, or more than one, depending on the game), but you don't deal to players, you deal to hands. Also some "hands" may not be associated with a player at all, like the "crib" in cribbage or the "board" in Texas Hold'em.

OTHER TIPS

When you deal a card, you don't add it to the players hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top