How to call Arraylist
in
Programming Questions
•
9 months ago
Hello all,
I am stuck on this bit of code. I have done alot of coding and i need a little bit help to finish it.
I created a card class and a deck class. I tested the card class and it is working perfect. I am not sure if my deck class is working since i can not get it running.
I do not know how to call deck class and card class in my set up and use them.
Without using the deck class i created 52 cards to test it and it works
here is my classes.
I am stuck on this bit of code. I have done alot of coding and i need a little bit help to finish it.
I created a card class and a deck class. I tested the card class and it is working perfect. I am not sure if my deck class is working since i can not get it running.
I do not know how to call deck class and card class in my set up and use them.
Without using the deck class i created 52 cards to test it and it works
here is my classes.
- Card[] testCards = new Card[52];
- void setup() {
- size(1000, 600);
- testCreateCards();
- }
- void draw() {
- background(220);
- testDrawCards();
- }
- void testCreateCards() {
- int k = 0;
- for (int suit = 0; suit <= 3; suit++) { // for suit
- for (int value = 1; value <= 13; value++) { // from Ace to King
- // build new card
- testCards[k++] = new Card(suit, value);
- }
- }
- println(testCards);
- noLoop();
- }
- void testDrawCards() {
- fill(0);
- for (int i = 0; i < 52; i++) {
- int x = i % 13;
- int y = i / 13;
- testCards[i].setFace(true); // face up
- testCards[i].displayCard(40 + x * 90, y * 140); // draw them
- println(testCards[i].getRank()); // println
- text("Value:" + testCards[i].getRank() + " of " + testCards[i].getSuit(), 40+x * 90, y * 140 + 120);
- }
- }
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); }
}
1