Better interface to get the current move type
[glaurung_clone.git] / src / value.cpp
bloba93236c5f3d4f940880fb5753b53800996309d00
1 /*
2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ////
21 //// Includes
22 ////
24 #include <sstream>
25 #include <string>
27 #include "value.h"
30 ////
31 //// Functions
32 ////
34 /// value_to_tt() adjusts a mate score from "plies to mate from the root" to
35 /// "plies to mate from the current ply". Non-mate scores are unchanged.
36 /// The function is called before storing a value to the transposition table.
38 Value value_to_tt(Value v, int ply) {
39 if(v >= value_mate_in(100))
40 return v + ply;
41 else if(v <= value_mated_in(100))
42 return v - ply;
43 else
44 return v;
48 /// value_from_tt() is the inverse of value_to_tt(): It adjusts a mate score
49 /// from the transposition table to a mate score corrected for the current
50 /// ply depth.
52 Value value_from_tt(Value v, int ply) {
53 if(v >= value_mate_in(100))
54 return v - ply;
55 else if(v <= value_mated_in(100))
56 return v + ply;
57 else
58 return v;
62 /// value_to_centipawns() converts a value from Glaurung's somewhat unusual
63 /// scale of pawn = 256 to the more conventional pawn = 100.
65 int value_to_centipawns(Value v) {
66 return (int(v) * 100) / int(PawnValueMidgame);
70 /// value_from_centipawns() converts a centipawn value to Glaurung's internal
71 /// evaluation scale. It's used when reading the values of UCI options
72 /// containing material values (e.g. futility pruning margins).
74 Value value_from_centipawns(int cp) {
75 return Value((cp * 256) / 100);
79 /// value_to_string() converts a value to a string suitable for use with the
80 /// UCI protocol.
82 const std::string value_to_string(Value v) {
83 std::stringstream s;
85 if(abs(v) < VALUE_MATE - 200)
86 s << "cp " << value_to_centipawns(v);
87 else {
88 s << "mate ";
89 if(v > 0)
90 s << (VALUE_MATE - v + 1) / 2;
91 else
92 s << -(VALUE_MATE + v) / 2;
94 return s.str();