-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrapsGame.java
More file actions
50 lines (41 loc) · 1.23 KB
/
CrapsGame.java
File metadata and controls
50 lines (41 loc) · 1.23 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
public class CrapsGame{
private int numOfRolls = 0;
private CrapsMetricMonitor monitor;
// Constructor
public CrapsGame(CrapsMetricMonitor monitor)
{
this.monitor = monitor;
}
// Accessor & Mutators
public int getNumOfRolls() { return numOfRolls; }
public void resetNumOfRolls() { numOfRolls = 0; }
// Game Algorithm
public boolean playGame()
{
boolean playing = true;
int roll = (int)(Math.random()*6 + 1) + (int)(Math.random()*6 + 1);
numOfRolls++;
System.out.println("Rolled a " + roll);
if(roll == 7 || roll == 11){
System.out.println("*****Natural! You win!*****");
monitor.setNaturalCount();
return true;
} else if(roll == 2 || roll == 3 || roll == 12){
System.out.println("*****Craps! You loose.*****");
monitor.setCrapsCount();
return false;
} while(playing){
int newRoll = (int)(Math.random()*6 +1) + (int)(Math.random()*6 +1);
numOfRolls++;
System.out.println("Rolled a " + newRoll);
if(newRoll == roll){
System.out.println("*****Rolled the point! You win!*****");
return true;
}else if(newRoll == 7) {
System.out.println("*****Crap out! You loose.*****");
return false;
}
}
return true;
}
}