1 /* Copyright (C) 2007-2011 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
53 case 'p': c
= BLACK
, t
= PAWN
; break;
54 case 'n': c
= BLACK
, t
= KNIGHT
; break;
55 case 'b': c
= BLACK
, t
= BISHOP
; break;
56 case 'r': c
= BLACK
, t
= ROOK
; break;
57 case 'q': c
= BLACK
, t
= QUEEN
; break;
58 case 'k': c
= BLACK
, t
= KING
; break;
59 case 'P': c
= WHITE
, t
= PAWN
; break;
60 case 'N': c
= WHITE
, t
= KNIGHT
; break;
61 case 'B': c
= WHITE
, t
= BISHOP
; break;
62 case 'R': c
= WHITE
, t
= ROOK
; break;
63 case 'Q': c
= WHITE
, t
= QUEEN
; break;
64 case 'K': c
= WHITE
, t
= KING
; break;
65 default: assert(false);
68 s
= Square(s
+ RIGHT
); // Next square
71 assert(s
== Square(H1
+ RIGHT
));
73 // Set the side to move
76 assert(current_position().get_turn_color() == WHITE
);
79 case 'b': current_position().change_side(); break;
80 default: assert(!"FEN: no side to move!"); break;
85 for (auto it
= castling
.begin(); it
!= castling
.end(); ++it
) {
88 case 'K': current_position().set_castle_right(WHITE
, KING
); break;
89 case 'Q': current_position().set_castle_right(WHITE
, QUEEN
); break;
90 case 'k': current_position().set_castle_right(BLACK
, KING
); break;
91 case 'q': current_position().set_castle_right(BLACK
, QUEEN
); break;
100 s
= Square((rank
- '1') * 16 + file
- 'a');
101 assert(!board
.is_out(s
));
102 current_position().set_en_passant(s
);
107 current_position().set_halfmove(halfmove
);
111 int ply
= 2 * (fullmove
- 1);
112 if (current_position().get_turn_color() == BLACK
) ++ply
;
113 current_position().set_ply(ply
);