-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRow.java
More file actions
291 lines (236 loc) · 6.9 KB
/
Row.java
File metadata and controls
291 lines (236 loc) · 6.9 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
package P0600;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
@SuppressWarnings("serial")
/**
* Créée unev ligne pour que le joueur devine
*
* @author Nathan Antonietti
*/
public class Row extends JPanel {
public boolean isLocked;
public Color[] resColors = new Color[4];
JPanel pnlButtons, pnlHints;
DoneButton btnDone;
ColorButton btn1, btn2, btn3, btn4;
JButton btnHint1, btnHint2, btnHint3, btnHint4;
// private int colorindex;
private Color[] hintColors = { Color.black, Color.white };
private Color brown;
private int inArray = 0;
private int inGoodPos = 0;
public Row() {
isLocked = true;
initGUI();
}
public Row(boolean isLocked) {
this.isLocked = isLocked;
initGUI();
}
private void initGUI() {
brown = new Color(139, 69, 19);
this.setLayout(new FlowLayout());
pnlButtons = new JPanel();
pnlHints = new JPanel();
pnlButtons.setLayout(new FlowLayout());
pnlHints.setLayout(new GridLayout(2, 2));
btnDone = new DoneButton(this);
btn1 = new ColorButton();
btn2 = new ColorButton();
btn3 = new ColorButton();
btn4 = new ColorButton();
btnDone.setPreferredSize(new Dimension(70, 30));
btn1.setPreferredSize(new Dimension(50, 50));
btn2.setPreferredSize(new Dimension(50, 50));
btn3.setPreferredSize(new Dimension(50, 50));
btn4.setPreferredSize(new Dimension(50, 50));
btnDone.addActionListener(new DoneButton(this));
btn1.addActionListener(new ColorButton());
btn2.addActionListener(new ColorButton());
btn3.addActionListener(new ColorButton());
btn4.addActionListener(new ColorButton());
btnHint1 = new JButton();
btnHint2 = new JButton();
btnHint3 = new JButton();
btnHint4 = new JButton();
btnHint1.setPreferredSize(new Dimension(25, 25));
btnHint2.setPreferredSize(new Dimension(25, 25));
btnHint3.setPreferredSize(new Dimension(25, 25));
btnHint4.setPreferredSize(new Dimension(25, 25));
btnHint1.setBackground(Color.gray);
btnHint2.setBackground(Color.gray);
btnHint3.setBackground(Color.gray);
btnHint4.setBackground(Color.gray);
pnlButtons.setBackground(brown);
this.setBackground(brown);
pnlButtons.add(btnDone);
pnlButtons.add(btn1);
pnlButtons.add(btn2);
pnlButtons.add(btn3);
pnlButtons.add(btn4);
pnlHints.add(btnHint1);
pnlHints.add(btnHint2);
pnlHints.add(btnHint3);
pnlHints.add(btnHint4);
this.add(pnlButtons);
this.add(pnlHints);
LockButtons();
}
/**
* @return Retourne la ligne actuel
*/
public Row getRow() {
return this;
}
private void LockButtons() {
if (isLocked) {
// For each button in row
for (int i = 0; i < pnlButtons.getComponentCount(); i++) {
pnlButtons.getComponent(i).setEnabled(false);
}
// For each button in hints
for (int i = 0; i < pnlHints.getComponentCount(); i++) {
pnlHints.getComponent(i).setEnabled(false);
}
}
}
private void UnLockButtons() {
if (isLocked) {
// For each button in row
for (int i = 0; i < pnlButtons.getComponentCount(); i++) {
pnlButtons.getComponent(i).setEnabled(true);
}
// For each button in hints
for (int i = 0; i < pnlHints.getComponentCount(); i++) {
pnlHints.getComponent(i).setEnabled(true);
}
}
}
/**
* Active la prochaine ligne si la réponse est fausse
*/
public void UnlockNextRow() {
Color[] guessedColors = new Color[4];
guessedColors = getGuessedColors();
// if is correct guess
if (isEqual(guessedColors)) {
JOptionPane.showMessageDialog(this, "Bravo! Vous avez gagné.");
SwingUtilities.getWindowAncestor(this).setVisible(false);
P0605 newGame = new P0605();
newGame.setExtendedState(JFrame.MAXIMIZED_BOTH);
newGame.setVisible(true);
System.out.println("WIN !!!");
} else {
setHints(guessedColors);
System.out.println("Next row");
P0605 frame = (P0605) SwingUtilities.getWindowAncestor(this);
Row[] rows = frame.getRows();
isLocked = true;
// if on last row !!!!
if (!(P0605.currentRowIndex >= P0605.numberRows)) {
System.out.println("Locked next row");
rows[P0605.currentRowIndex].LockButtons();
P0605.currentRowIndex++;
if (P0605.currentRowIndex < P0605.numberRows) {
System.out.println("Unlocked next row");
rows[P0605.currentRowIndex].UnLockButtons();
}
else {
loseGame();
}
} else {
loseGame();
}
}
}
/**
* Récupère les couleurs devinées
*
* @return les couleurs
*/
private Color[] getGuessedColors() {
Color[] guessedColors = new Color[4];
for (int i = 0; i < guessedColors.length; i++) {
guessedColors[i] = pnlButtons.getComponent(i + 1).getBackground();
}
return guessedColors;
}
private void loseGame() {
P0605.btnres1.setBackground(P0605.resultColors[0]);
P0605.btnres2.setBackground(P0605.resultColors[1]);
P0605.btnres3.setBackground(P0605.resultColors[2]);
P0605.btnres4.setBackground(P0605.resultColors[3]);
JOptionPane.showMessageDialog(this, "Bravo! Vous avez perdu.");
SwingUtilities.getWindowAncestor(this).setVisible(false);
P0605 newGame = new P0605();
newGame.setExtendedState(JFrame.MAXIMIZED_BOTH);
newGame.setVisible(true);
System.out.println("PERDU !!!");
}
/**
* Ajoute les indices
*
* @param guessColors
*/
private void setHints(Color[] guessColors) {
int index = 0;
for (Color c : guessColors) {
for (Color c2 : P0605.resultColors) {
// si la couleur est dans le code final
if (c == c2) {
if(c == P0605.resultColors[index]) {
inGoodPos++;
} else {
inArray++;
}
}
}
index++;
}
setBlackColor();
setWhiteColor();
}
private void setBlackColor() {
for (int i = 0; i < inGoodPos; i++) {
if (btnHint1.getBackground() == Color.gray) {
btnHint1.setBackground(hintColors[0]);
} else if (btnHint2.getBackground() == Color.gray) {
btnHint2.setBackground(hintColors[0]);
} else if (btnHint3.getBackground() == Color.gray) {
btnHint3.setBackground(hintColors[0]);
} else if (btnHint4.getBackground() == Color.gray) {
btnHint4.setBackground(hintColors[0]);
}
}
}
private void setWhiteColor() {
for (int i = 0; i < inArray; i++) {
if (btnHint1.getBackground() == Color.gray) {
btnHint1.setBackground(hintColors[1]);
} else if (btnHint2.getBackground() == Color.gray) {
btnHint2.setBackground(hintColors[1]);
} else if (btnHint3.getBackground() == Color.gray) {
btnHint3.setBackground(hintColors[1]);
} else if (btnHint4.getBackground() == Color.gray) {
btnHint4.setBackground(hintColors[1]);
}
}
}
/**
* Test si le joueur a gagné
*
* @param guessColors les couleurs du joueurs
* @return vrai ou faux si le joueur gagne ou pas
*/
private boolean isEqual(Color[] guessColors) {
if (P0605.resultColors[0] == guessColors[0] && P0605.resultColors[1] == guessColors[1]
&& P0605.resultColors[2] == guessColors[2] && P0605.resultColors[3] == guessColors[3]) {
return true;
} else {
return false;
}
}
}