I'm to creating a blackjack game and i'm trying to figure out how to deal the card and have it display the value and the name. The reason I went with two properties was purely logic based when it comes to dealing with the Ace as being a 1 or 11. Haven't gotten to that point yet just figured i'd include that if anyone was wondering. here is what I have so far. I appreciate any help! I included the error i'm getting below as a comment
import java.util.Random;
public class CardDeck {
public enum Cards {
ACE_SPADES (1, 11),
ACE_HEARTS (1, 11),
ACE_CLOVES (1, 11),
ACE_DIAMONDS (1, 11),
TWO_SPADES (2, 2),
TWO_HEARTS (2, 2),
TWO_CLOVES (2, 2),
TWO_DIAMONDS (2, 2),
THREE_SPADES (3, 3),
THREE_HEARTS (3, 3),
THREE_CLOVES (3, 3),
THREE_DIAMONDS (3, 3),
FOUR_SPADES (4, 4),
FOUR_HEARTS (4, 4),
FOUR_CLOVES (4, 4),
FOUR_DIAMONDS (4, 4),
FIVE_SPADES (5, 5),
FIVE_HEARTS (5, 5),
FIVE_CLOVES (5, 5),
FIVE_DIAMONDS (5, 5),
SIX_SPADES (6, 6),
SIX_HEARTS (6, 6),
SIX_CLOVES (6, 6),
SIX_DIAMONDS (6, 6),
SEVEN_SPADES (7, 7),
SEVEN_HEARTS (7, 7),
SEVEN_CLOVES (7, 7),
SEVEN_DIAMONDS (7, 7),
EIGHT_SPADES (8, 8),
EIGHT_HEARTS (8, 8),
EIGHT_CLOVES (8, 8),
EIGHT_DIAMONDS (8, 8),
NINE_SPADES (9, 9),
NINE_HEARTS (9, 9),
NINE_CLOVES (9, 9),
NINE_DIAMONDS (9, 9),
TEN_SPADES (10, 10),
TEN_HEARTS (10, 10),
TEN_CLOVES (10, 10),
TEN_DIAMONDS (10, 10),
JACK_SPADES (10, 10),
JACK_HEARTS (10, 10),
JACK_CLOVES (10, 10),
JACK_DIAMONDS (10, 10),
QUEEN_SPADES (10, 10),
QUEEN_HEARTS (10, 10),
QUEEN_CLOVES (10, 10),
QUEEN_DIAMONDS (10, 10),
KING_SPADES (10, 10),
KING_HEARTS (10, 10),
KING_CLOVES (10, 10),
KING_DIAMONDS (10, 10);
public final int faceValue1;
public final int faceValue2;
private Cards(int faceValue1, int faceValue2) {
this.faceValue1 = faceValue1;
this.faceValue2 = faceValue2;
}
public String toString() { // error: non-static method name() cannot
be referenced from a static context
return Cards.name();
}
public int getFaceValue1() {
return faceValue1;
}
public int getFaceValue2() {
return faceValue2;
}
}
Cards[] deck;
public CardDeck() {
deck = new Cards[52];
int i = 0;
while(i<52) {
for(Cards card : Cards.values()) {
deck[i] = card;
i++;
}
}
shuffle(deck);
}
//This will return the first card after shuffling.
public Cards deal() {
Cards[] cardReturn = new Cards[1];
cardReturn[0] = deck[0];
Cards[] tempArr = new Cards[deck.length - 1];
for(int i=1; i<deck.length; i++) {
tempArr[i-1] = deck[i];
}
deck = new Cards[tempArr.length];
deck = tempArr;
return cardReturn[0];
}
//this shuffles the deck
public void shuffle(Cards[] deckToShuffle) {
int index;
Cards[] temp = new Cards[1];
Random random = new Random();
for (int i = deckToShuffle.length - 1; i > 0; i--) {
index = random.nextInt(i + 1);
temp[0] = deckToShuffle[index];
deckToShuffle[index] = deckToShuffle[i];
deckToShuffle[i] = temp[0];
}
deck = deckToShuffle;
}
}
Simple: don't call
Cards.name()
But simply
name()
To get your return value.
Beyond that: you are over-doing. You want two enums, one for card value, one for card suite. And then you have a card class that has two fields to hold value and suit.
Having all values times 4 as of now in one enum will make your code much more complicated than it ought to be.
There is also no point in making the value fields both public and having a getter method. Better make the fields private.
Finally, I don't like the naming of just calling them face value 1 and 2. But I don't have a better idea right now.