The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!
public void createDeckOfCards()
{
deck = new ArrayList<Card>();
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();
for (int i = 0; i < getPossibleRanks().length ; i++)
{
for (int j = 0; j < getPossibleSuits().length; j++)
{
deck.add(new Card(ranks[i], suites[j]));//
}
}
}
import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
// instance variables - replace the example below with your own
private ArrayList<Card> deck;
Random getRandom = new Random();
/**
* Constructor for objects of class DeckOfCards
*/
public DeckOfCards()
{
deck = new ArrayList<Card>();
resetDeckOfCards();
}
public void resetDeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();
for (int i = 0; i < ranks.length ; i++)
{
for (int j = 0; j < suites.length; j++)
{
deck.add(new Card(ranks[i], suites[j]));//
}
}
}
public Card getCard()//remove random card of arraylist of cards and return value from method
{
if (deck.isEmpty())
resetDeckOfCards();
return deck.remove(getRandom.nextInt(deck.size()));
}
public ArrayList<Card> getHand(int size)
{
ArrayList<Card> hand = new ArrayList<Card>();
for (int i = 0; i < size; i++)
{
hand.add(this.getCard());
}
return hand;
}
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
What you call resetDeckOfCards() this is in fact the method which creates the
deck of cards - waht else is necessary?
Just rename it to createDeck and
create new ArrayList in the beginnighg:
Open in new window