-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChuckALuck.java
335 lines (307 loc) · 8.05 KB
/
ChuckALuck.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
323
324
325
326
327
328
329
330
331
332
333
334
335
import java.util.Scanner;
public class ChuckALuck {
public static int AMOUNT_OF_DICE = 3;
public static int SLIDES_OF_DICE = 6;
public static int INDEX_START_VALUE = 0;
public static double MIN_WALLET_AMOUNT = 0.0;
public static int TRIPLE_PAYOUT = 30;
public static int STANDARD_PAYOUT =1;
public static int TRIPLE_CASE_NUM =1;
public static int FIELD_CASE_NUM =2;
public static int HIGH_CASE_NUM =3;
public static int LOW_CASE_NUM =4;
public static int MAX_VALUE_LOW = 11;
public static int MIN_VALUE_HIGH = 10;
public static int MAX_VALUE_FIELD = 12;
public static int MIN_VALUE_FIELD = 8;
public static int ALL_ONES_VALUE = 3;
public static int ALL_TWO_VALUE = 6;
public static int ALL_THREE_VALUE = 9;
public static int ALL_FOUR_VALUE = 12;
public static int ALL_FIVE_VALUE = 15;
public static int ALL_SIX_VALUE = 18;
public static void main(String[] args) {
System.out.println("Welcome to Chuck-A-Luck");
boolean isgameOver = false;
Wallet userWallet = new Wallet();
Scanner inputScanner = new Scanner(System.in);
initiateWallet( userWallet, inputScanner);
double userStartingCash = userWallet.check();
while(!isgameOver && userWallet.check() > MIN_WALLET_AMOUNT )
{
inputScanner = new Scanner(System.in);
String betString = null;
System.out.println("What type of bet do you want to place?");
System.out.println("If you want to leave the game, enter Quit");
System.out.print("Low, High, Field or Triple?: ");
betString = generatingBet( inputScanner);
if(betString.equals("Low") || betString.equals("High")
|| betString.equals("Field") || betString.equals("Triple") )
{
resolveBet(betString, userWallet,inputScanner);
}
else if( betString.equals("Quit"))
{
isgameOver = true;
}
else
{
System.out.println( "Sorry, this isn't a valid bet type");
}
}
inputScanner.close();
gameOver(userWallet,userStartingCash );
}
public static void initiateWallet( Wallet userWallet, Scanner walletScanner )
{
boolean walletEmpty = true;
while( walletEmpty)
{
System.out.print("How much cash do you have?: ");
if( walletScanner.hasNextDouble())
{
double usersStartingCash = walletScanner.nextDouble();
userWallet.put(usersStartingCash);
walletEmpty = false;
}
else if(walletScanner.hasNext() )
{
System.out.println("Sorry, this isn't a valid input");
walletScanner = new Scanner(System.in);
}
}
}
private static String generatingBet(Scanner inputScanner) {
String betString =null;
boolean gettingBet = true;
while( gettingBet)
{
if(inputScanner.hasNextLine())
{
betString = inputScanner.nextLine();
gettingBet = false;
}
}
return betString;
}
public static void resolveBet( String betType, Wallet userWallet, Scanner betScanner )
{
Dice[] diceArray = new Dice[AMOUNT_OF_DICE];
int betCase = -1;
int payoutMul = STANDARD_PAYOUT;
int diceTotal = 0;
switch (betType)
{
case "Triple":
payoutMul = TRIPLE_PAYOUT;
betCase = TRIPLE_CASE_NUM;
break;
case "Field":
payoutMul = STANDARD_PAYOUT;
betCase = FIELD_CASE_NUM;
break;
case "Low":
payoutMul = STANDARD_PAYOUT;
betCase = LOW_CASE_NUM;
break;
case "High":
payoutMul = STANDARD_PAYOUT;
betCase = HIGH_CASE_NUM;
break;
default:
break;
}
for( int index =INDEX_START_VALUE; index < diceArray.length; index++)
{
diceArray[index] = new Dice(SLIDES_OF_DICE);
diceArray[index].roll();
diceTotal += diceArray[index].topFace();
}
boolean calculateBettingAmount = true;
double betAmount = -1;
System.out.println( userWallet.toString() );
while(calculateBettingAmount )
{
System.out.print("How much would you like to bet?: ");
if( betScanner.hasNextDouble())
{
betAmount = betScanner.nextDouble();
if( betAmount <= userWallet.check() )
{
calculateBettingAmount = false;
}
else
{
System.out.println("Sorry, You didn't have enough money for this bet.");
betScanner = new Scanner(System.in);
}
}
else if(betScanner.hasNext() )
{
System.out.println("Sorry, this isn't a valid input");
betScanner = new Scanner(System.in);
}
}
if( isBetWon( betCase, diceTotal, diceArray))
{
System.out.println("Congrats, your Bet was correct.");
userWallet.put( payoutMul * betAmount);
}
else
{
System.out.println("Sorry, your Bet was incorrect.");
userWallet.get(betAmount);
}
printDisc(diceArray, diceTotal);
}
public static boolean isBetWon( int betCase, int diceTotal, Dice[] diceArray )
{
boolean betWon =false;
if( betCase == TRIPLE_CASE_NUM)
{
if (diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +1 ].topFace()
&& diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +2 ].topFace() )
{
if(diceTotal == ALL_SIX_VALUE || diceTotal == ALL_ONES_VALUE )
{
betWon = false;
}
else
{
betWon = true;
}
}
else
{
betWon = false;
}
}
else if( betCase == FIELD_CASE_NUM)
{
if(diceTotal < MIN_VALUE_FIELD || diceTotal > MAX_VALUE_FIELD )
{
betWon = true;
}
else
{
betWon = false;
}
}
else if( betCase == HIGH_CASE_NUM)
{
if(diceTotal == ALL_SIX_VALUE )
{
betWon = false;
}
else if (diceTotal == ALL_FIVE_VALUE )
{
if (diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +1 ].topFace()
&& diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +2 ].topFace() )
{
betWon = false;
}
else
{
betWon = true;
}
}
else if (diceTotal == ALL_FOUR_VALUE )
{
if (diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +1 ].topFace()
&& diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +2 ].topFace())
{
betWon = false;
}
else
{
betWon = true;
}
}
else if(diceTotal > MIN_VALUE_HIGH )
{
betWon = true;
}
else
{
betWon = false;
}
}
else if( betCase == LOW_CASE_NUM)
{
if(diceTotal == ALL_ONES_VALUE )
{
betWon = false;
}
else if (diceTotal == ALL_TWO_VALUE )
{
if (diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +1 ].topFace()
&& diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +2 ].topFace())
{
betWon = false;
}
else
{
betWon = true;
}
}
else if (diceTotal == ALL_THREE_VALUE )
{
if (diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +1 ].topFace()
&& diceArray[INDEX_START_VALUE].topFace() == diceArray[INDEX_START_VALUE +2 ].topFace() )
{
betWon = false;
}
else
{
betWon = true;
}
}
else if(diceTotal < MAX_VALUE_LOW )
{
betWon = true;
}
else
{
betWon = false;
}
}
return betWon;
}
public static void printDisc(Dice[] diceArray, int diceTotal)
{
System.out.println("The Dice for this round were: ");
for( int index = INDEX_START_VALUE; index < diceArray.length; index++)
{
System.out.println( diceArray[index]);
}
System.out.println(" The Total was : " + diceTotal);
System.out.println("");
}
public static void gameOver(Wallet userWallet, double userStartingCash)
{
System.out.println("This game of Chuck-a-Luck is over");
System.out.println( userWallet.toString() );
if( userWallet.check() == MIN_WALLET_AMOUNT)
{
System.out.println("Unforunately, you lost all your money :( ");
}
else
{
double diffFromStartingAmount = ( userWallet.check() - userStartingCash);
if (diffFromStartingAmount == 0 )
{
System.out.println("You didn't make or lose any money!");
}
else if (diffFromStartingAmount > 0 )
{
String winings = Double.toString(diffFromStartingAmount);
System.out.println("Congrats! You made " + winings+ " euro.");
}
else
{
String winings = Double.toString(diffFromStartingAmount);
System.out.println("Hard luck! You lost " + winings + " euro.");
}
}
}
}