0

Not sure why I can't get it to print out one single card. I'm just stuck on this one part. Any advice of help would be greatly appreciated.

This is the card class i've created. Seem to be working fine with other programs I've made.

public class Card {

private int faceValue;
private int suit;

public static final int HEARTS = 1;
public static final int DIAMONDS = 2;
public static final int CLUBS = 3;
public static final int SPADES = 4;
private static final int MAX_SUITS = 4;
private static final int MAX_FACES = 13;
//sets the face and suit of card at random
public Card() {
    faceValue = (int) (Math.random() * MAX_FACES) + 1;
    suit = (int) (Math.random() * MAX_SUITS) + 1;
}

public Card(int faceValue, int suit) {
    this.faceValue = faceValue;
    if (faceValue < 1 || faceValue > 13) {
        System.out.println("ERROR: Invalid face value");
        this.faceValue = 1;
    }
    this.suit = suit;
    if (suit != HEARTS && suit != DIAMONDS && suit != CLUBS && suit != SPADES) {
        System.out.println("ERROR: Invalid suit value");
        this.suit = HEARTS;
    }
}
//return the face value
public int getFaceValue() {
    return faceValue;
}
//returns the suit
public int getSuit() {
    return suit;
}
//converts suit/face number to name as string
public String toString() {
    String s = "";
    switch (faceValue) {
        case 1:
        s += " Ace";
        break;
        case 11:
        s += " Jack";
        break;
        case 12:
        s += " Queen";
        break;
        case 13:
        s += " King";
        break;
        case 10:
        s += " 10";
        break;
        default:
        s += " " + faceValue;
        break;
    }

    switch (suit) {
        case HEARTS:
        s += " of Hearts";
        break;
        case DIAMONDS:
        s += " of Diamonds";
        break;
        case CLUBS:
        s += " of Clubs";
        break;
        case SPADES:
        s += " of Spades";
        break;
    }

    return s;
}

}

This is the deckOfCards class I'm having trouble with. Especially the dealCard method.

import java.util.Random;
public class DeckOfCards
{
public static final int SIZE = 52;
private Card[] deckOfCards; //Contains all 52 cards

// Creates a normal Deck of 52 Cards
public DeckOfCards()
{
    deckOfCards = new Card[SIZE];
    int i=0;
    for(int suit=1;suit<=4;suit++)
    {
        for(int face=1;face<=13;face++)
        {
            deckOfCards[i++] = new Card(face,suit);
        }
    }
}

public int dealCard()
{
    return deckOfCards[];
}
// Returns Deck of Cards
public Card[] getDeckOfCards()
{
    return deckOfCards;
}

public int cardRemaining()
{
    int i = 0;
    return SIZE - i;
}

// Shuffles a Deck of Cards randomly
public void shuffle()
{
    Random rand = new Random();
    Card temp; // store a number temporarily
    int j; // random index to swap
    for(int i=0;i<SIZE;i++)
    {
        j = rand.nextInt(SIZE);
        temp = deckOfCards[i];
        deckOfCards[i] = deckOfCards[j];
        deckOfCards[j] = temp;
    }
}
}

this is just a simple test to see if it can even deal a card.

class DeckTester 
{
public static void main(String[] args) 
{
    DeckOfCards deck = new DeckOfCards();
    Card curCard = null;

    deck.shuffle();

    System.out.println(deck.dealCard());
}
}
Amine
  • 3
  • 1

0 Answers0