Reputation:
Over here I have some code regarding a carddeck that I'm trying to make in Java. The idea behind it is, to make 2 methods, to create a createDeck method, that returns an ArrayList that also contains 52 different cards.
And randomizing them. so basically placing the list of cards provided as a parameter and placing them in random order. Could it be done using "Collections.shuffle(arrayList);"?
Any help would be appreciated
class Card
{
public static final String[] SUIT = new String[]{"Clubs", "Spades", "Diamonds", "Hearts"};
public static final String[] RANK = new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
public static ArrayList<Card> createDeck()
{
}
private Integer suit;
private Integer rank;
public Card(Integer suit, Integer rank)
{
this.suit = suit;
this.rank = rank;
}
public String toString()
{
return "'" + SUIT[this.suit] + " " + RANK[this.rank] + "'";
}
}
class Main
{
public static void main(String[] arguments)
{
System.out.println(new Card(2, 6));
System.out.println(new Card(3, 12));
}
}
Upvotes: 1
Views: 866
Reputation: 654
You could use an enum to implement is. This would give you the possibility to assign values to the ranks. Another benefit would be, that you can easily generate a reduced deck, e.g. without cards 2, 3 and 4 or so.
I wrote a solution with internal classes, but you could pull them out as well.
public class Cards {
public static void main(String[] args) {
Cards cards = new Cards();
final List<Card> deck = cards.createDeck();
Collections.shuffle(deck);
deck.forEach(System.out::println);
}
private List<Card> createDeck() {
final List<Card> deck = new ArrayList<>();
for (Rank rank : Rank.values()) {
for (Suit suit : Suit.values()) {
deck.add(Card.of(suit, rank));
}
}
return deck;
}
public enum Suit {
Clubs,
Spades,
Diamonds,
Hearts
}
public enum Rank {
Ace("Ace"),
King("King"),
Queen("Queen"),
Jack("Jack"),
Ten("10"),
Nine("9"),
Eight("8"),
Seven("7"),
Six("6"),
Five("5"),
Four("4"),
Three("3"),
Two("2");
private final String name;
Rank(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static class Card {
private final Suit suit;
private final Rank rank;
private Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public static Card of(Suit suit, Rank rank) {
return new Card(suit, rank);
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
@Override
public String toString() {
return rank.getName() + " of " + suit;
}
}
}
Upvotes: 1
Reputation: 1098
Idea is to basically iterate through all possible combinations of SUIT
and RANK
. You can do that by looping through them in nested way.
Make sure you add these combinations in ArrayList
using ArrayList.add(Object)
method. This will generate your deck.
Now, to randomize this deck you can use Java Collections
library. Call random method with Collections.shuffle(deck)
and whole deck will be shuffled.
import java.util.ArrayList;
import java.util.Collections;
class Card {
public static final String[] SUIT = new String[]{"Clubs", "Spades", "Diamonds", "Hearts"};
public static final String[] RANK = new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
public static ArrayList<Card> createDeck() {
ArrayList<Card> deck = new ArrayList<>();
for(int i=0; i<4; i++){
for(int j=0; j<13; j++){
deck.add(new Card(i, j));
}
}
Collections.shuffle(deck);
return deck;
}
...
}
Upvotes: 1