Reputation: 11
I'm quite new to java and have created a Java class that creates a deck of cards, and assigns them a suite, a name, a value and an ID. The problem is, there are 52 different cards that require an ID, and I've been using switch statements to assign the name, value and suite. However, if I were to do this for the card ID, I would need 52 lines of case statements, which is far too many.
public class Deck {
private Card[] cards;
public Deck() {
String suit = null;
String name = null;
int cardID=0;
int value = 0;
cards = new Card[52];
int arrayID=0;
for (int i=1; i<=4; i++){ //number of suites
for (int j=1; j <= 13; j++){ //number of card types
for (int k=1; k==52; k++){ //number of cards
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 11; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
switch (k){
case k: cardID=k; break; //"Case expressions must be constant expressions"
}
Card card = new Card (cardID, name, suit, value);
cards[arrayID] = card;
arrayID++;
}
}
}
}
public void printDeck(){
System.out.println(Arrays.toString(cards));
}
}
I may be doing this whole thing wrong, so are there any other ways I could assign a unique ID to the card without using a switch statement?
Upvotes: 1
Views: 1219
Reputation: 2580
What I would do is instead:
Create a class card:
class Card {
private String suit;
private String name;
private int ID;
public Card(String suit, String name, int ID) {
this.suit = suit
(same for name & ID)}}
Then, in your Deck class, you can just have a ArrayList that contains all your card, with some method to add the cards in your deck:
s
class Deck {
private ArrayList<Cards> myDeck = new ArrayList<Cards>();
public void addCards(Card myCard) {
this.myDeck.add(myCard); } }
EDIT: Forgot the variable type in addCards
Upvotes: 0
Reputation: 35011
You don't need the loop or the switch statement for the cardID. You can synthesize it from i
and j
for (int i=1; i<=4; i++){ //number of suites
for (int j=1; j <= 13; j++){ //number of card types
cardId = (i - 1) * 13 + j; // <<< synthesize cardID like this
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 11; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
Card card = new Card (cardID, name, suit, value);
cards[arrayID] = card;
arrayID++;
}
}
Upvotes: 0
Reputation: 37023
You could use enum and you could use ordinal value to get each count automatically and you could have property like value in your case and you could do it like:
public enum Cards {
ACE(11), TWO(..), .. JACK(....;
int value;
public int getValue() {return value;}
}
System.out.println(CARDS.ACE.ordinal() + 1);
System.out.println(CARDS.ACE.getValue());
Output:
1
11
Upvotes: 0
Reputation: 393761
There's no point in this switch statement :
switch (k){
case k: cardID=k; break; //"Case expressions must be constant expressions"
}
Just write :
cardID = k;
Upvotes: 3