Overload Board's subscript operator
[purplehaze.git] / src / protocol.cpp
blobf3cebf86470446c4471bed7d7627b148e5ff9425
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>
20 #include "protocol.h"
22 void Protocol::new_game()
24 game.tt.clear();
25 game.clear_killers();
26 game.search_moves.clear();
29 bool Protocol::set_board(std::string fen)
31 // TODO Test if fen is valid?
33 // Load fen
34 game.init(fen);
36 return true;
40 * Set time control:
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);
46 return true;
49 bool Protocol::set_remaining_time(int time)
51 game.time.set_remaining_time(time);
52 return true;
55 bool Protocol::set_output_thinking(bool ot)
57 game.output_thinking = ot;
58 return true;
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
70 switch (move[4]) {
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);
95 // Capture
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));
100 } else {
101 return Move(from, to, t);
105 bool Protocol::play_move(std::string move)
107 // Parse move
108 Move m = parse_move(move);
110 // Test legality
111 if (!game.is_legal(m)) return false;
113 // Play move
114 game.make_move(m);
116 // Put move to history
117 history.push(m);
119 return true;
122 bool Protocol::undo_move()
124 if (history.empty()) return false;
125 Move m = history.top();
126 history.pop();
128 // Undo move
129 game.undo_move(m);
130 return true;
133 std::string Protocol::search_move(bool use_san_notation)
135 Move m = game.root(depth + 1);
136 if (m.is_null()) {
137 if (game.is_check(game.current_position().get_turn_color())) {
138 return "LOST";
140 return "DRAW";
141 } else {
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())) {
146 res += "+";
148 undo_move();
149 return res;
151 return m.to_string();