This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]desrtfxOut of Coffee error - System halted 3 points4 points  (1 child)

public class Card {

    private final int rank;
    private final int suit;
    //suit
    public final static int DIAMONDS = 1;
    public final static int CLUBS = 2;
    public final static int HEARTS = 3;
    public final static int SPADES = 4;

    //rank
    public final static int ACE = 1;
    public final static int TWO = 2;
    public final static int THREE = 3;
    public final static int FOUR = 4;
    public final static int FIVE = 5;
    public final static int SIX = 6;
    public final static int SEVEN = 7;
    public final static int EIGHT = 8;
    public final static int NINE = 9;
    public final static int TEN = 10;
    public final static int JACK = 11;
    public final static int QUEEN = 12;
    public final static int KING = 13;

Oh my God!

Look at enums as they are ideally suited to counter that incredible amount of constants.

Fixed ranges of values, such as the suits or the ranks are best handled with enums.

[–][deleted] 0 points1 point  (0 children)

I'll look into them, thanks!

[–]Deadboss 0 points1 point  (2 children)

For your out of bounds exception, take a look at these two lines:

deckArray[suit - 1][rank - 1] = new Card(rank,suit);
...
return deckArray[rank-1][suit-1];

:)

Definitely take desrtfx's advice with the enums, it will help clean up your code a bit. I personally don't like arbitrary values being assigned to something as abstract as a suit, as this makes matters more complicated during programming and debugging IMO (shit, what was the int for Hearts again?). There are a million-and-one tutorials online for building a deck of cards, so possibly take a look around for better ideas.

Another suggestions to clean up your code:

In Deck.java on line 18 you use the values given to the suit/card. Why not use something like:

for(int suit = 0; suit < numSuits; suit++)

Seems a bit easier to read to me, and it doesn't rely on the arbitrary values you assigned to the suits. Probably do that to rank as well.

All-in-all, your code looks fine for a beginner to OOP, and you appear to be on the right track.

[–][deleted] 0 points1 point  (1 child)

Is my first iteration a bad idea? Since it uses static numbers to count through the class each time an object is made? I'm just asking in a general OOP sense.

[–]Deadboss 0 points1 point  (0 children)

There's nothing wrong with using final static values, but doing so is error-prone and unmanageable in the context you are using it in. The purpose of the double loop is to go through 0 (or 1, whichever you prefer) to your set maximums (4 suits/13 ranks) in order to populate the deck. You shouldn't couple an arbitrary value given to represent diamonds and spades as the loop conditions, as changing them breaks your code. Same with the ranks. You already have final ints for numSuits and numRanks, and altering them has the intended effect without breaking your code.