Processing Forum
class Card { // declare variables int suit; // 0, 1, 2, 3 represent "clubs", "diamonds", "hearts", "spades" int rank; // 1 through 13 (1 is Ace, 11 is jack, 12 is queen, 13 is king) PImage cardImage; // holds the image boolean faceUp = false ; // show face of card // constructor Card() { } // Temporary variable assignment Card(int tempsuit, int temprank) { suit = tempsuit; rank = temprank; cardImage = loadImage(tempsuit + "-" + temprank+".png"); } void displayCard(float x, float y) { if (faceUp) { image(cardImage, x, y); } else { fill(0); rect(x, y, 72, 96); } } void setFace(boolean updown) { faceUp = updown; } int getRank() { switch(rank) { case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; case 6: return 6; case 7: return 7; case 8: return 8; case 9: return 9; case 10: return 10; case 11: return 10; case 12: return 10; case 13: return 10; default: return 0; } } int getSuit() { return suit; } } class Deck extends Card { List<Card> deck = new ArrayList<Card>(); // list of cards in the deck int NUMBER_OF_CARDS = 13; // # of cards in a std suit of playing cards int NUMBER_OF_SUITS = 4; // # of suits in a std deck of playing cards // Constructs an unshuffled deck of playing cards. Deck() { for (int suit = 0; suit < NUMBER_OF_SUITS; suit++) for (int value = 1; value <= NUMBER_OF_CARDS; value++ ) addCard(new Card(suit, value)); } //Adds a card to the deck's ArrayList. public void addCard(Card c) { deck.add(c); } //Returns a reference to the card object at index in the deck's ArrayList. //index of the card to return //return a card object Card getCard(int index) { return (Card)deck.get(index); } //Returns the size of the deck. // @return an int representing the size of the deck public int getSize() { return deck.size(); } //Shuffles the deck. public void shuffle() { for (int i = 0; i < 100 ; i++) { int swapIndex1=(int)Math.floor(Math.random()*52); if (swapIndex1==52) swapIndex1--; int swapIndex2=(int)Math.floor(Math.random()*52); if (swapIndex2==52) swapIndex2--; //swap them Card tmp=deck.get(swapIndex1); deck.set(swapIndex1, deck.get(swapIndex2)); deck.set(swapIndex2, tmp); // Collections.shuffle(deck); } } //Deals (returns and removes) a card from the top (front) of the deck. public Card dealCard() { return (Card)deck.remove(0); }
}
and this is how i call the class