/
FailCaffe.java
307 lines (244 loc) · 7.25 KB
/
FailCaffe.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
<<<<<<< HEAD
=======
/* AUTHOR: Nicola Lamonaca
*
* DATE: Feb. 26, 2014
*
* DESCRIPTION: 'FailCaffè' is a game designed by Francesco "Ciccio" Granato.
* The goal of 'FailCaffè' is to decide who, among n people, will brew coffee.
* Each player has 3 dices: a tetrahedron (4 faces), an octahedron (8 faces) and a dodecahedron (12 faces) and 3 shots.
* During each shot, the 3 dices are rolled and the player can choose which of the 3 dices to keep, possibly none.
* When a dice is chosen, its value is added to the player's points and the dice cannot be kept again in the next roll.
* The final player's score is given by the sum of all the kept dices' values.
* In the final roll, all the remaining values are added to the player's points anyway.
* Dices names are {ID12, ID8, ID4, NONE}. If names are written in lowercase, an exception will be thrown.
*
* SCREENSHOT: https://imageshack.com/i/nmbllfp
*
* DOWNLOAD: https://db.tt/NQrNWliF (Requires Java installed on your machine: https://www.java.com/en/download/.)
*/
import java.util.EnumSet;
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
import java.util.Random;
import java.util.Scanner;
public class FailCaffe
{
<<<<<<< HEAD
public int randInt(Moka id)
=======
private int randInt(Moka part)
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
{
Random rand = new Random();
int randomNumb = -1;
<<<<<<< HEAD
switch(id)
{
case ID12: randomNumb = rand.nextInt(12) + 1;
break;
case ID8: randomNumb = rand.nextInt(8) + 1;
break;
=======
switch(part)
{
case ID12: randomNumb = rand.nextInt(12) + 1;
break;
case ID8: randomNumb = rand.nextInt(8) + 1;
break;
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
case ID4: randomNumb = rand.nextInt(4) + 1;
default: break;
}
<<<<<<< HEAD
//System.out.println("randomNumb: " + randomNumb);
//System.out.println("id: " + id);
=======
//DEBUG INFO
//System.out.println("RANDOMNUMB: " + randomNumb);
//System.out.println("ID: " + id);
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
return randomNumb;
}
<<<<<<< HEAD
public static int findSmallest(int[] array)
=======
// Find the smallest integer value within an integer array
private static int findSmallest(int[] array)
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
{
int smallest = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] < smallest)
{
smallest = array[i];
}
}
return smallest;
}
<<<<<<< HEAD
public static void main(String[] args)
{
final int trials = 3;
System.out.print("Please enter the number of players: ");
Scanner scanner = new Scanner(System.in);
int numPlayers = scanner.nextInt();
System.out.println("");
Player players[] = new Player[numPlayers];
int[] points = new int[numPlayers];
=======
// Sum all the the elemnts in an integer array
private int countPoints(int[] array)
{
int points = 0;
for(int i = 0; i < array.length; i++)
{
points += array[i];
}
return points;
}
public static void main(String args[])
{
final int trials = 3;
FailCaffe fc = new FailCaffe();
Dice dice = new Dice();
int value = -1;
// Read the number of players
System.out.print("Please enter the number of players: ");
Scanner scanner = new Scanner(System.in);
int numPlayers = scanner.nextInt();
System.out.println("");
int[] points = new int[numPlayers];
Player players[] = new Player[numPlayers];
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
for(int i = 0; i < players.length; i++)
{
// Creates a new player
players[i] = new Player();
// Asks for his name
System.out.print("Please enter player #" + (i+1) + "'s name: ");
players[i].name = scanner.next();
}
System.out.println("");
<<<<<<< HEAD
FailCaffe fc = new FailCaffe();
for(int i = 0; i < players.length; i++)
{
for(int j = 0; j < trials; j++)
{
for(Moka dice : Moka.values())
{
players[i].points += fc.randInt(dice);
}
}
points[i] = players[i].points;
System.out.println(players[i].name + " totalized " + players[i].points + " points.");
}
int largest = findSmallest(points);
for(int i = 0; i < players.length; i++)
{
if(players[i].points == largest)
=======
// For each player..
for(int i = 0; i < players.length; i++)
{
System.out.println("\nIt's " + players[i].name + "'s turn:");
final EnumSet<Moka> dicesYetToRoll = Moka.VALID_DICES.clone();
// For 'trials' times..
for(int j = 0; j < trials; j++)
{
System.out.println("Roll #" + (j + 1));
// Roll each dice.
for(Moka m : dicesYetToRoll)
{
value = fc.randInt(m);
dice.values[m.ordinal()] = value;
System.out.println("- " + m + ": " + value); // Print the dice name and value
}
try
{
Moka moka;
if(dicesYetToRoll.size() >= 1)
{
//DEBUG INFO
//System.out.println("j = " + j);
if(j == (trials - 1))
{
//DEBUG INFO
//System.out.println("COUNTPOINTS(DICE.VALUES): " + fc.countPoints(dice.values));
players[i].partialPoints += fc.countPoints(dice.values);
System.out.println("Partial result: " + players[i].partialPoints + " points.\n");
break;
}
System.out.print("Which dice do you want to keep? {ID12, ID8, ID4, NONE}: ");
String next = scanner.next();
moka = Moka.valueOf(next);
// Check if a dice is to be kept
if(!next.equalsIgnoreCase("NONE"))
{
dicesYetToRoll.remove(moka);
}
players[i].partialPoints += dice.values[moka.ordinal()];
dice.values[moka.ordinal()] = 0;
//DEBUG INFO
//System.out.println("PLAYERS[i].KEPTVALUES[0] " + players[i].keptValues[0]);
//System.out.println("DICESYETTOROLL.SIZE " + dicesYetToRoll.size());
}
//DEBUG INFO
//System.out.println("DICESYETTOROLL: " + dicesYetToRoll);
System.out.println("Partial result: " + players[i].partialPoints + " points.\n");
//DEBUG INFO
//System.out.println("DICE.VALUES: " + dice.values[moka.ordinal()]);
}
catch(IllegalArgumentException e)
{
System.err.println("Invalid Dice. Aborting.");
System.exit(-1);
}
//DEBUG INFO
//System.out.println("PLAYERS[i].NAME: " + players[i].name);
}
points[i] = players[i].partialPoints;
}
for(int i = 0; i < players.length; i++)
{
System.out.println(players[i].name + " totalized " + players[i].partialPoints + " points.");
}
int smallest = findSmallest(points);
for(int i = 0; i < players.length; i++)
{
if(players[i].partialPoints == smallest)
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
{
System.out.println("\n" + players[i].name + " lost. Coffee, pleeease!");
break;
}
}
}
}
<<<<<<< HEAD
enum Moka
{
ID12, ID8, ID4
}
class Player
{
int points;
=======
enum Moka
{
ID12, ID8, ID4, NONE;
public static final EnumSet<Moka> VALID_DICES = EnumSet.range(ID12, ID4);
public static final EnumSet<Moka> DICES = EnumSet.allOf(Moka.class);
}
class Dice
{
Moka moka;
int[] values = new int[Moka.values().length];
}
class Player
{
int partialPoints;
>>>>>>> 3536a42bbe4ca9ea3c76dc93f34e1d5300147b6a
String name;
}