Fix gtest dependency for Travis CI
[purplehaze.git] / src / init.cpp
blob92060944658dddb653bfcc82cc9057d95da22f33
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/>.
17 #include <cassert>
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':
54 c = BLACK;
55 t = PAWN;
56 break;
57 case 'n':
58 c = BLACK;
59 t = KNIGHT;
60 break;
61 case 'b':
62 c = BLACK;
63 t = BISHOP;
64 break;
65 case 'r':
66 c = BLACK;
67 t = ROOK;
68 break;
69 case 'q':
70 c = BLACK;
71 t = QUEEN;
72 break;
73 case 'k':
74 c = BLACK;
75 t = KING;
76 break;
77 case 'P':
78 c = WHITE;
79 t = PAWN;
80 break;
81 case 'N':
82 c = WHITE;
83 t = KNIGHT;
84 break;
85 case 'B':
86 c = WHITE;
87 t = BISHOP;
88 break;
89 case 'R':
90 c = WHITE;
91 t = ROOK;
92 break;
93 case 'Q':
94 c = WHITE;
95 t = QUEEN;
96 break;
97 case 'K':
98 c = WHITE;
99 t = KING;
100 break;
101 default:
102 assert(false);
104 add_piece(c, t, s);
105 s = Square(s + RIGHT); // Next square
108 assert(s == Square(H1 + RIGHT));
110 // Set the side to move
111 char side;
112 iss >> side;
113 assert(current_position().side() == WHITE);
114 switch(side) {
115 case 'w':
116 break;
117 case 'b':
118 current_position().change_side();
119 break;
120 default:
121 assert(false);
122 break;
125 std::string castling;
126 iss >> castling;
127 for (auto it = castling.begin(); it != castling.end(); ++it) {
128 switch(*it) {
129 case 'K':
130 current_position().set_castle_right(WHITE, KING);
131 break;
132 case 'Q':
133 current_position().set_castle_right(WHITE, QUEEN);
134 break;
135 case 'k':
136 current_position().set_castle_right(BLACK, KING);
137 break;
138 case 'q':
139 current_position().set_castle_right(BLACK, QUEEN);
140 break;
141 case '-':
142 break;
146 std::string ep;
147 iss >> ep;
148 if (ep != "-") {
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);
156 int halfmove = 0;
157 iss >> halfmove;
158 current_position().set_halfmove(halfmove);
160 int fullmove = 1;
161 iss >> fullmove;
162 int ply = 2 * (fullmove - 1);
163 if (current_position().side() == BLACK) {
164 ++ply;
166 tree.set_ply(ply);