]> git.sesse.net Git - stockfish/blob - src/value.cpp
Switch to developer version numbering
[stockfish] / src / value.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
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.
9   
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.
14   
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/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <sstream>
25 #include <string>
26
27 #include "value.h"
28
29
30 ////
31 //// Functions
32 ////
33
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.
37
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;
45 }
46
47
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.
51
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;
59 }
60
61
62 /// value_to_centipawns() converts a value from Glaurung's somewhat unusual
63 /// scale of pawn = 256 to the more conventional pawn = 100.
64
65 int value_to_centipawns(Value v) {
66   return (int(v) * 100) / int(PawnValueMidgame);
67 }
68
69
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).
73
74 Value value_from_centipawns(int cp) {
75   return Value((cp * 256) / 100);
76 }
77
78
79 /// value_to_string() converts a value to a string suitable for use with the
80 /// UCI protocol.
81
82 const std::string value_to_string(Value v) {
83   std::stringstream s;
84
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;
93   }
94   return s.str();
95 }