1 package net.sourceforge.simplegamenet.dice;
2
3 import java.io.Serializable;
4 import java.util.Arrays;
5
6 public class DiceScore implements DiceSettingOptions, DicePacketCodes, Serializable {
7
8 private Integer[] diceParticipantsIDs;
9 private int[] amountValues;
10
11
12
13 private int[] settingValues;
14
15
16
17
18
19
20
21
22 private DicePlayerScore[] playerScores;
23 private DiceAll allDice;
24
25 public DiceScore() {
26
27 }
28
29 public DiceScore(Integer[] diceParticipantsIDs, DiceSettings diceSettings, DiceAll allDice) {
30
31 this.diceParticipantsIDs = diceParticipantsIDs;
32 settingValues = diceSettings.getSettingValues();
33 amountValues = diceSettings.getAmountValues();
34 this.allDice = allDice;
35 playerScores = new DicePlayerScore[diceParticipantsIDs.length];
36 for (int i = 0; i < playerScores.length; i++) {
37 playerScores[i] = new DicePlayerScore();
38 }
39 }
40
41 public void setDiceSettingValues(DiceSettings diceSettings) {
42 settingValues = diceSettings.getSettingValues();
43 amountValues = diceSettings.getAmountValues();
44 }
45
46 public int countScore(Integer diceParticipantID, int scoreToCount) {
47 int[] diceValues = new int[5];
48 int[] checkEqual = new int[6];
49 diceValues = allDice.getDice();
50 Arrays.sort(diceValues);
51 for (int i = 0; i < checkEqual.length; i++) {
52 checkEqual[i] = 0;
53 }
54 int score = 0;
55 boolean scoreReached = false;
56 switch (scoreToCount) {
57 case 0:
58 for (int i = 0; i < diceValues.length; i++) {
59 if (diceValues[i] == 0) {
60 score += 1;
61 }
62 }
63 scoreReached = true;
64 break;
65 case 1:
66 for (int i = 0; i < diceValues.length; i++) {
67 if (diceValues[i] == 1) {
68 score += 2;
69 }
70 }
71 scoreReached = true;
72 break;
73 case 2:
74 for (int i = 0; i < diceValues.length; i++) {
75 if (diceValues[i] == 2) {
76 score += 3;
77 }
78 }
79 scoreReached = true;
80 break;
81 case 3:
82 for (int i = 0; i < diceValues.length; i++) {
83 if (diceValues[i] == 3) {
84 score += 4;
85 }
86 }
87 scoreReached = true;
88 break;
89 case 4:
90 for (int i = 0; i < diceValues.length; i++) {
91 if (diceValues[i] == 4) {
92 score += 5;
93 }
94 }
95 scoreReached = true;
96 break;
97 case 5:
98 for (int i = 0; i < diceValues.length; i++) {
99 if (diceValues[i] == 5) {
100 score += 6;
101 }
102 }
103 scoreReached = true;
104 break;
105 case 8:
106 for (int i = 0; i < checkEqual.length; i++) {
107 for (int j = 0; j < diceValues.length; j++) {
108 if (i == diceValues[j]) {
109 checkEqual[i]++;
110 }
111 }
112 }
113 for (int i = 0; i < checkEqual.length; i++) {
114 if (checkEqual[i] > 2) {
115 scoreReached = true;
116 for (int j = 0; j < diceValues.length; j++) {
117 score += (diceValues[j] + 1);
118 }
119 }
120 }
121 break;
122 case 9:
123 for (int i = 0; i < checkEqual.length; i++) {
124 for (int j = 0; j < diceValues.length; j++) {
125 if (i == diceValues[j]) {
126 checkEqual[i]++;
127 }
128 }
129 }
130 for (int i = 0; i < checkEqual.length; i++) {
131 if (checkEqual[i] > 3) {
132 scoreReached = true;
133 for (int j = 0; j < diceValues.length; j++) {
134 score += (diceValues[j] + 1);
135 }
136 }
137 }
138 break;
139 case 10:
140 for (int i = 0; i < checkEqual.length; i++) {
141 for (int j = 0; j < diceValues.length; j++) {
142 if (i == diceValues[j]) {
143 checkEqual[i]++;
144 }
145 }
146 }
147 for (int i = 0; i < checkEqual.length; i++) {
148 if (checkEqual[i] >= 2) {
149 for (int j = 0; j < checkEqual.length; j++) {
150 if ((checkEqual[j] == 3 && i != j) ||
151 (checkEqual[j] == 5 && i == j)) {
152 scoreReached = true;
153 score = settingValues[3];
154 }
155 }
156 }
157 }
158 break;
159 case 11:
160 for (int i = 0; i < (diceValues.length - 1); i++) {
161 if ((diceValues[i] + 1) == diceValues[i + 1]) {
162 score++;
163 }
164 }
165 if (score > 2) {
166 scoreReached = true;
167 score = settingValues[4];
168 }
169 break;
170 case 12:
171 for (int i = 0; i < (diceValues.length - 1); i++) {
172 if ((diceValues[i] + 1) == diceValues[i + 1]) {
173 score++;
174 }
175 }
176 if (score > 3) {
177 scoreReached = true;
178 score = settingValues[5];
179 }
180 break;
181 case 13:
182 for (int i = 0; i < checkEqual.length; i++) {
183 for (int j = 0; j < diceValues.length; j++) {
184 if (i == diceValues[j]) {
185 checkEqual[i]++;
186 }
187 }
188 }
189 for (int i = 0; i < checkEqual.length; i++) {
190 if (checkEqual[i] > 4) {
191 scoreReached = true;
192 score = settingValues[6];
193 }
194 }
195 break;
196 case 14:
197 for (int i = 0; i < diceValues.length; i++) {
198 score += (diceValues[i] + 1);
199 }
200 scoreReached = true;
201 break;
202 default :
203 System.out.println("Unrecognized score");
204 }
205 if (scoreReached) {
206 return score;
207 }
208 return 0;
209 }
210
211 public void adjustSettingValues(int scoreToCount) {
212 switch (scoreToCount) {
213 case 6:
214 switch (settingValues[0]) {
215 case FIRST_PLAYER:
216 settingValues[2] = 0;
217 break;
218 case MIN_5:
219 settingValues[2] -= 5;
220 break;
221 case MIN_10:
222 settingValues[2] -= 10;
223 break;
224 }
225 if (settingValues[2] < 0) {
226 settingValues[2] = 0;
227 }
228 break;
229 case 10:
230 switch (settingValues[1]) {
231 case FIRST_PLAYER:
232 settingValues[3] = 0;
233 break;
234 case MIN_5:
235 settingValues[3] -= 5;
236 break;
237 case MIN_10:
238 settingValues[3] -= 10;
239 break;
240 }
241 if (settingValues[3] < 0) {
242 settingValues[3] = 0;
243 }
244 break;
245 case 11:
246 switch (settingValues[1]) {
247 case FIRST_PLAYER:
248 settingValues[4] = 0;
249 break;
250 case MIN_5:
251 settingValues[4] -= 5;
252 break;
253 case MIN_10:
254 settingValues[4] -= 10;
255 break;
256 }
257 if (settingValues[4] < 0) {
258 settingValues[4] = 0;
259 }
260 break;
261 case 12:
262 switch (settingValues[1]) {
263 case FIRST_PLAYER:
264 settingValues[5] = 0;
265 break;
266 case MIN_5:
267 settingValues[5] -= 5;
268 break;
269 case MIN_10:
270 settingValues[5] -= 10;
271 break;
272 }
273 if (settingValues[5] < 0) {
274 settingValues[5] = 0;
275 }
276 break;
277 case 13:
278 switch (settingValues[1]) {
279 case FIRST_PLAYER:
280 settingValues[6] = 0;
281 break;
282 case MIN_5:
283 settingValues[6] -= 5;
284 break;
285 case MIN_10:
286 settingValues[6] -= 10;
287 break;
288 }
289 if (settingValues[6] < 0) {
290 settingValues[6] = 0;
291 }
292 break;
293 }
294 }
295
296 public void setPlayerScore(Integer diceParticipantID, int scoreNumber, int score) {
297 for (int i = 0; i < diceParticipantsIDs.length; i++) {
298 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
299 playerScores[i].setNewPoints(scoreNumber, score);
300 }
301 }
302 }
303
304 public int getPlayerScore(Integer diceParticipantID, int scoreNumber) {
305 for (int i = 0; i < diceParticipantsIDs.length; i++) {
306 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
307 return playerScores[i].getPoints(scoreNumber);
308 }
309 }
310 return -1;
311 }
312
313 public int getPlayerFOAKScore(Integer diceParticipantID) {
314 for (int i = 0; i < diceParticipantsIDs.length; i++) {
315 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
316 return playerScores[i].getFOAKPoints(12);
317 }
318 }
319 return 0;
320 }
321
322 public int[] getPlayerTotals(Integer diceParticipantID) {
323 for (int i = 0; i < diceParticipantsIDs.length; i++) {
324 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
325 return playerScores[i].getPlayerTotal();
326 }
327 }
328 return null;
329 }
330
331 public int checkBonus(Integer diceParticipantID) {
332 int bonusScore = 0;
333 for (int i = 0; i < diceParticipantsIDs.length; i++) {
334 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
335 int[] playerTotals = getPlayerTotals(diceParticipantID);
336 if (playerTotals[0] > 62 && !(settingValues[2] == 0)
337 && !playerScores[i].getBonusFilled()) {
338 playerScores[i].setNewPoints(6, settingValues[2]);
339 bonusScore = settingValues[2];
340 } else if ((playerTotals[0] <= 62 || settingValues[2] == 0)
341 && !playerScores[i].getBonusFilled()) {
342 playerScores[i].setNewPoints(6, 0);
343 }
344 }
345 }
346 return bonusScore;
347 }
348
349 public boolean getPlayerBonusFilled(Integer diceParticipantID) {
350 for (int i = 0; i < diceParticipantsIDs.length; i++) {
351 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
352 return playerScores[i].getBonusFilled();
353 }
354 }
355 return true;
356 }
357
358 public void setPlayerBonusFilled(Integer diceParticipantID,
359 boolean playerBonusFilled) {
360 for (int i = 0; i < diceParticipantsIDs.length; i++) {
361 if (diceParticipantID.equals(diceParticipantsIDs[i])) {
362 playerScores[i].setBonusFilled(playerBonusFilled);
363 }
364 }
365 }
366
367 public int[] getWinner() {
368 int[] grandTotals = new int[diceParticipantsIDs.length];
369 int[] winner = new int[2];
370 for (int i = 0; i < diceParticipantsIDs.length; i++) {
371 grandTotals[i] = playerScores[i].getGrandTotal();
372 }
373 winner[0] = 0;
374 winner[1] = grandTotals[0];
375 for (int i = 1; i < diceParticipantsIDs.length; i++) {
376 if (grandTotals[i] > winner[1]) {
377 winner[0] = i;
378 winner[1] = grandTotals[i];
379 }
380 }
381 for (int i = 0; i < diceParticipantsIDs.length; i++) {
382 if ((grandTotals[winner[0]] == grandTotals[i]) && (winner[0] != i)) {
383 winner[1] = 0;
384 }
385 }
386 return winner;
387 }
388
389 public int[] getSettingValues() {
390 return settingValues;
391 }
392
393 public int[] getAmountValues() {
394 return amountValues;
395 }
396
397 }
398