Add Game::is_dangerous() for Futility Pruning
[purplehaze.git] / src / init.cpp
blob23218e240848e9d87ef2029a4b4e002246a3d4cd
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/>.
17 #include <assert.h>
18 #include <iostream>
19 #include <sstream>
20 #include <string>
22 #include "game.h"
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);
34 // Initialize objects
35 board = Board();
36 pieces = Pieces();
37 tree = Tree();
39 // Parse board positions
40 Square s = A8;
41 std::string positions;
42 iss >> positions;
43 for (auto it = positions.begin(); it != positions.end(); ++it) {
44 char sq = *it;
45 if (sq == '/') {
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
50 Color c = WHITE;
51 PieceType t = EMPTY;
52 switch (sq) {
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);
67 add_piece(c, t, s);
68 s = Square(s + RIGHT); // Next square
71 assert(s == Square(H1 + RIGHT));
73 // Set the side to move
74 char side;
75 iss >> side;
76 assert(current_position().get_turn_color() == WHITE);
77 switch(side) {
78 case 'w': break;
79 case 'b': current_position().change_side(); break;
80 default: assert(!"FEN: no side to move!"); break;
83 std::string castling;
84 iss >> castling;
85 for (auto it = castling.begin(); it != castling.end(); ++it) {
86 switch(*it) {
87 case '-': break;
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;
95 std::string ep;
96 iss >> ep;
97 if (ep != "-") {
98 char file = ep.at(0);
99 char rank = ep.at(1);
100 s = Square((rank - '1') * 16 + file - 'a');
101 assert(!board.is_out(s));
102 current_position().set_en_passant(s);
105 int halfmove = 0;
106 iss >> halfmove;
107 current_position().set_halfmove(halfmove);
109 int fullmove = 1;
110 iss >> fullmove;
111 int ply = 2 * (fullmove - 1);
112 if (current_position().get_turn_color() == BLACK) ++ply;
113 current_position().set_ply(ply);