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_time(int time
)
51 game
.time
.set_remaining_time(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
)) return Move();
66 Color c
= game
.current_position().get_turn_color();
67 MoveType t
= QUIET_MOVE
;
69 if (move
.size() == 5) { // Promotion
71 case 'n': t
= KNIGHT_PROMOTION
; break;
72 case 'b': t
= BISHOP_PROMOTION
; break;
73 case 'r': t
= ROOK_PROMOTION
; break;
74 case 'q': t
= QUEEN_PROMOTION
; break;
75 default: return Move();
78 if (game
.board
[from
].type() == PAWN
) {
79 if (game
.board
.is_pawn_begin(c
, from
) &&
80 to
== Square(from
+ 2 * PAWN_PUSH_DIRS
[c
])) {
81 return Move(from
, to
, DOUBLE_PAWN_PUSH
);
82 } else if (game
.board
.is_empty(to
) &&
83 to
!= Square(from
+ PAWN_PUSH_DIRS
[c
])) {
84 return Move(from
, to
, EN_PASSANT
);
87 if (game
.board
[from
].type() == KING
) {
88 if (to
== Square(from
+ RIGHT
+ RIGHT
)) {
89 return Move(from
, to
, KING_CASTLE
);
90 } else if (to
== Square(from
+ LEFT
+ LEFT
)) {
91 return Move(from
, to
, QUEEN_CASTLE
);
96 if (!game
.board
.is_empty(to
)) {
97 assert((t
== QUIET_MOVE
) ||
98 (KNIGHT_PROMOTION
<= t
&& t
<= QUEEN_PROMOTION
));
99 return Move(from
, to
, static_cast<MoveType
>(t
+ CAPTURE
));
101 return Move(from
, to
, t
);
105 bool Protocol::play_move(std::string move
)
108 Move m
= parse_move(move
);
111 if (!game
.is_legal(m
)) return false;
116 // Put move to history
122 bool Protocol::undo_move()
124 if (history
.empty()) return false;
125 Move m
= history
.top();
133 std::string
Protocol::search_move(bool use_san_notation
)
135 Move m
= game
.root(depth
+ 1);
137 if (game
.is_check(game
.current_position().get_turn_color())) {
142 if (use_san_notation
) {
143 std::string res
= game
.output_move(m
);
144 play_move(m
.to_string());
145 if (game
.is_check(game
.current_position().get_turn_color())) {
151 return m
.to_string();