Im not extremely high level at this and this is my first time really working with objects and classes. I am trying to make a card game and want to color the card's suit name either red or black with the Java color code things. Each card is its own object, with a suit and a number value. Heres the "Card" class:
public class Card {
String suit = Main.generateCardSuit();
int value = Main.generateCardValue();
**if(suit.equals("Spades") || suit.equals("Clubs")){
String cardColor = Colors.WHITE;
} else {
String cardColor = Colors.RED;
}**
String cardDisplay = value ", " cardColor suit Colors.RESET;}
the methods in the Main class that determine the suit and values:
` public static String generateCardSuit() {
String cardSuit = "0";
int suitDeterminer = (int) Math.ceil(Math.random() * 4);
switch (suitDeterminer) {
case 1:
cardSuit = "Spades";
break;
case 2:
cardSuit = "Clubs";
break;
case 3:
cardSuit = "Hearts";
break;
case 4:
cardSuit = "Diamonds";
break;
}
return cardSuit;
}
public static int generateCardValue() {
int gameValue = (int) Math.ceil(Math.random() * 13 1);
return gameValue;
}`
How the Card class is used:
public static void printUserHand(ArrayList < Card > userHand) {
for (int i = 0; i < userHand.size(); i ) {
System.out.println(i ": " userHand.get(i).cardDisplay);
}
}
public static void main(String[] args) {
ArrayList < Card > userHand = new ArrayList < Card > ();
for (int i = 0; i < 7; i ) {
userHand.add(new Card());
}
for (int i = 7; i > 0; i--) {
Card gameCard = new Card();
System.out.println("The dealer turns up a: " gameCard.cardDisplay "\n");
...
So i need each cards color to be an attribute, but the bolded IF statement I have in the object doesnt work. Based on how my code is working, I dont know of a way that it could go in the Main class without causing a lot of other problems.
CodePudding user response:
Your Card
class has a severe problem: it is not a class at all, it tries to execute code outside a method.
Give it a private field called cardDisplay
and initialize it in a constructor of Card
. Add a method to retrieve the value of cardDisplay:
private final String cardDisplay;
public Card() {
// put all the code from your version of Card here
this.cardDisplay = cardDisplay;
}
public String getCardDisplay() {
return cardDisplay;
}
If you skip the declaration in your code (that's where you specify the type of the local variable), you can even save a line with
cardDisplay = value ", " cardColor suit Colors.RESET;
Just don't skip the declaration for real local variables that are not available as fields, like probably cardColor
.
CodePudding user response:
There is a data type named Color, you might want to try setting "Color cardColor = Color.yourColor" and then using this value directly as your color with cardColor.
EDIT to include solution:
if(suit.equals("Spades") || suit.equals("Clubs")){
Color cardColor = Colors.WHITE;
} else {
Color cardColor = Colors.RED;
}
You might want to give this list a look, lot of other ressources there if you are starting out: https://docs.oracle.com/javase/7/docs/api/overview-summary.html
CodePudding user response:
You mentioned it was this code that wasn't working:
**if(suit.equals("Spades") || suit.equals("Clubs")){
String cardColor = Colors.WHITE;
} else {
String cardColor = Colors.RED;
}**
This code is creating a variable called color
inside each block, but then the variable is discarded without anything being done with it. If your program compiles, even though you have the code:
String cardDisplay = value ", " cardColor suit Colors.RESET;
Then it means the variable called cardColor
that that line is accessing is not the variable you assigned a color to conditionally in the code with the if statement. The solution to the problem you're encountering right now is to identify what variable called cardColor
is being accessed on the line with String cardDisplay =
and ensure that that variable is the one being modified depending on the suit in your if statement blocks.
CodePudding user response:
You need to think more object-oriented about it:
First create some enums, I would suggest a Suit and FaceValue enum:
public enum Suit {
SPADE(Color.BLACK, "Spades"),
CLUB(Color.BLACK, "Clubs"),
HEART(Color.RED, "Hearts"),
DIAMOND(Color.RED, "Diamonds");
private Color color;
private String displayValue;
public Color getColor() {
return color;
}
private Suit(Color color, String displayValue) {
this.color = color;
this.displayValue = displayValue;
}
public String toString() {
return displayValue;
}
}
public enum FaceValue {
TWO("2"),
THREE("3"),
FOUR("4"),
FIVE("5"),
SIX("6"),
SEVEN("7"),
EIGHT("8"),
NINE("9"),
TEN("10"),
JACK("J"),
QUEEN("Q"),
KING("K"),
ACE("A");
private String displayValue;
private FaceValue(String displayValue) {
this.displayValue = displayValue;
}
public String toString() {
return displayValue;
}
}
Then a card class:
public class Card {
private Suit suit;
private FaceValue value;
public Card(Suit suit, FaceValue value) {
this.suit = suit;
this.value = value;
}
public Suit getSuit() {
return suit;
}
public FaceValue getValue() {
return value;
}
public String toString() {
return value " of " suit;
}
}
Then you can create a deck class with basic functionality.
public class Deck {
private List<Card> cards = new LinkedList<Card>();
public Deck() {
init();
shuffle();
}
public void init() {
cards.clear();
for (Suit suit : Suit.values()) {
for (FaceValue faceValue : FaceValue.values()) {
cards.add(new Card(suit, faceValue));
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card drawCard() {
return cards.remove(0);
}
public boolean hasCards() {
return !cards.isEmpty();
}
public static void main(String [] args) {
Deck deck = new Deck();
while (deck.hasCards()) {
System.out.println(deck.drawCard());
}
}
}
From there you can create games and such. A player could have a Hand of Cards. Then it's all just a matter of adding rules.