forked from lucasmartiniano6/mc322-projeto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFEN.java
More file actions
167 lines (152 loc) · 5.8 KB
/
Copy pathFEN.java
File metadata and controls
167 lines (152 loc) · 5.8 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FEN implements Data{
// Forsyth–Edwards Notation
// https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
// Usado para representar o estado do tabuleiro
// Exemplo de início: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
public String load(String filename){
try {
File file = new File(filename);
Scanner scanner = new Scanner(file);
String data = "";
while (scanner.hasNextLine()) {
data += scanner.nextLine();
}
scanner.close();
return data;
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filename);
return null;
}
}
// verifica se a peca na lista de pecas é a mesma peca sendo analisada na listaUsadas
public boolean verificaPeca(String posicao, Peca peca){
if(posicao.equals(peca.getPosicao())){
return true;
}
return false;
}
public boolean setBoard(String data, Tabuleiro tabuleiro){
// Internamente chamado por setBoardFromFEN para colocar as peças no tabuleiro
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
tabuleiro.getGrid()[i][j] = null;
String[] fields = data.split(" ");
String[] linhas = fields[0].split("/");
ArrayList<Peca> brancaJaUsada = new ArrayList<>();
ArrayList<Peca> pretaJaUsada = new ArrayList<>();
for(int i=0; i<linhas.length; i++){
String linha = linhas[i];
int posX = 0;
for(int j = 0; j < linha.length(); j++){
char c = linha.charAt(j);
if(Character.isDigit(c)){
int n = Character.getNumericValue(c);
for(int k = 0; k < n-1; k++){
posX++;
}
} else {
String posicao = Posicao.values()[posX].toString() + (8-i);
Peca peca = null;
if(Character.isUpperCase(c)){
for(int idx = 0; idx < tabuleiro.getBrancas().size(); idx++){
if(tabuleiro.getBrancas().get(idx).getLabel().equals(Character.toString(c))){
peca = tabuleiro.getBrancas().get(idx);
for(Peca p: brancaJaUsada){
if(p.equals(peca))
break;
}
brancaJaUsada.add(peca);
break;
}
}
} else {
for(int idx = 0; idx < tabuleiro.getPretas().size(); idx++){
if(tabuleiro.getPretas().get(idx).getLabel().equals(Character.toString(c))){
peca = tabuleiro.getPretas().get(idx);
for(Peca p: pretaJaUsada){
if(p.equals(peca))
break;
}
pretaJaUsada.add(peca);
break;
}
}
}
if(peca != null){
int x = Peca.getPosX(posicao);
int y = Peca.getPosY(posicao);
tabuleiro.getGrid()[x][y] = peca;
peca.setPosicao(posicao);
}
}
posX++;
}
}
return true;
}
public static String generateFen(Tabuleiro tabuleiro) {
String data = "";
for(int j=7; j>=0; j--){
String[] patterns = new String[8];
for(int i=0; i<8; i++){
Peca peca = tabuleiro.getPeca(i, j);
if(peca == null) patterns[i] = "";
else patterns[i] = peca.getLabel();
}
for(int i=0; i<8; i++){
int cnt = 0;
while(i< 8 && patterns[i] == ""){
i++;
cnt++;
}
if(cnt != 0) data += Integer.toString(cnt);
if(i >= 8) continue;
else data += patterns[i];
}
if(j>0) data += '/';
}
return data;
}
public File criarArquivoFen(){
String filename = "tabuleiro";
File fen = new File("fen/lastFens/" + filename + ".fen");
// Get the file
try{
if(!fen.exists()){
fen.createNewFile();
return fen;
} else {
int num = 1;
fen = new File("fen/lastFens/" + filename + Integer.toString(num) + ".fen" );
while(fen.exists()){
num++;
fen = new File("fen/lastFens/" + filename + Integer.toString(num) + ".fen" );
}
fen.createNewFile();
return fen;
}
}catch (Exception e) {
System.err.println(e);
return null;
}
}
public boolean save(String filename, Tabuleiro tabuleiro){
String data = generateFen(tabuleiro);
File fen = criarArquivoFen();
try {
FileWriter writer = new FileWriter(fen);
writer.write(data);
writer.close();
return true;
} catch (IOException e) {
System.out.println("Erro ao escrever arquivo");
return false;
}
}
}