Remove unused part of Search code
[purplehaze.git] / src / protocol.cpp
blob94263471f5797bbeacf2ea940655cc7b643eb3bc
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 <cassert>
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(int time)
51 game.time.set_remaining(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)) {
66 return Move();
68 Color c = game.current_position().side();
69 MoveType t = QUIET_MOVE;
71 if (move.size() == 5) { // Promotion
72 switch (move[4]) {
73 case 'n':
74 t = KNIGHT_PROMOTION;
75 break;
76 case 'b':
77 t = BISHOP_PROMOTION;
78 break;
79 case 'r':
80 t = ROOK_PROMOTION;
81 break;
82 case 'q':
83 t = QUEEN_PROMOTION;
84 break;
85 default:
86 return Move();
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);
105 // Capture
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));
110 } else {
111 return Move(from, to, t);
115 bool Protocol::play_move(std::string move)
117 // Parse move
118 Move m = parse_move(move);
120 // Test legality
121 if (!game.is_legal(m)) {
122 return false;
125 // Play move
126 game.make_move(m);
128 // Put move to history
129 history.push(m);
131 return true;
134 bool Protocol::undo_move()
136 if (history.empty()) {
137 return false;
139 Move m = history.top();
140 history.pop();
142 // Undo move
143 game.undo_move(m);
144 return true;
147 std::string Protocol::search_move(bool use_san_notation)
149 Move m = game.root(depth + 1);
150 if (m.is_null()) {
151 if (game.is_check(game.current_position().side())) {
152 return "LOST";
154 return "DRAW";
155 } else {
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())) {
160 res += "+";
162 undo_move();
163 return res;
165 return m.to_string();