-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSushiGo.java
322 lines (283 loc) · 10.9 KB
/
SushiGo.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import java.util.Scanner;
public class SushiGo {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
//Create our deck for the game
Deck tableDeck = new Deck();
System.out.println(startGame());
//Run the game loop
boolean playAgain = true;
while (playAgain) {
playGame(reader, tableDeck);
playAgain = askPlayAgain(reader);
}
}
public static void playGame(Scanner reader, Deck tableDeck) {
Player[] players = createPlayers(reader);
tableDeck.shuffle();
tableDeck.dealCards(players);
runGameLoop(players, reader);
}
public static boolean askPlayAgain(Scanner reader) {
System.out.println("Game Over, Want to play again? (Y/N)");
String response = reader.nextLine();
if (!response.equalsIgnoreCase("Y")) {
System.out.println("Goodbye!");
return false;
}
return true;
}
public static Player[] createPlayers(Scanner reader){
int numOfPlayers = getNumOfPlayers(reader);
return buildPlayers(numOfPlayers);
}
public static int getNumOfPlayers(Scanner reader) {
int numOfPlayers = 0;
boolean validInput = false;
do {
System.out.println("How many players? (Minimum 2, Maximum 5) ");
try {
numOfPlayers = Integer.parseInt(reader.nextLine());
if(numOfPlayers >= 2 && numOfPlayers <= 5) {
validInput = true;
} else {
System.out.println("\u001B[31mPlease enter a number between 2 and 5.\u001B[0m");
}
} catch (NumberFormatException e) {
System.out.println("\u001B[31mINVALID INPUT. PLEASE ENTER AN INTEGER\u001B[0m");
}
} while (!validInput);
return numOfPlayers;
}
public static Player[] buildPlayers(int numOfPlayers) {
Player[] players = new Player[numOfPlayers];
for(int i = 0; i < numOfPlayers; i++){
players[i] = new Player(i, numOfPlayers);
}
return players;
}
public static void runGameLoop(Player[] players, Scanner reader){
int rounds = 0;
while (rounds < 3) {
for(int ID = 0; ID < players.length; ID++){
int cardIndex = 0;
System.out.println(printPlayerTurn(ID, players[ID]));
boolean validInput = false;
do {
System.out.print(players[ID].printPlayerHand() + "\u001B[0m\n\u001B[34mSelect a card to play:\u001B[0m");
try {
//Offset by 1, to match the index of the card
cardIndex = Integer.parseInt(reader.nextLine())-1;
if(cardIndex >= 0 && cardIndex < players[ID].handLength()){
validInput = true;
} else {
System.out.println("\u001B[31mInvalid card index. Please enter a number between 1 and " + players[ID].handLength() + "\u001B[0m");
}
} catch (NumberFormatException e) {
System.out.println("\u001B[31mINVALID INPUT. PLEASE ENTER AN INTEGER\u001B[0m");
}
} while (!validInput);
players[ID].checkWasabi(cardIndex);
players[ID].manipulateHand(cardIndex);
System.out.println(players[ID].printPlayerHand());
}
moveDecks(players);
rounds++;
}
System.out.println(calculateWinner(players));
}
//Do calculation and determine winner
public static String calculateWinner(Player[] players) {
String builder = "";
builder += "Calculating points...\n";
int maxPoints = 0;
int winnerIndex = 0;
for (int i = 0; i < players.length; i++) {
int points = calculatePointsPerPlayer(players, i);
builder += "Player " + (i + 1) + " has " + points + " points\n";
if (points > maxPoints) {
maxPoints = points;
winnerIndex = i;
}
}
builder += "\u001B[32mPlayer " + (winnerIndex + 1) + " wins with " + maxPoints + " points!\u001B[0m";
return builder;
}
public static void moveDecks(Player[] players) {
// Array to hold the deep copies of the players' hands
DynamicCardArray[] copiedHands = new DynamicCardArray[players.length];
// Deep copy the players' hands
for (int i = 0; i < players.length; i++) {
copiedHands[i] = players[i].getHand();
}
// Shift the hands to the next player
for (int i = 0; i < players.length; i++) {
if (i == players.length - 1) {
players[0].setHand(copiedHands[i]);
} else {
players[i + 1].setHand(copiedHands[i]);
}
}
}
public static String startGame(){
return "\u001B[32m" + "WELCOME TO SUSHI GO" + "\u001B[0m";
}
public static String printPlayerTurn(int ID, Player player){
String builder = "";
//Offset by 1, to match the player ID, not the index
ID++;
builder+="\u001B[36m*****************************************" + "\n" +
player + "\u001B[36m\n*****************************************\u001B[0m";
return builder;
}
//Order: countSushiRoll, countTempura, countSashimi, countDumpling,
// countSalmonNigiri, countSquidNigiri, countEggNigiri, countPudding
public static int calculatePointsPerPlayer(Player[] players, int playerIndex){
int points = 0;
int[] cards = players[playerIndex].countCardsPerPlayer();
// Sushi Rolls
points += calculateSushiRollPoints(players, playerIndex);
// Tempura
points += calcTempura(cards);
// Sashimi
points += calcSashimiPoints(cards);
// Dumpling
points += calcDumpling(cards);
// Salmon Nigiri
points += cards[4];
// Squid Nigiri
points += cards[5];
// Egg Nigiri
points += cards[6];
// Pudding
points += calculatePuddingPoints(players, playerIndex);
return points;
}
public static int calcSashimiPoints(int[] cards){
int points = 0;
if (cards[2] >= 3) {
int setsOfThree = cards[2] / 3;
points += setsOfThree * 10;
}
return points;
}
public static int calcTempura(int[] cards){
int points = 0;
if (cards[1] >= 2) {
int setsOfTwo = cards[1] / 2;
points += setsOfTwo * 5;
}
return points;
}
public static int calcDumpling(int[] cards){
int points = 0;
switch (cards[3]) {
case 1:
points += 1;
break;
case 2:
points += 3;
break;
case 3:
points += 6;
break;
case 4:
points += 10;
break;
case 5:
points += 15;
break;
}
return points;
}
public static int calculateSushiRollPoints(Player[] players, int playerIndex) {
int maxSushiRollCards = 0;
int secSushiRollCards = 0;
boolean hasMax = false;
boolean hasSec = false;
boolean isTie = false;
int[] otherPlayerCards = new int[Type.values().length - 3]; // -3 because we are not counting all 3 sushi rolls and wasabi
int[] allSushiRollCards = new int[players.length];
for (int i = 0; i < players.length; i++) {
otherPlayerCards = players[i].countCardsPerPlayer();
allSushiRollCards[i] = otherPlayerCards[0];
if (allSushiRollCards[i] > maxSushiRollCards) {
secSushiRollCards = maxSushiRollCards;
maxSushiRollCards = allSushiRollCards[i];
} else if (allSushiRollCards[i] > secSushiRollCards) {
secSushiRollCards = allSushiRollCards[i];
}
}
int playerSushiRollCards = players[playerIndex].countCardsPerPlayer()[0];
if (playerSushiRollCards == 0)
return 0;
if (playerSushiRollCards == maxSushiRollCards)
hasMax = true;
if (playerSushiRollCards == secSushiRollCards)
hasSec = true;
int countMax = 0;
int countSec = 0;
for (int i = 0; i < allSushiRollCards.length; i++) {
if (maxSushiRollCards == allSushiRollCards[i])
countMax++;
if(secSushiRollCards == allSushiRollCards[i])
countSec++;
}
if (countMax > 1)
isTie = true;
if (countSec > 1)
isTie = true;
int points = 0;
if (hasMax) {
points += 6;
} else if (hasSec) {
points += 3;
}
if ((isTie && hasSec) || (isTie && hasMax)) // if tie, everyone gets half points
points /= (countMax > 1) ? countMax : (countSec > 1) ? countSec : 1;
return points;
}
public static int calculatePuddingPoints(Player[] players, int playerIndex) {
int maxPuddingCards = 0;
int minPuddingCards = Type.PUDDING.getCount(); //max amount of pudding cards, ie 10
boolean hasMax = false;
boolean hasMin = false;
boolean isTie = false;
int[] otherPlayerCards = new int[Type.values().length - 3];
int[] allPuddingCards = new int[players.length];
for (int i = 0; i < players.length; i++) {
otherPlayerCards = players[i].countCardsPerPlayer();
allPuddingCards[i] = otherPlayerCards[7];
if (allPuddingCards[i] > maxPuddingCards)
maxPuddingCards = allPuddingCards[i];
if (allPuddingCards[i] < minPuddingCards)
minPuddingCards = allPuddingCards[i];
}
int playerPuddingCards = players[playerIndex].countCardsPerPlayer()[7];
if (playerPuddingCards == maxPuddingCards)
hasMax = true;
if (playerPuddingCards == minPuddingCards)
hasMin = true;
int countMax = 0;
int countMin = 0;
for (int i = 0; i < allPuddingCards.length; i++) {
if (maxPuddingCards == allPuddingCards[i])
countMax++;
if (minPuddingCards == allPuddingCards[i])
countMin++;
}
if (countMax > 1)
isTie = true;
if (countMin > 1)
isTie = true;
int points = 0;
if (hasMax) {
points += 6;
} else if (hasMin && players.length > 2){
points -= 6;
}
if ((isTie && hasMin) || (isTie && hasMax)) //if theres a tie, everyone gets half the points
points /= (countMax > 1) ? countMax : (countMin > 1) ? countMin : 1;
return points;
}
}