-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrapsHelper.java
304 lines (269 loc) · 9.94 KB
/
CrapsHelper.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
/**
*Program Name: CrapsHelper.java
*Purpose: This method contains helper methods and to be called from
* the main method Craps.java
*@author Ahmed Ibrahim, 1182926
*Date: 12 Mar 2024
*/
import java.util.Scanner;
public class CrapsHelper
{
/*Declaring Magic numbers and the scanner object and variables that will be available to use
for all the methods below*/
final static int ANTE = 100;
final static int MIN_BET = 10;
public static Scanner input = new Scanner(System.in);
public static int actionAmount = 0;
public static boolean winOrLose = false; //Declare boolean WinOrLose variable and make it available for
//both the checkDiceRoll() & adjustBankBalances() methods
/* Prompts the user for number of player then validates it is between 2 and 6.
* Then prompts the user for names and stores them in playerArray of the same size as the number */
public static String [] createPlayerArray()
{
System.out.print("Enter the number of players for this game (minimum of 2 to maximum of 6): ");
int num = input.nextInt();
while(num > 6 || num < 2)
{
System.out.print("Error, invalid entry. Minimum of 2 players to Maximum of 6: ");
num = input.nextInt();
input.nextLine();
}
String[] playerArray = new String[num];
for(int i = 0; i<playerArray.length; i++)
{
System.out.printf("Enter first name of player# %d and press ENTER: ", i + 1);
playerArray[i] = input.next();
}
return playerArray;
}
/* Creates a bankRollArray of the same size as the playerArray size to be passed as an argument
* in the main method*/
public static int[] createBankRollArray(int num)
{
int[] bankRollArray = new int[num];
for(int i = 0; i < bankRollArray.length; i++)
{
bankRollArray[i] = ANTE;
}
return bankRollArray;
}
/* Prints a short and brief overview of the rules of the game of craps.*/
public static void showRules()
{
System.out.println();
System.out.println(" ============== ");
System.out.println("In craps, players start with a \"come-out roll.\" Rolling a 7 or 11 wins, while 2, 3, or 12 loses."
+ " \nOther numbers set a \"point.\" The shooter aims to hit the point again before rolling a 7,"
+ " \nrolling a 7 ends the turn and results in a loss, \"sevening out,\" passing the dice.");
System.out.println(" ============== ");
}
/* Prompts the shooter for a bet, validates it is an exact multiple of 10, larger than 10 and
* up to the shooter's bank balance. Then updates the actionAmount by the bet and then print it to the screen.*/
public static void getShooterBet(String[] playerArray, int shooterID, int[] bankRollArray, int Min_Bet, int[] betAmmountArray)
{
// actionAmount = 0;
System.out.printf("You have $%d in your bank roll and minimum bet is $%d. Enter your bet amount: ", bankRollArray[shooterID], MIN_BET);
int shooterBet = input.nextInt();
while(!(shooterBet >= MIN_BET && shooterBet % MIN_BET == 0 && shooterBet <= bankRollArray[shooterID]))
{
System.out.print("Bet must be at least $10, exact multiple of 10 and up to your bank balance: ");
shooterBet = input.nextInt();
}
actionAmount = shooterBet;
betAmmountArray[shooterID] = shooterBet;
System.out.printf("\nThe Action Amount Before Oppnents' bets is: %d\n", actionAmount);
System.out.println();
}
/* Prompts the opponents for their bets and validates it is an exact multiple of 10, larger than and
* up to the actionAmount or the players' bank balances and prints the bets to the screen.*/
public static void getOpponentBet(String[] playerArray, int shooterID, int[] bankRollArray, int minBet, int[] betAmountArray)
{
//Assign every value in betAmountArray to zero before all players make their bets
for(int i = 0; i < betAmountArray.length; i++)
{
betAmountArray[i] = 0;
}
//Initializing variables
int i = 1;
int oppBet = 0;
for(i = 0; i < playerArray.length; i++)
{
if(i == shooterID || bankRollArray[i] < MIN_BET)
{
continue;
}
if(actionAmount < MIN_BET)
{
System.out.println("The action is covered, no more bets can be made");
break;
}
System.out.printf("%s, how much of the action do you want?\r\n"
+ "Enter your bet, minimum of $%d up to $%d, or your bank balance, whichever is less:", playerArray[i], MIN_BET, actionAmount);
oppBet = input.nextInt();
while(!(oppBet >= MIN_BET && oppBet <= actionAmount && oppBet % MIN_BET == 0
&& oppBet <= bankRollArray[i]))
{
System.out.printf("Bet must be at least $10, exact multiple of 10 "
+ "and minimum of $%d up to $%d, or your bank balance, whichever is less: ",
MIN_BET, actionAmount );
oppBet = input.nextInt();
}
System.out.println();
actionAmount -= oppBet;
betAmountArray[i] = oppBet;
}
int totalOppBets = 0;
for (int j = 0; j < playerArray.length; j++)
{
if(j == shooterID)
{
continue;
}
totalOppBets += betAmountArray[j];
}
actionAmount = totalOppBets;
System.out.println();
}
/*Returns a random integer between 2 and 12 inclusive.*/
public static int rollDice()
{
int dice = 2 + (int)(Math.random() * ((12 - 2) + 1));
return dice;
}
/* Takes the diceRoll, playerArray names, shooter ID, bankRollArray and betAmountArray as parameters.
* Then checks if the shooter won or lost the pass based on the rules of craps. Then prints the win or loss
* according to the outcome of the diceRoll.*/
public static void checkDiceRoll(int diceRoll, String[] playerArray, int shooterID, int[] bankRollArray, int[] betAmountArray)
{
//boolean winOrLose = false;
if(diceRoll == 7 || diceRoll == 11)
{
winOrLose = true;
System.out.printf("Congratulations %s, You have rolled a natural. You win!\n", playerArray[shooterID]);
}
else if(diceRoll == 2 || diceRoll == 3 || diceRoll == 12)
{
winOrLose = false;
System.out.printf("Sorry %s, You have crapped out. You lose!\n", playerArray[shooterID]);
}
else
{
System.out.printf("OK %s, your point is %d. To win, you need to roll your point again, but if you roll a 7, you lose.\n", playerArray[shooterID], diceRoll);
System.out.println("Rolling the dice again to try for your point...");
int newRoll = rollDice();
while(newRoll != diceRoll && newRoll != 7)
{
newRoll = rollDice();
System.out.printf("Rolling...you rolled a %d\n", newRoll);
}
if(newRoll == 7)
{
winOrLose = false;
System.out.printf("Sorry %s, You rolled a 7. You lose!\n", playerArray[shooterID]);
}
else
{
winOrLose = true;
System.out.printf("Congratulations %s! You rolled your point! You win!\n", playerArray[shooterID]);
}
}
System.out.println();
}
/* Takes bankRollArray, betAmountArray and shooterID as parameters.
* Adjusts bank balances of shooter and opponent based on the result of the pass.*/
public static void adjustBankBalances(int[] bankRollArray, int[] betAmountArray, int shooterID)
{
if(winOrLose)
{
for(int i = 0; i < bankRollArray.length; i++)
{
if (i == shooterID)
{
bankRollArray[i] += actionAmount;
}
else
{
if (betAmountArray[i] <= bankRollArray[i])
{
bankRollArray[i] -= betAmountArray[i];
}
else
{
bankRollArray[i] = 0;
}
}
}
}
else
{
for(int i = 0; i < bankRollArray.length; i++)
{
if (i == shooterID)
{
if (actionAmount <= bankRollArray[i])
{
bankRollArray[i] -= actionAmount;
}
else
{
bankRollArray[i] = 0;
}
}
else
{
bankRollArray[i] += betAmountArray[i];
}
}
}
}
/* Takes the playerArray, bankRollArray as parameters.
* Prints the player's bank rolls to the screen based on the value stored in the bankRollArray.*/
public static void printPlayerBankBalances(String[] playerArray, int[] bankRollArray)
{
System.out.println("After this pass, here are the bankroll balances for everyone:");
for(int i = 0; i < playerArray.length; i++)
{
System.out.printf("%s has $%d.\n", playerArray[i], bankRollArray[i]);
}
System.out.println();
}
/* Takes the bankRollArray and the totalMoney integer as parameters.
* check if one player has all the money in the game if so returns a true value, if not a false value.*/
public static boolean checkForWinnner(int[] bankRollArray, int totalMoney)
{
for (int balance : bankRollArray)
{
if (balance >= totalMoney)
{
return true;
}
}
return false;
}
/* Takes the shooterID and bankRollArray as parameters.
* returns the next shooter ID based on the current shooter ID.*/
public static int getNextShooter(int shooterID, int[] bankRollArray)
{
int nextShooterID = (shooterID + 1) % bankRollArray.length;
while(bankRollArray[nextShooterID] <= 0)
{
nextShooterID = (nextShooterID + 1) % bankRollArray.length;
}
return nextShooterID;
}
/* Takes the playerArray, bankRollArray and the totalMoney integer as parameters.
* check if one player has all the money in the game if so returns the name of the player
* that has all the totalMoney in his bankRoll, if not returns a null.*/
public static String identifyWinner(String[] playerArray, int[] bankRollArray, int totalMoney)
{
for (int i = 0; i < bankRollArray.length; i++)
{
if(bankRollArray[i] == totalMoney)
{
return playerArray[i];
}
}
return null;
}
}
//End of class