3

I'm working on a project for my computer science class. My teacher wants us to create a program that will have a deck of cards, be able to shuffle them, and deal a hand. He wants us to create a card class that has the variables suitName, suitValue, faceName, and faceValue. I currently have code that looks like this:

//Construct Array
Card[] deck = new Card[52];
//Define cards
Card aceSpades = new Card("Spades", 1, "Ace", 1);
Card aceHearts = new Card("Hearts", 2, "Ace", 1);
Card aceClubs = new Card("Clubs", 3, "Ace", 1);
Card aceDiamonds = new Card("Diamonds", 4, "Ace", 1);
Card twoSpades = new Card("Spades", 1, "Two", 2);
Card twoHearts = new Card("Hearts", 2, "Two", 2);
Card twoClubs = new Card("Clubs", 3, "Two", 2);
Card twoDiamonds = new Card("Diamonds", 4, "Two", 2);
Card threeSpades = new Card("Spades", 1, "Three", 3);
Card threeHearts = new Card("Hearts", 2, "Three", 3);
Card threeClubs = new Card("Clubs", 3, "Three", 3);
Card threeDiamonds = new Card("Diamonds", 4, "Three", 3);
Card fourSpades = new Card("Spades", 1, "Four", 4);
Card fourHearts = new Card("Hearts", 2, "Four", 4);
Card fourClubs = new Card("Clubs", 3, "Four", 4);
Card fourDiamonds = new Card("Diamonds", 4, "Four", 4);
Card fiveSpades = new Card("Spades", 1, "Five", 5);
Card fiveHearts = new Card("Hearts", 2, "Five", 5);
Card fiveClubs = new Card("Clubs", 3, "Five", 5);
Card fiveDiamonds = new Card("Diamonds", 4, "Five", 5);
Card sixSpades = new Card("Spades", 1, "Six", 6);
Card sixHearts = new Card("Hearts", 2, "Six", 6);
Card sixClubs = new Card("Clubs", 3, "Six", 6);
Card sixDiamonds = new Card("Diamonds", 4, "Six", 6);
Card sevenSpades = new Card("Spades", 1, "Seven", 7);
Card sevenHearts = new Card("Hearts", 2, "Seven", 7);
Card sevenClubs = new Card("Clubs", 3, "Seven", 7);
Card sevenDiamonds = new Card("Diamonds", 4, "Seven", 7);
Card eightSpades = new Card("Spades", 1, "Eight", 8);
Card eightHearts = new Card("Hearts", 2, "Eight", 8);
Card eightClubs = new Card("Clubs", 3, "Eight", 8);
Card eightDiamonds = new Card("Diamonds", 4, "Eight", 8);
Card nineSpades = new Card("Spades", 1, "Nine", 9);
Card nineHearts = new Card("Hearts", 2, "Nine", 9);
Card nineClubs = new Card("Clubs", 3, "Nine", 9);
Card nineDiamonds = new Card("Diamonds", 4, "Nine", 9);
Card tenSpades = new Card("Spades", 1, "Ten", 10);
Card tenHearts = new Card("Hearts", 2, "Ten", 10);
Card tenClubs = new Card("Clubs", 3, "Ten", 10);
Card tenDiamonds = new Card("Diamonds", 4, "Ten", 10);
Card jackSpades = new Card("Spades", 1, "Jack", 11);
Card jackHearts = new Card("Hearts", 2, "Jack", 11);
Card jackClubs = new Card("Clubs", 3, "Jack", 11);
Card jackDiamonds = new Card("Diamonds", 4, "Jack", 11);
Card queenSpades = new Card("Spades", 1, "Queen", 12);
Card queenHearts = new Card("Hearts", 2, "Queen", 12);
Card queenClubs = new Card("Clubs", 3, "Queen", 12);
Card queenDiamonds = new Card("Diamonds", 4, "Queen", 12);
Card kingSpades = new Card("Spades", 1, "King", 13);
Card kingHearts = new Card("Hearts", 2, "King", 13);
Card kingClubs = new Card("Clubs", 3, "King", 13);
Card kingDiamonds = new Card("Diamonds", 4, "King", 13);
deck[0] = aceSpades;
deck[1] = aceHearts;
deck[2] = aceClubs;
deck[3] = aceDiamonds;
deck[4] = twoSpades;
deck[5] = twoHearts;
deck[6] = twoClubs;
deck[7] = twoDiamonds;
deck[8] = threeSpades;
deck[9] = threeHearts;
deck[10] = threeClubs;
deck[11] = threeDiamonds;
deck[12] = fourSpades;
deck[13] = fourHearts;
deck[14] = fourClubs;
deck[15] = fourDiamonds;
deck[16] = fiveSpades;
deck[17] = fiveHearts;
deck[18] = fiveClubs;
deck[19] = fiveDiamonds;
deck[20] = sixSpades;
deck[21] = sixHearts;
deck[22] = sixClubs;
deck[23] = sixDiamonds;
deck[24] = sevenSpades;
deck[25] = sevenHearts;
deck[26] = sevenClubs;
deck[27] = sevenDiamonds;
deck[28] = eightSpades;
deck[29] = eightHearts;
deck[30] = eightClubs;
deck[31] = eightDiamonds;
deck[32] = nineSpades;
deck[33] = nineHearts;
deck[34] = nineClubs;
deck[35] = nineDiamonds;
deck[36] = tenSpades;
deck[37] = tenHearts;
deck[38] = tenClubs;
deck[39] = tenDiamonds;
deck[40] = jackSpades;
deck[41] = jackHearts;
deck[42] = jackClubs;
deck[43] = jackDiamonds;
deck[44] = queenSpades;
deck[45] = queenHearts;
deck[46] = queenClubs;
deck[47] = queenDiamonds;
deck[48] = kingSpades;
deck[49] = kingHearts;
deck[50] = kingClubs;
deck[51] = kingDiamonds;

