-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPigDice.java
160 lines (142 loc) · 3.74 KB
/
PigDice.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
/*
* @author Iffat Nusaiba Mahmood
* <p> PigDice
* <p> Project 3
* <p>
*/
//
// The rules to the dice game Pig
//
// Number of Players: 2 +
// Game Duration: 30 mins
// Players Aged: 6 +
//
// You will need: 2 dice and paper to score on.
//
// To Play: The players take turns to roll both dice,
// they can roll as many times as they want in one turn.
//
// A player scores the sum of the two dice thrown and
// gradually reaches a higher score as they continue to roll.
//
// If a single number 1 is thrown on either die, the score
// for that whole turn is lost. However a double 1 counts as 25.
// The first player to 100 wins unless a player scores more
// subsequently in the same round. This means that everyone in
// the game must have the same number of turns.
//
// this class manages the state of the dice and the scoring
public class PigDice
{
// keep track of total and round scores as well as the two dice.
private int _totalScore = 0;
private int _roundScore = 0;
private Die _die1;
private Die _die2;
//Constructor; instantiates dice for use by players.
public PigDice()
{
_die1 = new Die();
_die2 = new Die();
}
// accessor for total score
public int currentTotal()
{
return _totalScore;
}
// accessor for this round score
public int currentRound()
{
return _roundScore;
}
// accessor to see if the user has rolled a single "1" and loses turn
public boolean piggedOut()
{
return singleOneRolled();
}
// mutator that simulates rolling two dice and evaluating the resulting score
public void rollDice()
{
// Roll the die
_die1.roll();
_die2.roll();
}
// accessor for a formatted string of what the last roll looked like
public String lastRoll()
{
return "D1 (" + _die1.faceValue() + "), D2 (" + _die2.faceValue() + ")";
}
//accessor; computes rolled score and adds to round score. Returns rolled score.
public int evaluate()
{
int rolledScore;
//Pigged out score
if (singleOneRolled() == true)
{
_roundScore = 0;
rolledScore = 0;
}
//Computes double ones as a rolled score of 25
else if (doubleOnesRolled() == true)
{
rolledScore = 25;
}
//Otherwise adds the face values of the dice to compute rolled score.
else
{
rolledScore = _die1.faceValue() + _die2.faceValue();
}
//Adds rolled score to the current round score.
_roundScore += rolledScore;
return rolledScore;
}
//Returns true if exactly one die has a faceValue == 1
private boolean singleOneRolled()
{
int die1 = _die1.faceValue();
int die2 = _die2.faceValue();
boolean singleOne;
if((die1 == 1 && die2 != 1) || (die1 != 1 && die2 == 1))
{
singleOne = true;
}
else
{
singleOne = false;
}
return singleOne;
}
//Returns true if both dice have a faceValue == 1
private boolean doubleOnesRolled()
{
int die1 = _die1.faceValue();
int die2 = _die2.faceValue();
boolean doubleOnes;
if(die1 == 1 && die2 == 1)
{
doubleOnes = true;
}
else
{
doubleOnes = false;
}
return doubleOnes;
}
//
// mutator to end a round and keep the add this round to the total
// also returns the total value of the round and resets the round total for next time
//
public int save()
{
int thisRoundScore = _roundScore;
_totalScore += _roundScore;
_roundScore = 0;
return thisRoundScore;
}
//mutator that clears scores for a new game.
public void clear()
{
_totalScore = 0;
_roundScore = 0;
}
}