-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalP.java
More file actions
222 lines (183 loc) · 6.01 KB
/
Copy pathFinalP.java
File metadata and controls
222 lines (183 loc) · 6.01 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
//Bret Owens - bto14
//Alex Sumner - acs14k
//imports for functionality
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
public class FinalP {
public static int x; //Number of players
public static int roll; //Number of rolls per turn
public static Player temp; //temp player var
public static void main( String args[] )
{
Yahtzee yFrame = new Yahtzee(); //Main frame for game
try //Input for number of players
{String s = JOptionPane.showInputDialog("WARNING: GAME MAY LAG A BIT\nInput number of players.");
x = Integer.parseInt(s);
}
catch(Exception e) //Exception for invalid input for num of players
{
JOptionPane.showMessageDialog(yFrame, "Invalid input for number of players!");
System.exit(0);
}
Player [] players = new Player[x]; //Array of players
for(int i = 0; i < x; i++)
{
players[i] = new Player(i);
}
roll = 1; //current roll
int z = 0; //current turn
temp = players[z]; //temp player
int turns = x * 14; //Number of turns in game
yFrame.setTitle("Yahtzee - Player " + temp.PLnum ); //changes for each players turn
yFrame.setSize( 1200, 750 );
JPanel top_bar = new JPanel(); //holds the GUI for all dice
top_bar.setPreferredSize(new Dimension(yFrame.getWidth(), (int)(yFrame.getWidth() / 6)));
GridLayout experimentLayout = new GridLayout(1,5);
experimentLayout.setVgap(50); //gap between di
experimentLayout.setHgap(50);
top_bar.setLayout(experimentLayout);
JDice [] DI = new JDice [5]; //Array of di
for(int i = 0; i < 5; i++)
{
DI[i] = new JDice();
DI[i].setBackground(Color.WHITE);
DI[i].setBorder(BorderFactory.createLineBorder(Color.black));
top_bar.add(DI[i]);
}
temp.check(DI[0].value() + DI[1].value() + DI[2].value() + DI[3].value() + DI[4].value()); //checks to see what combo is valid
yFrame.add(top_bar, BorderLayout.NORTH);
for(int i = x - 1; i >= 0; i--)
{
yFrame.add(players[i].chart, BorderLayout.CENTER); //Chart for every player
players[i].chart.setVisible(false); //All set to invisible
}
temp.chart.setVisible(true); //Sets first players chart to visible
JButton b1 = new JButton("Reroll Dice"); //Button for rerolling dice
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean rolled = false;
for(int i = 0; i < 5; i++)//Rerolls selected di
{
if(DI[i].isSelected())
{DI[i].roll();
DI[i].set();
rolled = true;
}
}
if(rolled)
{
roll++;
}
temp.check(DI[0].value() + DI[1].value() + DI[2].value() + DI[3].value() + DI[4].value());
}
});
yFrame.add(b1, BorderLayout.SOUTH); //Add button to frame
yFrame.setResizable(false); //Make frame a set size
yFrame.setBackground(Color.WHITE);
yFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
yFrame.setVisible( true );
// set frame size
// display frame
int y = 0;
while( y < turns) //loop for turns between users
{
System.out.print(roll);
if(roll == 3) //If current player is out of rolls
{
b1.setEnabled(false);
}
else if( roll == 4 && z < players.length - 1)//if current players turn ended and not at end of player array
{
temp.chart.setVisible(false); //set current chart to invisible
z++; //increment counter to player array
temp = players[z]; //temp is new player
yFrame.add(temp.chart, BorderLayout.CENTER);
temp.chart.setVisible(true);
yFrame.setTitle("Yahtzee - Player " + temp.PLnum );
for(int i = 0; i <5; i++)
{
DI[i].roll();
}
temp.check(DI[0].value() + DI[1].value() + DI[2].value() + DI[3].value() + DI[4].value());
roll = 1;
y++;
b1.setEnabled(true);
}
else if(roll == 4 && z == players.length - 1) //If at end of array go to beginning
{
temp.chart.setVisible(false);
z = 0;
temp = players[z];
temp.chart.setVisible(true);
yFrame.setTitle("Yahtzee - Player " + temp.PLnum );
for(int i = 0; i <5; i++)
{
DI[i].roll();
}
temp.check(DI[0].value() + DI[1].value() + DI[2].value() + DI[3].value() + DI[4].value());
roll = 1;
y++;
b1.setEnabled(true);
}
}
for(int i = x - 1; i >= 0; i--) //disable GUI
{
players[i].chart.setVisible(false);
}
b1.setVisible(false);
top_bar.setVisible(false);
//Start calculating Winner
int calc = 0;
int num = 0;
ArrayList<Integer> play = new ArrayList<Integer>();
ArrayList<Integer> play2 = new ArrayList<Integer>();
players[0].calc_final();
calc = players[0].FScore;
num = players[0].PLnum;
play.add(calc);
play2.add(num);
for(int i = 1; i < x; i++)
{
players[i].calc_final();
if(players[i].FScore > calc)
{
calc = players[i].FScore;
play.clear();
play2.clear();
play.add(calc);
play2.add(num);
}
else if(players[i].FScore == calc)
{
play.add(calc);
play2.add(num);
}
}
//Display winner(s)
if(play.size() == 1)
{
JOptionPane.showMessageDialog(yFrame, "Player " + play2.get(0) + " wins with a score of " + play.get(0) + "!");
System.exit(0);
}
else if(play.size() > 1)
{
String text = "It is a ties between:\n";
for(int i = 0; i < play.size(); i++)
{
text += "Player " + play2.get(i) + " with a score of " + play.get(i);
}
JOptionPane.showMessageDialog(yFrame, text);
System.exit(0);
}
}
}