-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetGame.java
126 lines (109 loc) · 3.26 KB
/
SetGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.*;
public class SetGame {
private ArrayList<Card> cards = new ArrayList<Card>();
private ArrayList<Card[]> validSets = new ArrayList<Card[]>();
public void addCards(Card[] inputCards){
for(int i=0; i<inputCards.length; i++){
cards.add(inputCards[i]);
}
}
public ArrayList<Card> getCards(){
return cards;
}
public void findSets(){
//Find all possible sets (n choose 3 combinations)
for(int i=0; i<cards.size(); i++){
for(int j=i+1; j<cards.size(); j++){
for(int k=j+1; k<cards.size(); k++){
if(isSetValid(cards.get(i), cards.get(j), cards.get(k))){
validSets.add(new Card[]{cards.get(i), cards.get(j), cards.get(k)});
}
}
}
}
if(validSets.isEmpty()){
System.out.println("No Valid Sets. Must Draw more cards.");
}
else{
System.out.println("Valid Sets");
for(Card[] set : validSets){
for(Card card : set){
System.out.print(card +" ");
}
System.out.println();
}
}
}
//Valid sets must either have all different numbers, shapes & colors, or all the same
private boolean isSetValid(Card card1, Card card2, Card card3){
if(!areAllEqual(card1.number, card2.number, card3.number) && !areAllDifferent(card1.number, card2.number, card3.number)){
return false;
}
if(!areAllEqual(card1.shape, card2.shape, card3.shape) && !areAllDifferent(card1.shape, card2.shape, card3.shape)){
return false;
}
if(!areAllEqual(card1.shading, card2.shading, card3.shading) && !areAllDifferent(card1.shading, card2.shading, card3.shading)){
return false;
}
if(!areAllEqual(card1.color, card2.color, card3.color) && !areAllDifferent(card1.color, card2.color, card3.color)){
return false;
}
return true;
}
private boolean areAllEqual(int num1, int num2, int num3){
return num1==num2 && num2==num3;
}
private boolean areAllDifferent(int num1, int num2, int num3){
return num1 != num2 && num2 != num3 && num1 != num3;
}
//Iterative implementation of n choose 3 for combinations
public void printCombinations(ArrayList<Card> cards){
int combinationCount=0;
for(int i=0; i<cards.size(); i++){
for(int j=i+1; j<cards.size(); j++){
for(int k=j+1; k<cards.size(); k++){
System.out.println(cards.get(i)+", " + cards.get(j) +", " + cards.get(k));
combinationCount++;
}
}
}
System.out.println("Combination Count = " + combinationCount);
}
public static void main(String[] args) {
SetGame game = new SetGame();
game.addCards(new Card[] {
new Card(1, 2, 2, 3),
new Card(2, 1, 2, 2),
new Card(3, 3, 2, 1),
new Card(2, 2, 2, 1),
new Card(2, 2, 2, 1),
new Card(2, 2, 2, 1),
}
);
System.out.println("Available Cards");
for(Card card : game.getCards()){
System.out.println(card);
}
System.out.println("\nAll Combinations");
game.printCombinations(game.getCards());
System.out.println();
game.findSets();
}
}
class Card{
//Internal representation of cards are integers for ease of calculation
public int number;
public int shape;
public int shading;
public int color;
public Card(int number, int shape, int shading, int color){
this.number=number;
this.shape=shape;
this.shading=shading;
this.color=color;
}
@Override
public String toString(){
return "[" + number + " " + shape + " " + shading + " " + color + "]";
}
}