-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scoresheet.java
720 lines (648 loc) · 21 KB
/
Scoresheet.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
* Course: CSC421
* Assignment: #3
* Author: Andrew Seligman
* Date: May 7, 2014
* File: Scoresheet.java
*******************************************************************************/
import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import javax.swing.border.*;
import java.applet.Applet;
import java.net.*;
/**
* Scoresheet.java manages the scoring for a player. It uses parallel arrays
* to hold category names and the score for each category.
**/
public class Scoresheet extends JPanel
{
private String[] Categories;
private int[] Scores;
private boolean playerUsingThisApplet;
private boolean hasYahtzee = false;
private int yahtzeeBonus = 0;
private boolean hasUBonus = false;
private boolean[] CategoryScored;
private boolean isGoodScore;
private CharacterImage ImageScore = new CharacterImage();
private SoundLib Sounds;
private String[] SoundFiles;
private JButton muteButton;
private Image background;
public ImageIcon[] CategoryImg, CategoryBorderImg;
private JButton[] ButtonArray;
private JPanel[] TextScoreArray;
private JPanel[] TextPotentialArray;
private JLabel LblCategory;
private JLabel LblScore;
private JLabel LblPotential;
private JLabel NumYahtzees;
private JLabel NumYahtzeesCtr;
private JLabel LblPadding;
private JLabel LblPadding2;
private JLabel UpperBonus;
private ImageIcon CategoryLabel;
private ImageIcon YourScore;
private ImageIcon Potential;
private ImageIcon NumYahtzeesLabel;
private ImageIcon NumYahtzeesCtrLabel;
private ImageIcon UpperBonusLabel;
/**
* Constructor initializes the category arrays, score arrays, and
* JComponents displayed by the scoresheet.
**/
public Scoresheet(boolean isUsingThisApp, JButton mute)
{
super();
try{
playerUsingThisApplet = isUsingThisApp;
muteButton = mute;
Categories = new String[14];
Scores = new int[13];
CategoryScored = new boolean[13];
ButtonArray = new JButton[14];
TextScoreArray = new JPanel[13];
TextPotentialArray = new JPanel[13];
Categories[0] = "Ones";
Categories[1] = "Twos";
Categories[2] = "Threes";
Categories[3] = "Fours";
Categories[4] = "Fives";
Categories[5] = "Sixes";
Categories[6] = "3 of a Kind";
Categories[7] = "4 of a Kind";
Categories[8] = "Full House";
Categories[9] = "Small Straight";
Categories[10] = "Large Straight";
Categories[11] = "Yahtzee";
Categories[12] = "Chance";
Categories[13] = "No Score";
CategoryImg = new ImageIcon[14];
CategoryBorderImg = new ImageIcon[14];
for(int idx = 0; idx < 14; idx++)
{
String file = new String("Score"+idx+".png");
CategoryImg[idx] = new ImageIcon(getClass().getResource(file));
file = new String("Score"+idx+"Border.png");
CategoryBorderImg[idx] = new ImageIcon(getClass().getResource(file));
}
for(int idx = 0; idx < 13; idx++)
{
Scores[idx] = 0;
CategoryScored[idx] = false;
}
setLayout(new GridLayout(16,3));
createBtnLbl();
disableCategories();
addBtnLbl();
setPreferredSize(new Dimension(345,443));
setOpaque(false);
try
{
background = ImageIO.read(getClass().getResourceAsStream(
"scorebg.png"));
}
catch (IOException ex)
{
System.out.println(
"Error reading background image in Scoresheet.java");
}
SoundFiles = new String[3];
SoundFiles[0] = new String("buttonHover1.wav");
SoundFiles[1] = new String("buttonHover2.wav");
SoundFiles[2] = new String("buttonHover3.wav");
Sounds = new SoundLib(SoundFiles);
} catch (Exception e) { e.printStackTrace();}
}
/**
* Draw the background border image on the Scorecard
**/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
}
/**
* Initialize the JComponents used by the scoresheet and add images
**/
private void createBtnLbl()
{
LblCategory = new JLabel();
LblScore = new JLabel();
LblPotential = new JLabel();
CategoryLabel = new ImageIcon(getClass()
.getResource("Category.png"));
YourScore = new ImageIcon(getClass()
.getResource("YourScore.png"));
Potential = new ImageIcon(getClass()
.getResource("PotentialScore.png"));
LblCategory.setIcon(CategoryLabel);
LblScore.setIcon(YourScore);
LblPotential.setIcon(Potential);
for(int idx = 0; idx < 14; idx++)
{
final JButton currentButton = new JButton();
final ImageIcon[] Cat = CategoryImg;
final ImageIcon[] CatBorder = CategoryBorderImg;
final int index = idx;
if(playerUsingThisApplet)
{
currentButton.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
if(currentButton.isEnabled())
{
currentButton.setIcon(CatBorder[index]);
if(muteButton.isEnabled())
Sounds.playRandom();
}
}
public void mouseExited(MouseEvent e)
{
currentButton.setIcon(Cat[index]);
}
public void mousePressed(MouseEvent e)
{
currentButton.setIcon(Cat[index]);
}
});
}
ButtonArray[idx] = currentButton;
ButtonArray[idx].setBorder(BorderFactory.createEmptyBorder());
ButtonArray[idx].setIcon(CategoryImg[idx]);
ButtonArray[idx].setContentAreaFilled(false);
}
for(int idx = 0; idx < 13; idx++)
{
TextScoreArray[idx] = new JPanel();
TextPotentialArray[idx] = new JPanel();
TextScoreArray[idx].setOpaque(false);
TextPotentialArray[idx].setOpaque(false);
TextScoreArray[idx].setLayout(new BoxLayout(
TextScoreArray[idx], BoxLayout.LINE_AXIS));
TextPotentialArray[idx].setLayout(new BoxLayout(
TextPotentialArray[idx], BoxLayout.LINE_AXIS));
}
NumYahtzees = new JLabel();
NumYahtzeesCtr = ImageScore.getLetters("0")[0];
LblPadding = new JLabel(" ");
LblPadding2 = new JLabel(" ");
UpperBonus = new JLabel();
NumYahtzeesLabel = new ImageIcon(getClass()
.getResource("YahtzeeBonus.png"));
UpperBonusLabel = new ImageIcon(getClass()
.getResource("Meter0.png"));
NumYahtzees.setIcon(NumYahtzeesLabel);
UpperBonus.setIcon(UpperBonusLabel);
}
/**
* Add JComponents to the scoresheet.
**/
private void addBtnLbl()
{
add(LblCategory);
add(LblScore);
add(LblPotential);
for(int idx = 0; idx < 14; idx++)
{
add(ButtonArray[idx]);
if(idx < 13)
{
//ImageScore.getNumber() returns a new array; must create a
//variable for each JLabel since a JLabel can only be added
//to a component once
JLabel[] BlankScore1 = ImageScore.getNumber(0);
JLabel[] BlankScore2 = ImageScore.getNumber(0);
add(TextScoreArray[idx]);
add(TextPotentialArray[idx]);
TextScoreArray[idx].add(BlankScore1[0]);
TextPotentialArray[idx].add(BlankScore2[0]);
}
}
add(LblPadding);
add(UpperBonus);
add(LblPadding2);
add(NumYahtzees);
add(NumYahtzeesCtr);
validate();
}
/**
* Set a flag for the given index indicating that a category has been
* scored.
**/
public void usedCategory(int index)
{
CategoryScored[index] = true;
}
/**
* Enable the JButtons used by the scoresheet if they have not already
* been scored.
**/
public void enableCategories()
{
for(int idx = 0; idx < 13; idx++)
{
if(!CategoryScored[idx])
ButtonArray[idx].setEnabled(true);
}
//no score button is always a choice
ButtonArray[13].setEnabled(true);
}
/**
* Disable the JButtons used by the scoresheet
**/
public void disableCategories()
{
for(int idx = 0; idx < 14; idx++)
{
ButtonArray[idx].setEnabled(false);
}
}
/**
* Set the score for a category and update the images on the scoresheet.
* If the player scores a yahtzee, set the hasYahtzee flag and
* increment the yahtzee bonus counter
*
* @param index of the Score array to change
*
* @param score to insert
*
* @return boolean that determines whether the player recieves an
* additional turn because of multiple yahtzees
**/
public boolean setScore(int index, int score)
{
Scores[index] = score;
JLabel[] ImgScores;
int length;
boolean scoredAnotherYahtzee = false;
if(index == 11 && score == 50)
{
if(hasYahtzee)
{
yahtzeeBonus++;
remove(NumYahtzeesCtr);
NumYahtzeesCtr = ImageScore.getLetters(""+yahtzeeBonus)[0];
add(NumYahtzeesCtr);
scoredAnotherYahtzee = true;
}
hasYahtzee = true;
}
ImgScores = ImageScore.getNumber(score);
length = ImgScores.length;
TextScoreArray[index].removeAll();
for(int idx = 0; idx < length; idx++)
TextScoreArray[index].add(ImgScores[idx]);
TextScoreArray[index].revalidate();
return scoredAnotherYahtzee;
}
/**
* Calculate the score for a category based on the current state
* of the dice. If a category has already been scored, the potential
* score will not appear (except for the yahtzee category if it has
* not been scored as 0).
*
* @param index of the category used to determine the score
*
* @param DieValues is a string containing the five Die values
**/
public void setPotentialScores(int index, String DieValues)
{
JLabel[] ImgScores;
int length;
ImgScores = ImageScore.getNumber(calculateScore(index, DieValues));
length = ImgScores.length;
TextPotentialArray[index].removeAll();
TextPotentialArray[index].revalidate();
TextPotentialArray[index].repaint();
for(int idx = 0; idx < length; idx++)
{
if(!CategoryScored[index])
TextPotentialArray[index].add(ImgScores[idx]);
}
TextPotentialArray[index].revalidate();
}
/**
* Clear the potential score column
**/
public void hidePotentialScores()
{
for(int index = 0; index < 13; index++)
{
TextPotentialArray[index].removeAll();
TextPotentialArray[index].revalidate();
TextPotentialArray[index].repaint();
}
}
/**
* Return the name of a category at a given index in the Categories
* array.
*
* @param index of the category name to retrieve
*
* @return the name of the category
**/
public String getCategoryName(int index)
{
return Categories[index];
}
/**
* @return whether a player has scored the upper bonus
**/
public boolean hasUpperBonus()
{
return hasUBonus;
}
/**
* @return the number of yahtzee bonuses a player has earned
**/
public int getYahtzeeBonus()
{
return yahtzeeBonus;
}
/**
* Return the ImageIcon for the category at the given index
*
* @param idx in the index of the category to retrieve
*
* @return the ImageIcon for the category
**/
public ImageIcon getCategoryImg(int idx)
{
return CategoryImg[idx];
}
/**
* Determine the score granted for a category given the state of the dice
*
* @param index of the category to score
*
* @param DieValues is the string of die values
*
* @return score for the input category index (-1 if index is not in the
* array of categories)
**/
public int calculateScore(int index, String DieValues)
{
int score = 0;
int[] values = new int[5];
int[] valueCount = new int[6];
Scanner parseDieValues = new Scanner(DieValues);
isGoodScore = false;
//initialize valueCount array
for(int idx = 0; idx < 6; idx++)
{
valueCount[idx] = 0;
}
//parse the string of die values into an array and
//increment a counter for each value
for(int idx = 0; idx < 5; idx++)
{
int num = parseDieValues.nextInt();
values[idx] = num;
valueCount[(num - 1)]++;
}
switch(index)
{
//ones
case 0:
for(int idx = 0; idx < 5; idx++)
{
if(values[idx] == 1)
{
score++;
}
}
if(score > 3)
isGoodScore = true;
return score;
//twos
case 1:
for(int idx = 0; idx < 5; idx++)
{
if(values[idx] == 2)
{
score += 2;
}
}
if(score > 6)
isGoodScore = true;
return score;
//threes
case 2:
for(int idx = 0; idx < 5; idx++)
{
if(values[idx] == 3)
{
score += 3;
}
}
if(score > 9)
isGoodScore = true;
return score;
//fours
case 3:
for(int idx = 0; idx < 5; idx++)
{
if(values[idx] == 4)
{
score += 4;
}
}
if(score > 12)
isGoodScore = true;
return score;
//fives
case 4:
for(int idx = 0; idx < 5; idx++)
{
if(values[idx] == 5)
{
score += 5;
}
}
if(score > 15)
isGoodScore = true;
return score;
//sixes
case 5:
for(int idx = 0; idx < 5; idx++)
{
if(values[idx] == 6)
{
score += 6;
}
}
if(score > 18)
isGoodScore = true;
return score;
//3 of a kind
case 6:
for(int idx = 0; idx < 6; idx++)
{
if(valueCount[idx] >= 3)
{
score = 3 * (idx + 1);
}
}
if(score > 14)
isGoodScore = true;
return score;
//4 of a kind
case 7:
for(int idx = 0; idx < 6; idx++)
{
if(valueCount[idx] >= 4)
{
score = valueCount[idx] * (idx + 1);
}
}
if(score > 15)
isGoodScore = true;
return score;
//full house
case 8:
for(int idx = 0; idx < 6; idx++)
{
//check for 3 of a kind
if(valueCount[idx] == 3)
{
for(int subIdx = 0; subIdx < 6; subIdx++)
{
//check for a pair
if(valueCount[subIdx] == 2)
{
score = 25;
}
}
}
}
if(score == 25)
isGoodScore = true;
return score;
//small straight
case 9:
if(valueCount[0] >= 1 && valueCount[1] >= 1 &&
valueCount[2] >= 1 && valueCount[3] >= 1)
{
score = 30;
}
else if(valueCount[1] >= 1 && valueCount[2] >= 1 &&
valueCount[3] >= 1 && valueCount[4] >= 1)
{
score = 30;
}
else if(valueCount[2] >= 1 && valueCount[3] >= 1 &&
valueCount[4] >= 1 && valueCount[5] >= 1)
{
score = 30;
}
if(score == 30)
isGoodScore = true;
return score;
//large straight
case 10:
if(valueCount[0] >= 1 && valueCount[1] >= 1 &&
valueCount[2] >= 1 && valueCount[3] >= 1 &&
valueCount[4] >= 1)
{
score = 40;
}
else if(valueCount[1] >= 1 && valueCount[2] >= 1 &&
valueCount[3] >= 1 && valueCount[4] >= 1 &&
valueCount[5] >= 1)
{
score = 40;
}
if(score == 40)
isGoodScore = true;
return score;
//yahtzee
case 11:
for(int idx = 0; idx < 6; idx++)
{
if(valueCount[idx] == 5)
{
score = 50;
}
}
if(score == 50)
isGoodScore = true;
return score;
//chance
case 12:
for(int idx = 0; idx < 5; idx++)
{
score += values[idx];
}
if(score > 23)
isGoodScore = true;
return score;
}
//bad index value
return -1;
}
/**
* @return whether the previously scored category was scored with a
* "good" score. Used by a Human to play sounds.
**/
public boolean wasGoodScore()
{
return isGoodScore;
}
/**
* Get the score for a category
*
* @param index of the category to return
*
* @return score value for a category
**/
public int getScore(int index)
{
return Scores[index];
}
/**
* Calculate the total score for a player, including bonuses
*
* @return the total score
**/
public int getTotalScore()
{
int totalScore = 0;
for(int idx = 0; idx < 6; idx++)
{
totalScore += getScore(idx);
}
//determine if upper bonus achieved and update bonus meter
if(totalScore >= 63)
{
totalScore += 35;
UpperBonus.setIcon(new ImageIcon(getClass()
.getResource("Meter10.png")));
hasUBonus = true;
}
else if(totalScore < 60)
{
String file = new String("Meter"+totalScore/6+".png");
UpperBonus.setIcon(new ImageIcon(getClass().getResource(file)));
}
else
{
UpperBonus.setIcon(new ImageIcon(getClass()
.getResource("Meter9.png")));
}
for(int idx = 6; idx < 13; idx++)
{
totalScore += getScore(idx);
}
//add yahtzee bonus
if(yahtzeeBonus > 0)
totalScore += (yahtzeeBonus * 100);
return totalScore;
}
}