-1

So i'm making an uno game where the user can ony play a card that has the same value or the same color as the card in the stock pile I currently have this as my code:

if(topPileCard().getColor()==card.getColor() || topPileCard().getValue() == card.getValue())

but if a user, for example, plays a green card the condition turns true only if the color matches and the value no longer works and vice versa Is there any way I can get around this?

and here is the code with uno cards class that has the getValue() and getColor() methods

public class UnoCard {
enum Color {
    RED, BLUE, GREEN, YELLOW,BLACK;

    private static final Color[] colors = Color.values();
    public static Color getColor(int i){
        return Color.colors[i];
    }
}

    enum Value {
        ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, SKIP, DRAW_TWO, REVERSE, WILD, WILD_FOUR;
        private static final Value[] values = Value.values();
        public static Value getValue(int i){
            return Value.values[i];
        }
}

private final Color color;
private final Value value;

public UnoCard(final Color color, final Value value){
    this.color = color;
    this.value = value;
}

public Color getColor(){
    return this.color;
}

public Value getValue(){
    return this.value;
}

@Override
public String toString() {
    return color + "_" + value;
}

}

Braiam
  • 1
  • 11
  • 47
  • 78
Joee
  • 57
  • 7

1 Answers1

0

I suggest using && instead of ||, || means "or" so neither of those two would be pick as true. && means "and", both of those conditions should meet before stating as true.

JayLord Abueva
  • 393
  • 4
  • 6
  • though i only need one condition to bet met for it to work either the color or the value matches – Joee May 13 '20 at 04:35
  • Try this: boolean isAlreadyCorrect = false; if(topPileCard().getColor()==card.getColor()){ //Code here isAlreadyCorrect = true; } if(topPileCard().getValue()==card.getValue()&&isAlreadyCorrect==false){ //Code here } – JayLord Abueva May 13 '20 at 04:42
  • This would kinda messy, but it works better than "||" – JayLord Abueva May 13 '20 at 04:42
  • or i think the problem is the strings you compare.... try using ".contains" instead of "==" ex: topPileCard().getValue().toLowerCase().contains(card.getValue().toLowerCase()) – JayLord Abueva May 13 '20 at 04:46