-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoresheet.java
More file actions
385 lines (333 loc) · 11.7 KB
/
Scoresheet.java
File metadata and controls
385 lines (333 loc) · 11.7 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
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
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JToggleButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import java.awt.Component;
import java.io.Serializable;
public class Scoresheet implements Serializable{
enum Category{
ACES,
TWOS,
THREES,
FOURS,
FIVES,
SIXES,
THREE_KIND,
FOUR_KIND,
HOUSE,
SMALL,
LARGE,
YAHTZEE,
CHANCE
}
int [][] scores;
transient int [] prospective;
int players;
Dice handlerDice;
YahtzeePanel ypanel;
transient ScoresheetPanel panel;
static String [] scoringCategories = {
"Aces",
"Twos",
"Threes",
"Fours",
"Fives",
"Sixes",
"3 of a kind",
"4 of a kind",
"Full House",
"Small Straight",
"Large Straight",
"Yahtzee",
"Chance"
};
public Scoresheet(int player_count, Dice dice, YahtzeePanel yahtzeePanel){
players = player_count;
handlerDice = dice;
ypanel = yahtzeePanel;
scores = new int [player_count][13];
prospective = new int[] { -1 , -1 };
// unsure of how to do prospective scores, maybe that's another arr?
reset_scores();
panel = new ScoresheetPanel();
panel.update_panel();
}
public void copy(Scoresheet rhs){
players = rhs.players;
scores = new int [players][13];
for (int player = 0; player < players; player++){
for (int score = 0; score < 13; score++){
scores[player][score] = rhs.scores[player][score];
}
}
handlerDice.copy(rhs.handlerDice);
}
void reset_scores(){
for (int player = 0; player < players; player++){
for (int score = 0; score < 13; score++){
scores[player][score] = -1;
}
}
}
public int [] getResult(){
int winner [] = {0, 0};
for (int player = 0; player < players; player++ ){
int score = 0;
for (int catagory = 0; catagory < 13; catagory++){
if ( scores[player][catagory] >= 0){ score += scores[player][catagory]; }
}
if (winner[1] < score){
winner = new int [] {player, score};
}
}
return winner;
}
public int compute_score(Category cat, Dice dice){
int score = 0;
int [] counts = new int [6];
boolean [] present = new boolean [7];
int total = 0;
switch(cat){
case ACES:
for(int i = 0; i < 5; i++){
if(dice.getValue(i) == 1){
score += 1;
}
}
break;
case TWOS:
for(int i = 0; i < 5; i++){
if(dice.getValue(i) == 2){
score += 2;
}
}
break;
case THREES:
for(int i = 0; i < 5; i++){
if(dice.getValue(i) == 3){
score += 3;
}
}
break;
case FOURS:
for(int i = 0; i < 5; i++){
if(dice.getValue(i) == 4){
score += 4;
}
}
break;
case FIVES:
for(int i = 0; i < 5; i++){
if(dice.getValue(i) == 5){
score += 5;
}
}
break;
case SIXES:
for(int i = 0; i < 5; i++){
if(dice.getValue(i) == 6){
score += 6;
}
}
break;
case THREE_KIND:
//count dice
for(int i = 0; i < 5; i++){
counts[dice.getValue(i) - 1]++;
total += dice.getValue(i);
}
//check for three of kind
for(int i = 0; i < 6; i++){
if(counts[i] >= 3){
score = total;
}
}
break;
case FOUR_KIND:
//count dice
for(int i = 0; i < 5; i++){
counts[dice.getValue(i) - 1]++;
total += dice.getValue(i);
}
//check for three of kind
for(int i = 0; i < 6; i++){
if(counts[i] >= 4){
score = total;
}
}
break;
case HOUSE:
//count dice
for(int i = 0; i < 5; i++){
counts[dice.getValue(i) - 1]++;
}
boolean two = false;
boolean three = false;
//find house
for(int i = 0; i < 6; i++){
if(counts[i] == 2){
two = true;
}
if(counts[i] == 3){
three = true;
}
if(counts[i] == 5){
two = three = true;
}
}
//check for house
if(two && three){
score = 25;
}
break;
case SMALL:
//find dice
for(int i = 0; i < 5; i++){
present[dice.getValue(i)] = true;
}
//check for small
if(present[1] && present[2] && present[3] && present[4]){
score = 30;
}
else if(present[2] && present[3] && present[4] && present[5]){
score = 30;
}
else if(present[3] && present[4] && present[5] && present[6]){
score = 30;
}
break;
case LARGE:
//find dice
for(int i = 0; i < 5; i++){
present[dice.getValue(i)] = true;
}
//check for large
if(present[1] && present[2] && present[3] && present[4] && present[5]){
score = 40;
}
else if(present[2] && present[3] && present[4] && present[5] && present[6]){
score = 40;
}
break;
case YAHTZEE:
//count dice
for(int i = 0; i < 5; i++){
counts[dice.getValue(i) - 1]++;
}
//check for yahtzee
for(int i = 0; i < 6; i++){
if(counts[i] == 5){
score = 50;
}
}
break;
case CHANCE:
for(int i = 0; i < 5; i++){
score += dice.getValue(i);
}
break;
}
return score;
}
public void play(){
prospective = new int [] {-1, -1};
panel.update_panel();
}
// for actually calculating the scores, initially I was thinking of doing a strategy pattern kind
// of thing where each category has an interface, but I think it might be easier to just have a for loop
// and a switch of the category index that returns the score for that category in one function
// the function would take in an int[6] or maybe a Dice object would be easier
class ScoresheetPanel extends JPanel implements ActionListener {
JToggleButton [] cat_buttons;
ScoresheetPanel(){
update_panel();
}
void update_panel(){
this.removeAll();
this.setLayout(new GridLayout(15, players + 1));
this.add(new JLabel(""));
for (int player = 0; player < players; player++){
JLabel playerLabel = new JLabel("Player " + (player + 1));
if ( ypanel.getCurPlayer() == player + 1 ){
playerLabel.setForeground(Color.RED);
}
this.add(playerLabel);
}
for (int catagory = 0; catagory < 13; catagory++){
CategoryToggleButton new_button = new CategoryToggleButton(scoringCategories[catagory]);
new_button.addActionListener(this);
new_button.setCatagory(catagory);
this.add(new_button);
for (int player = 0; player < players; player++){
JLabel label = new JLabel( scores[player][catagory] >= 0 ? "" + scores[player][catagory] : "" );
if ( prospective[0] == player && prospective[1] == catagory ){
label.setForeground(Color.RED);
}
this.add(label);
}
}
JLabel label = new JLabel("Total");
add(label);
for(int player = 0; player < players; player++){
int total_score = 0;
for(int category = 0; category < 13; category++){
if(prospective[0] == player && prospective[1] == category){
continue;
}
if(scores[player][category] > 0){
total_score += scores[player][category];
}
}
JLabel score_label = new JLabel(String.format("%d", total_score));
add(score_label);
}
//draw borders
boolean first = true;
for (Component component : this.getComponents())
{
if(first){
first = false;
continue;
}
JComponent jcomponent = (JComponent)component;
jcomponent.setBorder(BorderFactory.createLineBorder(Color.black));
}
}
public void actionPerformed(ActionEvent e){
for(int i = 0; i < 5; i++){
if(handlerDice.getValue(i) < 1){return;}
}
CategoryToggleButton pressed = (CategoryToggleButton)e.getSource();
System.out.println(pressed.getCatagory().toString() + " " + pressed.getCatagory().ordinal());
if ( prospective[0] != -1){ scores [ prospective[0] ][ prospective[1] ] = -1; }
if ( scores [ypanel.getCurPlayer() - 1][pressed.getCatagory().ordinal()] < 0){
ypanel.set_playable(true);
scores [ypanel.getCurPlayer() - 1][pressed.getCatagory().ordinal()] = compute_score(pressed.getCatagory(), handlerDice);
prospective[0] = ypanel.getCurPlayer() - 1;
prospective[1] = pressed.getCatagory().ordinal();
};
update_panel();
}
}
class CategoryToggleButton extends JToggleButton{
private Category cat;
CategoryToggleButton(String s) { super(s); }
public Category getCatagory(){ return cat; }
public void setCatagory(Category catagory){ cat = catagory; }
public void setCatagory(int catagory){ cat = Category.values()[catagory]; }
}
}