-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYahtzee.java
More file actions
283 lines (242 loc) · 7.58 KB
/
Yahtzee.java
File metadata and controls
283 lines (242 loc) · 7.58 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
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
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.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.io.Serializable;
import java.io.*;
public class Yahtzee
{
private static JMenuBar mb;
private static JMenu options;
static JMenuItem save, load;
public static void main( String args[] )
{
JFrame frame = new JFrame("Yahtzee");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//menu bar
mb = new JMenuBar();
options = new JMenu("Options");
save = new JMenuItem("Save");
load = new JMenuItem("Load");
options.add(save);
options.add(load);
mb.add(options);
frame.setJMenuBar(mb);
YahtzeePanel yp = new YahtzeePanel();
save.addActionListener(yp);
load.addActionListener(yp);
frame.add(yp);
frame.setSize(550, 475); // set frame size
frame.setVisible(true); // display frame
}
}
class YahtzeePanel extends JPanel implements ActionListener, Serializable
{
private Scoresheet scoresheet;
private Dice dice;
private Integer turn, round;
private transient JButton roll, play, newGame, loadGame;
private boolean playable;
private int players;
private int curPlayer;
public YahtzeePanel()
{
MainMenu();
}
public int promptNumplayers(){
String num_players = JOptionPane.showInputDialog("How many people are playing?");
if(num_players == null){
return -1;
}
int players = Integer.parseInt(num_players);
return players;
}
public int getCurPlayer(){
return curPlayer;
}
public void MainMenu(){
this.removeAll();
setLayout(new FlowLayout());
JLabel title = new JLabel("YAHTZEE!");
title.setFont(new Font("Serif", Font.PLAIN, 100));
add(title);
newGame = new JButton("New Game");
newGame.addActionListener(this);
add(newGame);
File f = new File("game_data.txt");
loadGame = new JButton("Load Game");
loadGame.addActionListener(this);
if(f.exists()){
add(loadGame);
}
}
public void EndGame(){
this.removeAll();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JLabel winner = new JLabel("Player " + (scoresheet.getResult()[0] + 1) + " wins!");
winner.setFont(new Font("Serif", Font.PLAIN, 64));
add(winner);
JLabel winnerScore = new JLabel("with a score of " + scoresheet.getResult()[1]);
add(winnerScore);
newGame = new JButton("New Game");
newGame.addActionListener(this);
add(newGame);
File f = new File("game_data.txt");
loadGame = new JButton("Load Game");
loadGame.addActionListener(this);
if(f.exists()){
add(loadGame);
}
this.revalidate();
this.repaint();
}
public void buildPanel(){
this.removeAll();
setLayout(new FlowLayout());
add(scoresheet.panel);
add(dice.panel);
add(roll);
add(play);
}
public void start_game(boolean newGame)
{
if(newGame){
players = promptNumplayers();
}
else{
players = 1;
}
if(players <= 0){
return;
}
this.removeAll();
dice = new Dice();
scoresheet = new Scoresheet(players, dice, this);
round = 0;
curPlayer = 1;
roll = new JButton("ROLL");
roll.addActionListener(this);
play = new JButton("PLAY");
play.addActionListener(this);
buildPanel();
this.revalidate();
turn = 0;
dice.reset();
scoresheet.play();
}
//only updates dice and buttons, not scorecards
public void start_round()
{
curPlayer++;
if(curPlayer > players){
curPlayer = 1;
}
roll.setEnabled(true);
play.setEnabled(false);
if (curPlayer == 1){
round++;
}
turn = 0;
dice.reset();
dice.panel.updateDiceButtons();
}
private void save_data(){
try (FileOutputStream f = new FileOutputStream("game_data.txt");
ObjectOutputStream s = new ObjectOutputStream(f))
{
s.writeObject(this);
s.close();
System.out.println("Successfully saved game!");
} catch (IOException error) {
error.printStackTrace();
System.out.println("IO save error: file stream");
}
}
private void load_data(){
//read data
YahtzeePanel loaded_panel = null;
try(FileInputStream in = new FileInputStream("game_data.txt");
ObjectInputStream s = new ObjectInputStream(in)) {
loaded_panel = (YahtzeePanel) s.readObject();
s.close();
//load into current panel
this.scoresheet.copy(loaded_panel.scoresheet);
this.dice.copy(loaded_panel.dice);
this.turn = loaded_panel.turn;
this.round = loaded_panel.round;
this.playable = loaded_panel.playable;
this.curPlayer = loaded_panel.curPlayer;
this.players = loaded_panel.players;
dice.panel.updateDiceButtons();
buildPanel();
scoresheet.panel.update_panel();
System.out.println("Loaded previous game!");
}
catch (IOException error) {
error.printStackTrace();
System.out.println("IO load error");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("class not found");
}
}
public void set_playable(boolean b){ play.setEnabled(b); }
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == roll){
dice.roll_dice();
turn++;
if(turn >= 3){
roll.setEnabled(false);
}
dice.panel.updateDiceButtons();
}
else if(e.getSource() == play){
System.out.println(curPlayer + " " + players + " " + round);
if ( curPlayer == players && round == 12){
EndGame();
}
else {
start_round();
}
scoresheet.play();
}
else if(e.getSource() == Yahtzee.load){
start_game(false);
load_data();
}
else if(e.getSource() == Yahtzee.save){
if(scoresheet != null){
save_data();
}
}
else if(e.getSource() == newGame){
start_game(true);
}
else if(e.getSource() == loadGame){
start_game(false);
load_data();
}
}
}