]> git.sesse.net Git - stockfish/blob - src/notation.cpp
Retire to_char() helpers
[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 "notation.h"
25 #include "position.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 score_to_uci(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 /// move_to_uci() converts a move to a string in coordinate notation
55 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
56 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
57 /// mode. Internally castling moves are always encoded as "king captures rook".
58
59 const string move_to_uci(Move m, bool chess960) {
60
61   Square from = from_sq(m);
62   Square to = to_sq(m);
63
64   if (m == MOVE_NONE)
65       return "(none)";
66
67   if (m == MOVE_NULL)
68       return "0000";
69
70   if (type_of(m) == CASTLING && !chess960)
71       to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
72
73   string move = to_string(from) + to_string(to);
74
75   if (type_of(m) == PROMOTION)
76       move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
77
78   return move;
79 }
80
81
82 /// move_from_uci() takes a position and a string representing a move in
83 /// simple coordinate notation and returns an equivalent legal Move if any.
84
85 Move move_from_uci(const Position& pos, string& str) {
86
87   if (str.length() == 5) // Junior could send promotion piece in uppercase
88       str[4] = char(tolower(str[4]));
89
90   for (MoveList<LEGAL> it(pos); *it; ++it)
91       if (str == move_to_uci(*it, pos.is_chess960()))
92           return *it;
93
94   return MOVE_NONE;
95 }