forked from lucasmartiniano6/mc322-projeto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRei.java
More file actions
142 lines (137 loc) · 7.26 KB
/
Copy pathRei.java
File metadata and controls
142 lines (137 loc) · 7.26 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
public class Rei extends Peca{
public Rei(String corDono, Tabuleiro tabuleiro, String posicao){
super(corDono, tabuleiro, posicao);
if(corDono.equals("branca"))
this.setLabel("K");
else
this.setLabel("k");
}
public boolean isReachable(String destino, boolean test) {
// Implementação do movimento do Rei
int deltaX = Math.abs(getPosX(this.getPosicao()) - getPosX(destino));
int deltaY = Math.abs(getPosY(this.getPosicao()) - getPosY(destino));
int nx = getPosX(destino);
int ny = getPosY(destino);
if(nx < 0 || nx >= 8 || ny < 0 || ny >= 8) // Posição do grid inválida
return false;
if(this.getTabuleiro().getPeca(nx, ny) != null){ // Caso a posição não esteja vazia
if(!this.getTabuleiro().getPeca(nx, ny).getCorDono().equals(this.getCorDono()) && deltaX <= 1 && deltaY <= 1){ // Peça inimiga
return true;
}
else
return false;
}
else { // Caso a posição esteja vazia
if(this.getMovimentos() == 0 && deltaX == 2 && deltaY == 0) { // Implementação do roque
if(this.getCorDono().equals("branca")) {
if(destino.equals("G1")) { // Roque menor do Rei das brancas
if(this.getTabuleiro().getPeca("F1") == null && this.getTabuleiro().getPeca("G1") == null) { //Checa se não há nenhuma peça entre o Rei e a Torre
if(this.getTabuleiro().getPeca("H1") instanceof Torre && this.getTabuleiro().getPeca("H1").getMovimentos() == 0) {
for(Peca peca : getTabuleiro().getPretas()) {
if(peca.isReachable("F1", true)) {
return false;
}
}
this.getTabuleiro().getPeca("H1").moverPeca("F1");
return true;
}
}
}
else if(destino.equals("C1")) { // Roque maior do Rei das brancas
if(this.getTabuleiro().getPeca("B1") == null && this.getTabuleiro().getPeca("C1") == null && this.getTabuleiro().getPeca("D1") == null) { //Checa se não há nenhuma peça entre o Rei e a Torre
if(this.getTabuleiro().getPeca("A1") instanceof Torre && this.getTabuleiro().getPeca("A1").getMovimentos() == 0) {
for(Peca peca : getTabuleiro().getPretas()) {
if(peca.isReachable("D1", true)) {
return false;
}
}
this.getTabuleiro().getPeca("A1").moverPeca("D1");
return true;
}
}
}
}
else if(this.getCorDono().equals("preta")) {
if(destino.equals("G8")) { // Roque menor do Rei das pretas
if(this.getTabuleiro().getPeca("F8") == null && this.getTabuleiro().getPeca("G8") == null) { //Checa se não há nenhuma peça entre o Rei e a Torre
if(this.getTabuleiro().getPeca("H8") instanceof Torre && this.getTabuleiro().getPeca("H8").getMovimentos() == 0) {
for(Peca peca : getTabuleiro().getBrancas()) {
if(peca.isReachable("F8", true)) {
return false;
}
}
this.getTabuleiro().getPeca("H8").moverPeca("F8");
return true;
}
}
}
else if(destino.equals("C8")){ // Roque maior do Rei das pretas
if(this.getTabuleiro().getPeca("B8") == null && this.getTabuleiro().getPeca("C8") == null && this.getTabuleiro().getPeca("D8") == null) { //Checa se não há nenhuma peça entre o Rei e a Torre
if(this.getTabuleiro().getPeca("A8") instanceof Torre && this.getTabuleiro().getPeca("A8").getMovimentos() == 0) {
for(Peca peca : getTabuleiro().getBrancas()) {
if(peca.isReachable("D8", true)) {
return false;
}
}
this.getTabuleiro().getPeca("A8").moverPeca("D8");
return true;
}
}
}
}
return false;
}
else if(deltaX > 1 || deltaY > 1) // Fora do alcance do Rei
return false;
return true;
}
}
@Override
public void undoMove(String lastPos, String destino, boolean comeu){
// Internamente chamado por moverPeca quando o movimento é inválido
// Coloca a peça em lastPos
setMovimentos(getMovimentos()-1);
if(Math.abs(getPosX(lastPos) - getPosX(destino)) > 1) { // Verifica se a tentativa de roque é inválida
String pos = "";
String font = "";
if(getPosY(lastPos) == 0) {
if(getPosX(destino) == 2) {
pos = "A1";
font = "D1";
} else if(getPosX(destino) == 6) {
pos = "H1";
font = "F1";
}
getTabuleiro().setPeca(pos, getTabuleiro().getPeca(font));
getTabuleiro().getPeca(pos).setMovimentos(getTabuleiro().getPeca(pos).getMovimentos() - 1);
getTabuleiro().setPeca("E1", this);
} else if(getPosY(lastPos) == 7) {
if(getPosX(destino) == 2) {
pos = "A8";
font = "D8";
} else if(getPosX(destino) == 6) {
pos = "H8";
font = "F8";
}
getTabuleiro().setPeca(pos, getTabuleiro().getPeca(font));
getTabuleiro().getPeca(pos).setMovimentos(getTabuleiro().getPeca(pos).getMovimentos() - 1);
getTabuleiro().setPeca("E8", this);
}
} else { // verifica se o movimento do rei é inválido
if(comeu) {
Peca pecaRemov = getTabuleiro().getPecasComidas().get(getTabuleiro().getPecasComidas().size()-1);
if(pecaRemov.getCorDono().equals("branca")) {
getTabuleiro().addBrancas(pecaRemov);
} else if(pecaRemov.getCorDono().equals("preta")) {
getTabuleiro().addPretas(pecaRemov);
}
getTabuleiro().setPeca(lastPos, this);
getTabuleiro().setPeca(destino, pecaRemov);
getTabuleiro().getPecasComidas().remove(pecaRemov);
} else {
getTabuleiro().setEmpty(destino);
getTabuleiro().setPeca(lastPos, this);
}
}
}
}