Is there a more efficent way to do this

SamH
  • 203
  • 1
  • 2
  • 13

5 Answers5

4

Yes:

  1. Assign directly into elements of deck
  2. Use one or more loops to go through the suits & values
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
2
Card[] deck = new Card[52];

deck[0] = new Card("Spades", 1, "Ace", 1);  

you can assign reference to your array index

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
2

The following code can work for you. It simply iterates two enumerations and also shuffles the cards for you.

The following abstractions are used:

  • Suit
  • Rank
  • Card (a Rank and a Suit)
  • Deck: contains the cards (shuffled if required)

Easy abstractions to work with and non-complex code to keep all cards.

public enum Rank {
    ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}

public enum Suit {
    hearts, clubs, diamonds, spades
}

public class Card {
    private final Rank rank;
    private final Suit suit;

    public Card(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() {
        return rank;
    }

    public Suit suit() {
        return suit;
    }
}

public class Deck {
    // Static
    private static final List<Card> allCards = new ArrayList<>();

    static {
        for (Suit s : Suit.values()) {
            for (Rank r : Rank.values()) {
                allCards.add(new Card(r, s));
            }
        }
    }

    private final List<Card> shuffledCards;

    public Deck() {
        // Shuffle
        shuffledCards= new ArrayList<>(allCards);
        Collections.shuffle(shuffledCards);
    }

    public Iterable<Card> shuffledCards() {
        return shuffledCards;
    }
}
wassgren
  • 18,651
  • 6
  • 63
  • 77
  • This uses a few features I'm not familiar with. I copied this into my IDE and I get errors with public enum Rank{ and public enum Suit{. It said they must be defined in their own file. – SamH Jan 08 '15 at 20:23
  • 1
    Well, enums are available from Java 5 onwards. The enum should be in its own file (like a public class) – wassgren Jan 08 '15 at 20:24
  • When defining your `List`, you forgot to add the generic into the `ArrayList()` – Ascalonian Jan 08 '15 at 20:25
  • @Ascalonian - that is not needed from Java 7 onwards. Check [this](http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7) out. – wassgren Jan 08 '15 at 20:26
  • @wassgren - I was actually referring to nothing in the diamond versus putting in the generics again (not what the link you provided was about, but a good read nonetheless!). So I know it's not needed, I just thought it was proper coding standard to include them (and got warnings about using them), that's all :-) But I just tried again and not seeing warnings anymore! – Ascalonian Jan 08 '15 at 20:35
  • @wassgren I'm still getting errors after putting it into a separate file. It gives me the option to import the rank enum from the other file into my card class, it then tells me to remove unused imports. – SamH Jan 08 '15 at 20:36
  • @SamH - Good, what was the problem? Something that needs fixing in the answer? – wassgren Jan 08 '15 at 21:14
  • @wassgren I was putting both the suit and rank enums into one file. I fixed it, and I renamed the enums to suitEnum and rankEnum. That fixed all of my problems with the Card class however in the deck class I can't get the for loop to work. I formatted it as: for(Suit s: suitEnum.values()) but I get an error that says suitEnum cannot be resolved. – SamH Jan 09 '15 at 00:35
  • @wassgren I fixed my previous problem. Now I have what I believe should be the last problem. I'm trying to use an array instead of a list because our teacher has not taught us lists yet and he gets annoyed when we use things he hasn't taught us. When I do this with an array instead of a list I get the first four assigned and then everything else null. – SamH Jan 09 '15 at 02:29
  • @wassgren once again, I have figured it out – SamH Jan 09 '15 at 13:18
0

To simplify your code, here's what I would do (just my two cents):

  1. In your Card class, just have your constructor take two arguments accepting ints and assign values to your suitName and faceName variables, something like:

    public Card(int suitValue, int faceValue) {
        switch (suitValue) {
            case 1: suitName = "Spades";   break;
            case 2: suitName = "Hearts";   break;
            case 3: suitName = "Clubs";    break;
            case 4: suitName = "Diamonds"; break;
            default: throw new RuntimeException("Invalid suit value: " + String.valueOf(suitValue));
        }
        // same for faceName
        this.suitValue = suitValue;
        this.faceValue = faceValue;
    }
    
  2. Create your array using a loop:

    Card[] deck = new Card[52];
    for (int k = 1; k <= 4; k++) {
        for (int l = 1; l <= 13; l++) {
            card[(k-1)*13+(l-1)] = new Card(k,l);
        }
    }
    
TNT
  • 2,900
  • 3
  • 23
  • 34
0

I would do it like this:

    String[] suitNames = {"Spades", "Hearts", "Clubs", "Diamonds" };
    String[] faceNames = {"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"};
    List<Card> deck = new ArrayList<Card>();
    for (int i = 0; i < suitNames.length; i++) {
        String suitName = suitNames[i];
        int suitValue = i + 1;
        for (int j = 0; j < faceNames.length; j++) {
            String faceName = faceNames[j];
            int faceValue = j + 1;
            Card c = new Card(suitName, suitValue, faceName, faceValue);
            deck.add(c);
        }
    }
bhspencer
  • 13,086
  • 5
  • 35
  • 44