-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.java
More file actions
175 lines (146 loc) · 6.38 KB
/
GameLogic.java
File metadata and controls
175 lines (146 loc) · 6.38 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GameLogic {
private static final int THRESHOLD = 4;
private static ArrayList<Card> getCardsBelowThreshold(List<Card> cards, int threshold) {
ArrayList<Card> result = new ArrayList<>();
for (Card card : cards) {
if (card.getNumber() < threshold) {
result.add(new Card(card.getColor(), card.getNumber()));
}
}
return result;
}
private static Card getMaxCardFromList(ArrayList<Card> cards) {
if (cards.isEmpty()) {
return new Card("Nothing", 0);
}
Card maxCard = cards.get(0);
for (Card card : cards) {
if (isCardHigher(card, maxCard)) {
maxCard = card;
}
}
return new Card(maxCard.getColor(), maxCard.getNumber());
}
private static boolean isCardHigher(Card card1, Card card2) {
if (card1.getNumber() > card2.getNumber()) {
return true;
}
if (card1.getNumber() == card2.getNumber()) {
return isColorHigherPriority(card1.getColor(), card2.getColor());
}
return false;
}
private static ColorSet getColorSet(ArrayList<Card> cards) {
HashMap<String, ArrayList<Integer>> colorMap = createColorMap(cards);
return findBestColorSet(colorMap);
}
private static HashMap<String, ArrayList<Integer>> createColorMap(ArrayList<Card> cards) {
HashMap<String, ArrayList<Integer>> colorMap = new HashMap<>();
for (Card card : cards) {
colorMap.computeIfAbsent(card.getColor(), k -> new ArrayList<>()).add(card.getNumber());
}
return colorMap;
}
private static ColorSet findBestColorSet(HashMap<String, ArrayList<Integer>> colorMap) {
String[] colorOrder = {"Red", "Yellow", "Violet"};
String bestColor = "Red";
ArrayList<Integer> bestList = colorMap.getOrDefault("Red", new ArrayList<>());
int bestMax = getMaxFromList(bestList);
for (String color : colorOrder) {
ArrayList<Integer> currentList = colorMap.getOrDefault(color, new ArrayList<>());
int currentMax = getMaxFromList(currentList);
if (isColorSetBetter(currentList, currentMax, bestList, bestMax)) {
bestList = currentList;
bestColor = color;
bestMax = currentMax;
}
}
return new ColorSet(bestColor, bestList.size(), bestMax);
}
private static boolean isColorSetBetter(ArrayList<Integer> currentList, int currentMax,
ArrayList<Integer> bestList, int bestMax) {
if (currentList.size() > bestList.size()) {
return true;
}
return currentList.size() == bestList.size() && currentMax > bestMax;
}
private static int getMaxFromList(ArrayList<Integer> list) {
int max = 0;
for (int num : list) {
max = Math.max(max, num);
}
return max;
}
private static boolean isColorHigherPriority(String c1, String c2) {
if (c1.equals("Red")) return true;
return c1.equals("Yellow") && c2.equals("Violet");
}
private static boolean compareCardsByNumberThenColor(Card playerCard, Card opponentCard) {
if (playerCard == null && opponentCard == null) {
return false;
}
if (playerCard == null) {
return false;
}
if (opponentCard == null) {
return true;
}
if (playerCard.getNumber() > opponentCard.getNumber()) {
return true;
}
if (playerCard.getNumber() == opponentCard.getNumber()) {
return isColorHigherPriority(playerCard.getColor(), opponentCard.getColor());
}
return false;
}
private static boolean compareValuesAndThenColor(int playerValue, int opponentValue, String playerColor, String opponentColor) {
if (playerValue > opponentValue) {
return true;
}
if (playerValue == opponentValue) {
return isColorHigherPriority(playerColor, opponentColor);
}
return false;
}
public static boolean playerWinning(String canvasColor, ArrayList<Card> playerPalette, ArrayList<Card> opponentPalette) {
switch (canvasColor) {
case "Red":
return checkRedRule(playerPalette, opponentPalette);
case "Yellow":
return checkYellowRule(playerPalette, opponentPalette);
default:
return checkVioletRule(playerPalette, opponentPalette);
}
}
private static boolean checkRedRule(ArrayList<Card> playerPalette, ArrayList<Card> opponentPalette) {
Card maxPlayer = getMaxCardFromList(playerPalette);
Card maxOpponent = getMaxCardFromList(opponentPalette);
System.out.println("Player's max card: " + maxPlayer.getColor() + " " + maxPlayer.getNumber());
System.out.println("Opponent's max card: " + maxOpponent.getColor() + " " + maxOpponent.getNumber());
return compareCardsByNumberThenColor(maxPlayer, maxOpponent);
}
private static boolean checkYellowRule(ArrayList<Card> playerPalette, ArrayList<Card> opponentPalette) {
ColorSet playerSet = getColorSet(playerPalette);
ColorSet opponentSet = getColorSet(opponentPalette);
if (playerSet.getCount() != opponentSet.getCount()) {
return playerSet.getCount() > opponentSet.getCount();
}
return compareValuesAndThenColor(
playerSet.getMaxNumber(), opponentSet.getMaxNumber(),
playerSet.getColor(), opponentSet.getColor()
);
}
private static boolean checkVioletRule(ArrayList<Card> playerPalette, ArrayList<Card> opponentPalette) {
ArrayList<Card> playerValidCards = getCardsBelowThreshold(playerPalette, THRESHOLD);
ArrayList<Card> opponentValidCards = getCardsBelowThreshold(opponentPalette, THRESHOLD);
if (playerValidCards.size() != opponentValidCards.size()) {
return playerValidCards.size() > opponentValidCards.size();
}
Card maxPlayer = getMaxCardFromList(playerValidCards);
Card maxOpponent = getMaxCardFromList(opponentValidCards);
return compareCardsByNumberThenColor(maxPlayer, maxOpponent);
}
}