forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScore.java
More file actions
276 lines (264 loc) · 7.66 KB
/
Score.java
File metadata and controls
276 lines (264 loc) · 7.66 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
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
/**
* Score board of the yahtzee game. Keeps track of player points.
* @author Felipe Scrochio Custodio, Gabriel Scalici
*/
public class Score {
/* positions = board with position numbers */
private int[] positions;
/* plays = board with scoring. -1 indicates position wasn't used yet. */
public int[] plays;
/* player total points */
private int points;
public Score() {
/* fill scoreboard with positions and
* set plays positions to unused (-1) */
positions = new int[11];
plays = new int[11];
for (int i = 0; i < 11; i++) {
plays[i] = -1;
switch (i) {
case 0:
positions[0] = 1;
break;
case 1:
positions[1] = 7;
break;
case 2:
positions[2] = 4;
break;
case 3:
positions[3] = 2;
break;
case 4:
positions[4] = 8;
break;
case 5:
positions[5] = 5;
break;
case 6:
positions[6] = 3;
break;
case 7:
positions[7] = 9;
break;
case 8:
positions[8] = 6;
break;
case 9:
positions[9] = -1;
break;
case 10:
positions[10] = 10;
break;
}
}
points = 0;
}
/* scoring methods */
/**
* checks if player has five dices of a kind
* @param dices results of the dices
* @return boolean
*/
public boolean checkFive(int[] dices) {
/* check for five of a kind */
boolean isFive = false;
int counter;
for (int i = 0; i < dices.length; i++) {
counter = 0;
for (int j = 0; j < dices.length; j++) {
if (dices[j] == dices[i]) {
counter++;
}
}
if (counter == dices.length) {
isFive = true;
}
}
return isFive;
}
/**
* checks if player has four dices of a kind
* @param dices results of the dices
* @return boolean
*/
public boolean checkFour(int[] dices) {
/* check for four of a kind */
boolean isFour = false;
int counter;
for (int i = 0; i < dices.length; i++) {
counter = 0;
for (int j = 0; j < dices.length; j++) {
if (dices[j] == dices[i]) {
counter++;
}
}
if (counter == 4) {
isFour = true;
}
}
return isFour;
}
/**
* checks for fullhouse scoring
* @param dices results of the dices
* @return boolean
*/
public boolean fullHouse(int[] dices) {
/* check for full house */
int counter;
boolean isFull = false;
for (int i = 0; i < dices.length; i++) {
counter = 0;
for (int j = 0; j < dices.length; j++) {
if (dices[j] == dices[i]) {
counter++;
}
}
if (counter == 3) {
isFull = true;
}
}
return isFull;
}
/**
* checks for straight scoring
* @param dices results of the dices
* @return boolean
*/
public boolean checkStraight(int[] dices) {
/* sort dices (bubble) */
int aux;
boolean isSeq = false;
for (int i = 0; i < dices.length; i++) {
for (int j = 0; j < 4; j++) {
if (dices[j] > dices[j + 1]) {
aux = dices[j];
dices[j] = dices[j + 1];
dices[j + 1] = aux;
}
}
}
aux = 0;
/* check if sequence is 1 2 3 4 5 */
for (int i = 0; i < dices.length; i++) {
if (dices[i] == i + 1) {
aux++;
}
}
if (aux == 5) {
isSeq = true;
} else {
/* check if sequence is 2 3 4 5 6 */
aux = 0;
for (int i = 0; i < dices.length; i++) {
if (dices[i] == i + 2) {
aux++;
}
}
if (aux == 5) {
isSeq = true;
}
}
return isSeq;
}
/**
* Adds a score to a determined position in the scoreboard. Sets position to used.
* @param Position to be filled, dices results
*/
public void add(int position, int[] dices) throws java.lang.IllegalArgumentException {
/* indexes of scoreboard != positions of scoreboard */
int sum = 0;
/* get sum of dices results */
for (int i = 0; i < 5; i++) {
sum += dices[i];
}
switch (position) {
case 0:
plays[0] = sum;
break;
case 1:
/* check for full house */
if (fullHouse(dices)) {
plays[1] = 15;
} else {
plays[1] = 0;
}
case 2:
plays[2] = sum;
break;
case 3:
plays[3] = sum;
break;
case 4:
/* check for straight sequence */
if (checkStraight(dices)) {
plays[4] = 20;
} else {
plays[4] = 0;
}
case 5:
plays[5] = sum;
break;
case 6:
plays[6] = sum;
break;
case 7:
/* check for 4 of a kind */
if (checkFour(dices)) {
plays[7] = 30;
break;
} else {
plays[7] = 0;
break;
}
case 8:
plays[8] = sum;
break;
case 10:
/* check for five of a kind */
if (checkFive(dices)) {
plays[10] = 40;
} else {
plays[10] = 0;
}
}
}
/**
* Gets the sum of results, considering only occupied positions
* @return Retorna o valor da sum
*/
public int getScore() {
points = 0;
for (int i = 0; i < 11; i++) {
if (plays[i] != -1) points += plays[i];
}
return points;
}
/**
* Graphic representation of the scoreboard
*/
public void display() {
int pos = 0;
int breakLine = 0;
for (int i = 0; i < 11; i++) {
if (i != 9) {
if (plays[i] != -1) {
System.out.print(plays[i] + "\t|\t");
breakLine++;
} else {
System.out.print(positions[i] + "\t|\t");
breakLine++;
}
if (breakLine == 3) {
breakLine = 0;
System.out.print("\n");
System.out.println("---------------------");
}
} else {
System.out.print("\t|\t");
}
}
System.out.print("\n");
}
}