-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.cpp
107 lines (87 loc) · 2.67 KB
/
Parser.cpp
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
/*
* File: Parser.cpp
* Author: pj
*
* Created on January 7, 2013, 11:03 PM
*/
#include "Parser.h"
class Board Parser::parse_fen(string fen){
Board board = Board();
int len = fen.length();
int pos = 0; // position in string
int square = A1;
// 8 rows of pieces
for(int row = 7; row >= 0; row--){
while(fen[pos] == '/') pos++;
for(int col = 0; col < 8; col++){
char c = fen[pos++];
// if number skip ahead that many columns
if (c >= '1' && c <= '8'){
col += c - '1';
} else { // find piece
U8 piece = parse_piece(c);
if(piece) board.add_piece(piece, (row << 3) | col);
}
}
}
while(fen[pos] != ' ') if (pos++ >= len) return board;
while(fen[pos] == ' ') if (pos++ >= len) return board;
// side to move
U8 side_to_move = Parser::side(fen[pos++]);
board.set_side_to_move(side_to_move);
while(fen[pos] == ' ') if (pos++ >= len) return board;
// castling rights
U8 rights = 0;
while(fen[pos++] != ' '){
rights |= Parser::castling_right(fen[pos]);
}
while(fen[pos] == ' ') if (pos++ >= len) return board;
// ep square
if(fen[pos] == '-') {
pos++;
} else {
char square[2] = {fen[pos], fen[pos+1]};
board.set_ep_square(Parser::square(square));
while(fen[pos] != ' ') if (pos++ >= len) return board;
}
while(fen[pos] == ' ') if (pos++ >= len) return board;
// half-move-count
int half_move_count = 0;
while(fen[pos] >= '0' && fen[pos] <= '9'){
half_move_count = half_move_count * 10 + (fen[pos] - '0');
if (pos++ >= len) return board;
}
board.set_half_move_count(half_move_count);
while(fen[pos] == ' ') if (pos++ >= len) return board;
// full-move-count
int full_move_count = 0;
while(fen[pos] != ' '){
full_move_count = full_move_count * 10 + (fen[pos] - '0');
if (pos++ >= len) return board;
}
return board;
}
U8 Parser::parse_piece(char piece){
for(int i = 2; i < 14; i++){
if(PIECE_CHARS[i] == piece) return i;
}
return EMPTY;
}
U8 Parser::side(char c){
if(c == 'b' || c == 'B') return BLACK;
return WHITE;
}
U8 Parser::castling_right(char c){
switch(c){
case 'K': return WHITE_KING_SIDE;
case 'Q': return WHITE_QUEEN_SIDE;
case 'k': return BLACK_KING_SIDE;
case 'q': return BLACK_QUEEN_SIDE;
}
return 0;
}
U8 Parser::square(char sq[]){
int col = sq[0] - 'a';
int row = sq[1] - '1';
return (row << 3) | col;
}