Reputation: 239
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;
}
}
Upvotes: 0
Views: 2363
Reputation: 1454
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ù:
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);
Upvotes: 2
Reputation: 174
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
Upvotes: 1
Reputation: 4169
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
Upvotes: 0