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/>.
22 void Protocol::new_game()
26 game
.search_moves
.clear();
29 bool Protocol::set_board(std::string fen
)
31 // TODO Test if fen is valid?
41 * 'n' moves in 't' time with time in seconds.
43 bool Protocol::set_time(int moves
, int time
)
45 game
.time
= Time(moves
, time
* 100);
49 bool Protocol::set_remaining(int time
)
51 game
.time
.set_remaining(time
);
55 bool Protocol::set_output_thinking(bool ot
)
57 game
.output_thinking
= ot
;
61 Move
Protocol::parse_move(std::string move
)
63 Square from
= Square(move
[0] - 'a' + 16 * (move
[1] - '1'));
64 Square to
= Square(move
[2] - 'a' + 16 * (move
[3] - '1'));
65 if (game
.board
.is_out(from
) || game
.board
.is_out(to
)) {
68 Color c
= game
.current_position().side();
69 MoveType t
= QUIET_MOVE
;
71 if (move
.size() == 5) { // Promotion
89 if (game
.board
[from
].is(PAWN
)) {
90 const Direction d
= PAWN_PUSH_DIRS
[c
];
91 if (Board::is_pawn_begin(c
, from
) && to
== Square(from
+ 2 * d
)) {
92 return Move(from
, to
, DOUBLE_PAWN_PUSH
);
93 } else if (game
.board
.is_empty(to
) && to
!= Square(from
+ d
)) {
94 return Move(from
, to
, EN_PASSANT
);
97 if (game
.board
[from
].is(KING
)) {
98 if (to
== Square(from
+ 2 * RIGHT
)) {
99 return Move(from
, to
, KING_CASTLE
);
100 } else if (to
== Square(from
+ 2 * LEFT
)) {
101 return Move(from
, to
, QUEEN_CASTLE
);
106 if (!game
.board
.is_empty(to
)) {
107 assert((t
== QUIET_MOVE
) ||
108 (KNIGHT_PROMOTION
<= t
&& t
<= QUEEN_PROMOTION
));
109 return Move(from
, to
, static_cast<MoveType
>(t
+ CAPTURE
));
111 return Move(from
, to
, t
);
115 bool Protocol::play_move(std::string move
)
118 Move m
= parse_move(move
);
121 if (!game
.is_legal(m
)) {
128 // Put move to history
134 bool Protocol::undo_move()
136 if (history
.empty()) {
139 Move m
= history
.top();
147 std::string
Protocol::search_move(bool use_san_notation
)
149 Move m
= game
.root(depth
+ 1);
151 if (game
.is_check(game
.current_position().side())) {
156 if (use_san_notation
) {
157 std::string res
= game
.output_move(m
);
158 play_move(m
.to_string());
159 if (game
.is_check(game
.current_position().side())) {
165 return m
.to_string();