forked from lucasmartiniano6/mc322-projeto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
247 lines (218 loc) · 9.52 KB
/
Copy pathSquare.java
File metadata and controls
247 lines (218 loc) · 9.52 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
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
import java.util.ArrayList;
public class Square {
// Quadrado individual do tabuleiro
private static ArrayList<Square> selected = new ArrayList<>(); // Guarda os dois squares selecionados (em verde)
private static Square[][] squares = new Square[8][8]; // Grid dos squares (mesma ordem do tabuleiro)
private Tabuleiro tabuleiro;
private JButton button;
private String icon; // imagem do square (mesma label da peca)
public Square(int x, int y, String peca, Container panel, Tabuleiro tabuleiro){
this.tabuleiro = tabuleiro;
this.icon = peca;
int x_map = x/100;
int y_map = 7-(y/100);
if((x_map+y_map)%2 != 0){
// light background
if(Character.isUpperCase(peca.charAt(0)))
this.icon = "white/" + peca + "_light"; // peca branca
else
this.icon = "black/" + peca + "_light"; // peca preta
}
else{
// dark background
if(Character.isUpperCase(peca.charAt(0)))
this.icon = "white/" + peca + "_dark"; // peca branca
else
this.icon = "black/" + peca + "_dark"; // peca preta
}
if(peca.equals("dark"))
this.icon = "light";
else if(peca.equals("light"))
this.icon = "dark";
this.button = new JButton(new ImageIcon("imgs/" + icon + ".png"));
this.button.setBounds(x, y+25, 100, 100);
this.button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
action();}
});
panel.add(button);
// map squares in a grid
squares[x_map][y_map] = this;
}
public Square(int x, int y, String peca, Container panel){
this.icon = peca;
int x_map = x/100;
int y_map = 7-(y/100);
if((x_map+y_map)%2 != 0){
// light background
if(Character.isUpperCase(peca.charAt(0)))
this.icon = "white/" + peca + "_light"; // peca branca
else
this.icon = "black/" + peca + "_light"; // peca preta
}
else{
// dark background
if(Character.isUpperCase(peca.charAt(0)))
this.icon = "white/" + peca + "_dark"; // peca branca
else
this.icon = "black/" + peca + "_dark"; // peca preta
}
if(peca.equals("dark"))
this.icon = "light";
else if(peca.equals("light"))
this.icon = "dark";
this.button = new JButton(new ImageIcon("imgs/" + icon + ".png"));
this.button.setBounds(x, y+25, 100, 100);
panel.add(button);
// map squares in a grid
squares[x_map][y_map] = this;
}
public static void printarTabuleiro(Tabuleiro tabuleiro){
// Printar o tabuleiro no terminal
System.out.println("**************************");
for(int j = 7; j >= 0; j--){
System.out.print(j+1 + " ");
for(int i = 0; i < 8; i++){
Peca peca = tabuleiro.getPeca(i, j);
if(peca == null) System.out.print(" . ");
else System.out.print(" " + peca.getLabel() + " ");
}
System.out.println();
}
System.out.println(" A B C D E F G H");
System.out.println("**************************");
}
public static void setMovimento(Tabuleiro tabuleiro){
for(int x=0; x<8; x++){
for(int y=0; y<8; y++){
Peca peca = tabuleiro.getPeca(x, y);
Square square = squares[x][y];
if(peca == null){
// Caso não tenha peca no tabuleiro (icon = "dark" ou "light" de fundo)
// linha par
// coluna par -> light
// coluna impar -> dark
// linha impar
// coluna par -> dark
// coluna impar -> light
if((x+y)%2 != 0)
square.icon = "light";
else
square.icon = "dark";
square.button.setIcon(new ImageIcon("imgs/" + square.icon + ".png"));
}
else if(!peca.getLabel().equals(square.icon)){
// Caso tenha peca no tabuleiro e o icon do square não seja o mesmo da peca
square.icon = peca.getLabel();
if((x+y)%2 != 0){
if(Character.isUpperCase(peca.getLabel().charAt(0)))
square.icon = "white/" + square.icon + "_light";
else
square.icon = "black/" + square.icon + "_light";
}
else{
if(Character.isUpperCase(peca.getLabel().charAt(0)))
square.icon = "white/" + square.icon + "_dark";
else
square.icon = "black/" + square.icon + "_dark";
}
square.button.setIcon(new ImageIcon("imgs/" + square.icon + ".png"));
}
}
}
}
public void parSelected(){
// Caso tenha dois selecionados (em verde)
int x1 = (selected.get(0).button.getX() / 100) + 1;
int y1 = 8 - ((selected.get(0).button.getY() / 100));
int x2 = (selected.get(1).button.getX() / 100) + 1;
int y2 = 8 - ((selected.get(1).button.getY() / 100));
String letra = "ABCDEFGH";
String s1 = letra.substring(x1-1, x1) + y1;
String s2 = letra.substring(x2-1, x2) + y2;
if(tabuleiro.mover(s1, s2)){
// Movimento válido
// Iterar por todas as pecas do grid (Tabuleiro) e atualizar o icon dos squares (interface) de acordo
for(int x=0; x<8; x++){
for(int y=0; y<8; y++){
Peca peca = tabuleiro.getPeca(x, y);
Square square = squares[x][y];
if(peca == null){
// Caso não tenha peca no tabuleiro (icon = "dark" ou "light" de fundo)
// linha par
// coluna par -> light
// coluna impar -> dark
// linha impar
// coluna par -> dark
// coluna impar -> light
if((x+y)%2 == 0)
square.icon = "dark";
else
square.icon = "light";
square.button.setIcon(new ImageIcon("imgs/" + square.icon + ".png"));
}
else if(!peca.getLabel().equals(square.icon)){
// Caso tenha peca no tabuleiro e o icon do square não seja o mesmo da peca
square.icon = peca.getLabel();
if((x+y)%2 != 0){
if(Character.isUpperCase(peca.getLabel().charAt(0)))
square.icon = "white/" + square.icon + "_light";
else
square.icon = "black/" + square.icon + "_light";
}
else{
if(Character.isUpperCase(peca.getLabel().charAt(0)))
square.icon = "white/" + square.icon + "_dark";
else
square.icon = "black/" + square.icon + "_dark";
}
square.button.setIcon(new ImageIcon("imgs/" + square.icon + ".png"));
}
}
}
System.out.println(s1 + " " + s2);
printarTabuleiro(tabuleiro);
}
else
System.out.println("Movimento inválido");
}
public void action(){
if(selected.size() == 1){ // Caso tenha dois selecionados
for(Square s : selected){
// Desselecionar o square
if(s.icon.equals("dark_selected"))
s.icon = "dark";
else if(s.icon.equals("light_selected"))
s.icon = "light";
else
s.icon = s.icon.substring(0, s.icon.length() - 9);
s.button.setIcon(new ImageIcon("imgs/" + s.icon + ".png"));
}
selected.add(this);
parSelected(); // Verificar se o movimento é válido
selected.clear();
}
else {
// Selecionar o square (em verde) ou desselecionar
if(icon.equals("dark"))
icon = "dark_selected";
else if(icon.equals("light"))
icon = "light_selected";
else if(icon.equals("dark_selected"))
icon = "dark";
else if(icon.equals("light_selected"))
icon = "light";
else if(icon.length() == 1)
icon = icon + "_selected";
else if(icon.substring(icon.length() - 9, icon.length()).equals("_selected"))
icon = icon.substring(0, icon.length() - 9);
else
icon = icon + "_selected";
button.setIcon(new ImageIcon("imgs/" + icon + ".png"));
selected.add(this);
}
}
}