-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrapsSimulation.java
More file actions
136 lines (113 loc) · 3.31 KB
/
CrapsSimulation.java
File metadata and controls
136 lines (113 loc) · 3.31 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
import java.util.Scanner;
public class CrapsSimulation{
private CrapsGame game;
private CrapsMetricMonitor monitor;
private String name;
private int balance;
private int betAmount;
private int winStreak;
private int loseStreak;
private Scanner s;
// Constructor
public CrapsSimulation() {
monitor = new CrapsMetricMonitor();
game = new CrapsGame(monitor);
winStreak = 0;
loseStreak = 0;
s = new Scanner(System.in);
}
// Simulation Algorithm
public void start() {
boolean win;
// User Input
System.out.print("Welcome to SimCraps! Enter your user name: ");
name = s.nextLine();
while(true)
{
if(!name.contains(" "))
break;
System.out.println("Invalid, please enter username without whitespaces");
name = s.nextLine();
}
System.out.println("Hello " + name + "!");
System.out.print("Enter the amount of money you will bring to the table: ");
balance = s.nextInt();
while(balance < 1) {
System.out.print("Invalid amount! Please enter an amount greater than 0: ");
balance = s.nextInt();
}
System.out.print("Enter the bet amount between $1 and $" + balance + ": ");
betAmount = s.nextInt();
while(betAmount > balance || betAmount < 1) {
System.out.print("Invalid bet! Please enter a bet between $1 and " + balance + ".");
betAmount = s.nextInt();
}
// Start Game Play
monitor.setMaxBalance(balance);
monitor.setMaxBalanceGame(1);
int originalBet = betAmount;
while(balance > 0) {
//win = game.playGame();
monitor.setGamesPlayed();
if(betAmount > balance && balance > 0){
betAmount = balance;
} if (originalBet > betAmount && balance > 0 && balance > betAmount) {
if (originalBet >= balance)
betAmount = balance;
else
betAmount = originalBet;
}
System.out.println(name + " bets $" + betAmount);
win = game.playGame();
if(balance > monitor.getMaxBalance()) {
monitor.setMaxBalance(balance);
monitor.setMaxBalanceGame(monitor.getGamesPlayed());
}if(game.getNumOfRolls() > monitor.getMaxRolls()){
monitor.setMaxRolls(game.getNumOfRolls());
}if(win){
winStreak++;
monitor.setGamesWon();
if(winStreak > monitor.getMaxWinStreak()){
monitor.setMaxWinStreak(winStreak);
}
loseStreak = 0;
balance += betAmount;
game.resetNumOfRolls();
}else if(!win){
loseStreak++;
monitor.setGamesLost();
if(loseStreak > monitor.getMaxLoseStreak()){
monitor.setMaxLoseStreak(loseStreak);
}
winStreak = 0;
balance -= betAmount;
game.resetNumOfRolls();
}
if(balance > 0){
System.out.println(name + "'s balance: " + balance + ". Playing a new game...");
//System.out.println(name + " bets $" + betAmount);
}else
System.out.println(name + "'s balance: " + balance + ". End of game");
}
monitor.printStatistics();
// Replay?
boolean response = true;
System.out.print("Replay? Enter 'y' or 'n': ");
String replay = s.next();
while( response ) {
if(replay.charAt(0) == 'y'){
response = false;
CrapsSimulation simulation = new CrapsSimulation();
simulation.start();
}else if(replay.charAt(0) == 'n'){
System.out.println("Thanks for playing!");
response = false;
}else {
System.out.println("Please enter a valid response.");
replay = s.next();
//response = true;
}
}
s.close();
}
}