1 /* Copyright (C) 2007-2012 Vincent Ollivier
3 * Purple Haze is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Purple Haze is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Initialise the game according to a FEN record.
26 * For example the starting position in chess is:
27 * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
29 void Game::init(std::string fen
)
31 assert(fen
.length() > 0);
32 std::istringstream
iss(fen
);
39 // Parse board positions
41 std::string positions
;
43 for (auto it
= positions
.begin(); it
!= positions
.end(); ++it
) {
46 s
= Square(s
+ DOWN
+ 8 * LEFT
); // New rank
47 } else if ('1' <= sq
&& sq
<= '8') { // Empty squares
48 s
= Square(s
+ sq
- '1' + 1); // Next square
49 } else { // Non empty square
105 s
= Square(s
+ RIGHT
); // Next square
108 assert(s
== Square(H1
+ RIGHT
));
110 // Set the side to move
113 assert(current_position().side() == WHITE
);
118 current_position().change_side();
125 std::string castling
;
127 for (auto it
= castling
.begin(); it
!= castling
.end(); ++it
) {
130 current_position().set_castle_right(WHITE
, KING
);
133 current_position().set_castle_right(WHITE
, QUEEN
);
136 current_position().set_castle_right(BLACK
, KING
);
139 current_position().set_castle_right(BLACK
, QUEEN
);
149 char file
= ep
.at(0);
150 char rank
= ep
.at(1);
151 s
= Square((rank
- '1') * 16 + file
- 'a');
152 assert(!board
.is_out(s
));
153 current_position().set_en_passant(s
);
158 current_position().set_halfmove(halfmove
);
162 int ply
= 2 * (fullmove
- 1);
163 if (current_position().side() == BLACK
) {