class whose instances represent a full deck of cards

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vneha
    New Member
    • Sep 2008
    • 10

    class whose instances represent a full deck of cards

    [code=java]import java.util.*;

    public class Deck {

    public static int numSuits = 4;
    public static int numRanks = 13;
    public static int numCards = numSuits * numRanks;

    private Card[][] cards;

    public Deck() {
    cards = new Card[numSuits][numRanks];
    for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
    for (int rank = Card.ACE; rank <= Card.KING; rank++) {
    cards[suit-1][rank-1] = new Card(rank, suit);
    }
    }
    }

    public Card getCard(int suit, int rank) {
    return cards[suit-1][rank-1];
    }
    }[/code]


    Q:- Why this expression (cards[suit-1][rank-1] = new Card(rank, suit); ) is added?
    Last edited by Nepomuk; Sep 18 '08, 12:58 PM. Reason: Added [CODE] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Most likely Card.DIAMONDS and Card.ACE (the lowest values) are defined as 1.
    Index values start at 0. Quite clumsy if you'd ask me.

    kind regards,

    Jos

    Comment

    Working...