0

logic is of setgame

boolean isset = false;

three card array each with 4 different propert

String[] arraycard1 = {"1","open","red","diamond"};
String[] arraycard2 ={"1","solid","green","diamond"};
String[] arraycard3 ={"1","open","purple","oval"};

a set is any combination of three cards in which each property is the same on all three cards, or different on all three cards

I need to check If this set of 3 cards is set or not.

for (int i=0 ; i<arraycard1.length ; i++){

checking if properties are all same or all different

      if ((arraycard1[i].equalsIgnoreCase(arraycard2[i]) && arraycard1[i].equalsIgnoreCase(arraycard3[i]) && arraycard2[i].equalsIgnoreCase(arraycard3[i]))||(arraycard1[i]!=arraycard2[i] && arraycard1[i]!=arraycard3[i] && arraycard2[i]!=arraycard3[i])){
              isset = true;
      }
}
1
  • 1
    So out of the 16 elements. if one element is different then its "all different"? or because 3 of the 4 elements in one array is the same, is it "the same"? Please explain more in detail what you want to achieve. Commented Sep 9, 2014 at 6:24

3 Answers 3

2

Why not use a class and override the equals method?

import java.awt.Color;

public class Card{
    int number;
    String state;
    Color color;
    String suit;

    public Card(int number, String state, Color color, String suit) {
        super();
        this.number = number;
        this.state = state;
        this.color = color;
        this.suit = suit;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((color == null) ? 0 : color.hashCode());
        result = prime * result + number;
        result = prime * result + ((state == null) ? 0 : state.hashCode());
        result = prime * result + ((suit == null) ? 0 : suit.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Card other = (Card) obj;
        if (color == null) {
            if (other.color != null)
                return false;
        } else if (!color.equals(other.color))
            return false;
        if (number != other.number)
            return false;
        if (state == null) {
            if (other.state != null)
                return false;
        } else if (!state.equals(other.state))
            return false;
        if (suit == null) {
            if (other.suit != null)
                return false;
        } else if (!suit.equals(other.suit))
            return false;
        return true;
    }
}

If you are using Eclipse IDE all of this is generated automatically, you only have to declare the 4 fields.

From the menù:

  • Source -> Generate getters and setters.
  • Source -> Generate constructor using fields.
  • Source -> Generate hashCode() and equals().

Then you can create a Card object:

Card card1 = new Card(1, "open", Color.RED, "diamond");
Card card2 = new Card(1, "solid", Color.GREEN, "diamond");

boolean sameCard = card1.equals(card2); //false

You can also create a CardUtils class which can check some basic combinations:

public class CardUtils{

    public static boolean isPair(Card a, Card b){
        return a.getNumber() == b.getNumber() && //Same number, different suit
               !a.getSuit().equals(b.getSuit());
    }

    public static boolean isFlush(Card.. cards){
        String suit = carsds[0].getSuit();
        for(Card c: cards){
             if(!c.getSuit().equals(suit))
                 return false;
        return true;
    }
    public static boolean isPoker(Card..cards){
         if(cards.lenght!=5) return false;
         for(int i = 0; i < 2; i++){
             int count = 0;
             for(Card c1: cards)
                 if(isPair(cards[i], c1)) count++;
             if(count==3) return true; //There are other 3 cards with same number but different suit in hand -> Poker!
         }
         return false;
    }
}

And use it

Card card1 = new Card(1, "open", Color.RED, "diamond");
Card card2 = new Card(1, "solid", Color.GREEN, "spears");
Card card3 = new Card(2, "solid", Color.RED, "diamond");

CardUtils.isPair(card1, card2); //True
CardUtils.isPair(card1, card3); //False

CardUtils.isFlush(card1, card2, card3); //False
CardUtils.isFlush(card1, card3); //True

Last but not least you can use Enums to deal with the suits:

public enum Suit{
    DIAMONDS,
    HEARTS,
    SPADES,
    CLUBS
}
Card card1 = new Card(1, "open", Color.RED, Suit.DIAMONDS);
Sign up to request clarification or add additional context in comments.

7 Comments

he wrote equalsIgnoreCase.
else if (!suit.equals(other.suit)) -> else if (!suit.equalsIgnoreCase(other.suit)) in the equals overriding (you are overriding, you can put whatever you want there! Eclipse doesn't do it as default tough...)
hashCode and equals gives differnt results now. This will break some logics in HashSet TreeSet LinkedHashSet and HashTable.
You're right, you should modify the hashCode() method accordingly: suit.toLowerCase().hashCode() (if two objects are equal, then they must have the same hash code) which is not a good idea if you want to maintain compatibility with versions. Best way to overcome this problem is using enums.
Thanks Namrmer. I think creating a class Card and passing the card properties is good idea. I still learning java and I get really confused by creating classes and separate methods to perform certain tasks.
|
1

You can use ArrayaList to for quicker comparison.Assign the string array to an Array list and use the .equals () ? Method

Here is an example:

String[] array1 = {"1", "2", "3"};

String[] array2 = {"1", "2", "3"};

String[] array3 = {"1", "2", "3"};

List<List<String>> lst1 = new      ArrayList<>();
lst1.add(Arrays.asList(array1));

List<List<String>> lst2 = new ArrayList<>();
lst2.add(Arrays.asList(array2));

List<List<String>> lst3 = new ArrayList<>();
lst3.add(Arrays.asList(array3));
System.out.println(lst1.equals(lst2) && lst1.equals(lst3));     //prints true

2 Comments

equalsIgnoreCase not equals.
@user2254601 Is List<List<String>> lst1 = new ArrayList<>(); creating list of list?
0

You could try something like this.

public static void main(String[] arraycard1,String[] arraycard2,String[] arraycard3) {
  String[] arraycard1 = {"1","open","red","diamond"};
  String[] arraycard2 ={"1","solid","green","diamond"};
  String[] arraycard3 ={"1","open","purple","oval"};

  System.out.println("Is array 1 equal to array 2?? "
  +Arrays.equals(arraycard1, arraycard2));

  System.out.println("Is array 1 equal to array 3?? "
  +Arrays.equals(arraycard1, arraycard3));

  System.out.println("Is array 2 equal to array 3?? "
  +Arrays.equals(arraycard2, arraycard3));
}

RESULT

Is array 1 equal to array 2?? false
Is array 1 equal to array 3?? false
Is array 2 equal to array 3?? false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.