]> git.sesse.net Git - stockfish/blob - src/notation.cpp
d167c2ff446208c5b0b534a2b2c2971518bbd9b1
[stockfish] / src / notation.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <cassert>
21 #include <sstream>
22
23 #include "movegen.h"
24 #include "position.h"
25 #include "uci.h"
26
27 using namespace std;
28
29 static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
30
31
32 /// score_to_uci() converts a Value to a string suitable for use with the UCI
33 /// protocol specifications:
34 ///
35 /// cp <x>     The score from the engine's point of view in centipawns.
36 /// mate <y>   Mate in y moves, not plies. If the engine is getting mated
37 ///            use negative values for y.
38
39 string UCI::format_value(Value v, Value alpha, Value beta) {
40
41   stringstream ss;
42
43   if (abs(v) < VALUE_MATE_IN_MAX_PLY)
44       ss << "cp " << v * 100 / PawnValueEg;
45   else
46       ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
47
48   ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
49
50   return ss.str();
51 }
52
53
54 /// format_square() converts a Square to a string (g1, a7, etc.)
55
56 std::string UCI::format_square(Square s) {
57   char ch[] = { 'a' + file_of(s), '1' + rank_of(s), 0 };
58   return ch;
59 }
60
61
62 /// format_move() converts a Move to a string in coordinate notation
63 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
64 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
65 /// mode. Internally castling moves are always encoded as "king captures rook".
66
67 string UCI::format_move(Move m, bool chess960) {
68
69   Square from = from_sq(m);
70   Square to = to_sq(m);
71
72   if (m == MOVE_NONE)
73       return "(none)";
74
75   if (m == MOVE_NULL)
76       return "0000";
77
78   if (type_of(m) == CASTLING && !chess960)
79       to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
80
81   string move = format_square(from) + format_square(to);
82
83   if (type_of(m) == PROMOTION)
84       move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
85
86   return move;
87 }
88
89
90 /// to_move() takes a position and a string representing a move in
91 /// simple coordinate notation and returns an equivalent legal Move if any.
92
93 Move UCI::to_move(const Position& pos, string& str) {
94
95   if (str.length() == 5) // Junior could send promotion piece in uppercase
96       str[4] = char(tolower(str[4]));
97
98   for (MoveList<LEGAL> it(pos); *it; ++it)
99       if (str == format_move(*it, pos.is_chess960()))
100           return *it;
101
102   return MOVE_NONE;
103 }