]> git.sesse.net Git - stockfish/blob - src/value.cpp
Use pointers instead of array indices in MovePicker
[stockfish] / src / value.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-2009 Marco Costalba
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
21 ////
22 //// Includes
23 ////
24
25 #include <sstream>
26 #include <string>
27
28 #include "value.h"
29
30
31 ////
32 //// Functions
33 ////
34
35 /// value_to_tt() adjusts a mate score from "plies to mate from the root" to
36 /// "plies to mate from the current ply".  Non-mate scores are unchanged.
37 /// The function is called before storing a value to the transposition table.
38
39 Value value_to_tt(Value v, int ply) {
40   if(v >= value_mate_in(100))
41     return v + ply;
42   else if(v <= value_mated_in(100))
43     return v - ply;
44   else
45     return v;
46 }
47
48
49 /// value_from_tt() is the inverse of value_to_tt():  It adjusts a mate score
50 /// from the transposition table to a mate score corrected for the current
51 /// ply depth.
52
53 Value value_from_tt(Value v, int ply) {
54   if(v >= value_mate_in(100))
55     return v - ply;
56   else if(v <= value_mated_in(100))
57     return v + ply;
58   else
59     return v;
60 }
61
62
63 /// value_to_centipawns() converts a value from Stockfish's somewhat unusual
64 /// scale of pawn = 256 to the more conventional pawn = 100.
65
66 int value_to_centipawns(Value v) {
67   return (int(v) * 100) / int(PawnValueMidgame);
68 }
69
70
71 /// value_from_centipawns() converts a centipawn value to Stockfish's internal
72 /// evaluation scale.  It's used when reading the values of UCI options
73 /// containing material values (e.g. futility pruning margins).
74
75 Value value_from_centipawns(int cp) {
76   return Value((cp * 256) / 100);
77 }
78
79
80 /// value_to_string() converts a value to a string suitable for use with the
81 /// UCI protocol.
82
83 const std::string value_to_string(Value v) {
84   std::stringstream s;
85
86   if(abs(v) < VALUE_MATE - 200)
87     s << "cp " << value_to_centipawns(v);
88   else {
89     s << "mate ";
90     if(v > 0)
91       s << (VALUE_MATE - v + 1) / 2;
92     else
93       s << -(VALUE_MATE + v) / 2;
94   }
95   return s.str();
96 }