right now I am working on a coupon collector program with a deck of cards. What has to happen is the program will run until it finds all 4 suits of cards. Already I have the deck of cards created. I am just confused on how to pick and store the cards from the deck. The way I would do it is to store the first card. Then compare the next card to the first card and so on. Once it finds a different suit it will store that card and and the comparison will continue. Any help on doing this is appreciated. Thanks!
import java.util.ArrayList;
import java.util.Random;
public class Driver {
public static void main(String[] args) {
ArrayList<Card> deck = new ArrayList<Card>(52); //creating the array
for (int i = 0; i < 52; i++)
{
Card c = new Card();
deck.add(c);
}
int i = 0;
for (int s = 1; s <= 4; s++) //making the faces and suits for cards
{
for (int f = 1; f <= 13; f++)
{
Card k = deck.get(i);
String face = " ";
String suit = " ";
switch (f)
{
case 1: face = "Ace";break;
case 2: face = "2";break;
case 3: face = "3";break;
case 4: face = "4";break;
case 5: face = "5";break;
case 6: face = "6";break;
case 7: face = "7";break;
case 8: face = "8";break;
case 9: face = "9";break;
case 10: face = "10";break;
case 11: face = "Jack";break;
case 12: face = "Queen";break;
case 13: face = "King";break;
}
switch(s)
{
case 1: suit = "Clubs";break;
case 2: suit = "Spades";break;
case 3: suit = "Diamonds";break;
case 4: suit = "Hearts";break;
}
k.setCard(face, suit);
deck.set(i, k);
i++;
}
}
Random rd = new Random();
for (int index = 0;index < 52; index++) //shuffling
{
Card c = deck.get(index);
deck.remove(index);
int n = rd.nextInt(52);
deck.add(n, c);
}
System.out.println("THANKS FOR USING MY PROGRAM");
}
}
public class Card {
private String face;
private String suit;
public Card()
{
face = " ";
suit = " ";
}
public void setCard (String f, String s)
{
face = f;
suit = s;
}
public void displayCard()
{
System.out.print(" " + face + " " + "of" + " "+ suit);
}
}
Aucun commentaire:
Enregistrer un commentaire