-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFEN.java
More file actions
210 lines (176 loc) · 7 KB
/
FEN.java
File metadata and controls
210 lines (176 loc) · 7 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* EN class reads and writes FEN notation to and from a LiteBoard state
*
*/
public class FEN {
public static void decode(LiteBoard board, String in) {
for (int ndx=0; ndx < LiteBoard.BOARD_SIZE; ndx++) {
board.board[ndx] = LiteUtil.makeSpot(LiteBoard.Empty, 0, false, false);
}
int ndx = 0;
int pos = 0;
char c;
while( ndx < LiteBoard.BOARD_SIZE && in.charAt(pos) != ' ') {
c = in.charAt(pos++);
switch (c) {
case 'p':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Pawn, Side.Black, true, false);
break;
case 'n':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Knight, Side.Black, true, false);
break;
case 'b':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Bishop, Side.Black, true, false);
break;
case 'r':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Rook, Side.Black, true, false);
break;
case 'q':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Queen, Side.Black, true, false);
break;
case 'k':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.King, Side.Black, true, false);
break;
case 'P':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Pawn, Side.White, true, false);
break;
case 'N':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Knight, Side.White, true, false);
break;
case 'B':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Bishop, Side.White, true, false);
break;
case 'R':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Rook, Side.White, true, false);
break;
case 'Q':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Queen, Side.White, true, false);
break;
case 'K':
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.King, Side.White, true, false);
break;
case '/':
assert(ndx % 8 == 0) : "Oops?\n";
break;
default:
if (c >= '0' && c <= '9') {
String s = "" + c;
int empty = Integer.valueOf(s);
while (empty-- > 0) {
board.board[ndx++] = LiteUtil.makeSpot(LiteBoard.Empty, 0, false, false);
}
}
break;
}
}
c = in.charAt(pos++);
if (c == ' ') c = in.charAt(pos++);
if (c == 'w') board.turn = Side.White;
else if (c == 'b') board.turn = Side.Black;
}
public static boolean read(LiteBoard board, String filename) {
boolean result = true;
String in = "";
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
in = reader.readLine();
reader.close();
} catch (IOException e) {
result = false;
}
decode(board, in);
return result;
}
public static String encode(LiteBoard board) {
String[] table = {" ","p","n","b","r","q","k"};
String placement = "";
int empty = 0;
for (int ndx=0; ndx < LiteBoard.BOARD_SIZE; ndx++) {
int type = board.getType(ndx);
int side = board.getSide(ndx);
if ((ndx > 0) && (ndx % 8) == 0) {
if (empty > 0) {
placement += empty;
empty = 0;
}
placement += "/";
}
switch (type) {
case LiteBoard.Empty:
empty++;
break;
case LiteBoard.Pawn:
case LiteBoard.Knight:
case LiteBoard.Bishop:
case LiteBoard.Rook:
case LiteBoard.Queen:
case LiteBoard.King:
if (empty > 0) {
placement += empty;
empty = 0;
}
placement += (side == Side.Black) ? table[type] : table[type].toUpperCase();
break;
}
}
if (empty > 0) {
placement += empty;
empty = 0;
}
placement += (board.turn == Side.White) ? " w " : " b ";
String castleWht = (castleQ(board, 7) ? "Q" : "") + (castleK(board, 7) ? "K" : "");
String castleBlk = (castleQ(board, 0) ? "q" : "") + (castleK(board, 0) ? "k" : "");
if (castleWht.isEmpty() && castleBlk.isEmpty())
placement += "-";
else
placement += castleWht + castleBlk;
placement += " - 0 0";
return placement;
}
public static boolean write(LiteBoard board, String filename) {
boolean result = true;
String placement = encode(board);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(placement);
writer.newLine();
writer.close();
} catch (IOException e) {
result = false;
}
return result;
}
private static boolean castleQ(LiteBoard board, int row) {
byte rQ = board.board[0 + row * 8];
byte nQ = board.board[1 + row * 8];
byte bQ = board.board[2 + row * 8];
byte qQ = board.board[3 + row * 8];
if (LiteUtil.getType(rQ) != LiteBoard.Rook) return false;
if (LiteUtil.hasMoved(rQ)) return false;
byte k = board.board[4 + row * 8];
if (LiteUtil.getType(k) != LiteBoard.King) return false;
if (LiteUtil.hasMoved(k)) return false;
if (LiteUtil.getType(nQ) != LiteBoard.Empty) return false;
if (LiteUtil.getType(bQ) != LiteBoard.Empty) return false;
if (LiteUtil.getType(qQ) != LiteBoard.Empty) return false;
return true;
}
private static boolean castleK(LiteBoard board, int row) {
byte rK = board.board[7 + row * 8];
byte nK = board.board[6 + row * 8];
byte bK = board.board[5 + row * 8];
if (LiteUtil.getType(rK) != LiteBoard.Rook) return false;
if (LiteUtil.hasMoved(rK)) return false;
byte k = board.board[4 + row * 8];
if (LiteUtil.getType(k) != LiteBoard.King) return false;
if (LiteUtil.hasMoved(k)) return false;
if (LiteUtil.getType(nK) != LiteBoard.Empty) return false;
if (LiteUtil.getType(bK) != LiteBoard.Empty) return false;
return true;
}
}