1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-07-11 19:49:14 +00:00

Use string instead of std::string

And others small code style touches.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-05-10 17:58:53 +01:00
parent 78eecbf6d7
commit 27619830d4
10 changed files with 85 additions and 92 deletions

View file

@ -30,12 +30,13 @@
#include "thread.h" #include "thread.h"
#include "ucioption.h" #include "ucioption.h"
using namespace std;
//// ////
//// Variables //// Variables
//// ////
const std::string BenchmarkPositions[] = { const string BenchmarkPositions[] = {
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"r4rk1/1b2qppp/p1n1p3/1p6/1b1PN3/3BRN2/PP3PPP/R2Q2K1 b - - 7 16", "r4rk1/1b2qppp/p1n1p3/1p6/1b1PN3/3BRN2/PP3PPP/R2Q2K1 b - - 7 16",
"4r1k1/ppq3pp/3b4/2pP4/2Q1p3/4B1P1/PP5P/R5K1 b - - 0 20", "4r1k1/ppq3pp/3b4/2pP4/2Q1p3/4B1P1/PP5P/R5K1 b - - 0 20",
@ -67,25 +68,25 @@ const std::string BenchmarkPositions[] = {
/// format (default are the BenchmarkPositions defined above). /// format (default are the BenchmarkPositions defined above).
/// The analysis is written to a file named bench.txt. /// The analysis is written to a file named bench.txt.
void benchmark(const std::string& commandLine) { void benchmark(const string& commandLine) {
std::istringstream csVal(commandLine); istringstream csVal(commandLine);
std::istringstream csStr(commandLine); istringstream csStr(commandLine);
std::string ttSize, threads, fileName, limitType; string ttSize, threads, fileName, limitType;
int val, secsPerPos, maxDepth, maxNodes; int val, secsPerPos, maxDepth, maxNodes;
csStr >> ttSize; csStr >> ttSize;
csVal >> val; csVal >> val;
if (val < 4 || val > 1024) if (val < 4 || val > 1024)
{ {
std::cerr << "The hash table size must be between 4 and 1024" << std::endl; cerr << "The hash table size must be between 4 and 1024" << endl;
Application::exit_with_failure(); Application::exit_with_failure();
} }
csStr >> threads; csStr >> threads;
csVal >> val; csVal >> val;
if (val < 1 || val > THREAD_MAX) if (val < 1 || val > THREAD_MAX)
{ {
std::cerr << "The number of threads must be between 1 and " << THREAD_MAX << std::endl; cerr << "The number of threads must be between 1 and " << THREAD_MAX << endl;
Application::exit_with_failure(); Application::exit_with_failure();
} }
set_option_value("Hash", ttSize); set_option_value("Hash", ttSize);
@ -107,30 +108,30 @@ void benchmark(const std::string& commandLine) {
else else
maxNodes = val; maxNodes = val;
std::vector<std::string> positions; vector<string> positions;
if (fileName != "default") if (fileName != "default")
{ {
std::ifstream fenFile(fileName.c_str()); ifstream fenFile(fileName.c_str());
if (!fenFile.is_open()) if (!fenFile.is_open())
{ {
std::cerr << "Unable to open positions file " << fileName << std::endl; cerr << "Unable to open positions file " << fileName << endl;
Application::exit_with_failure(); Application::exit_with_failure();
} }
std::string pos; string pos;
while (fenFile.good()) while (fenFile.good())
{ {
std::getline(fenFile, pos); getline(fenFile, pos);
if (!pos.empty()) if (!pos.empty())
positions.push_back(pos); positions.push_back(pos);
} }
fenFile.close(); fenFile.close();
} else } else
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
positions.push_back(std::string(BenchmarkPositions[i])); positions.push_back(string(BenchmarkPositions[i]));
int startTime = get_system_time(); int startTime = get_system_time();
std::vector<std::string>::iterator it; vector<string>::iterator it;
int cnt = 1; int cnt = 1;
int64_t totalNodes = 0; int64_t totalNodes = 0;
for (it = positions.begin(); it != positions.end(); ++it, ++cnt) for (it = positions.begin(); it != positions.end(); ++it, ++cnt)
@ -138,13 +139,12 @@ void benchmark(const std::string& commandLine) {
Move moves[1] = {MOVE_NONE}; Move moves[1] = {MOVE_NONE};
int dummy[2] = {0, 0}; int dummy[2] = {0, 0};
Position pos(*it); Position pos(*it);
std::cout << "\nProcessing position " << cnt << '/' << positions.size() << std::endl << std::endl; cout << "\nProcessing position " << cnt << '/' << positions.size() << endl << endl;
if (!think(pos, true, false, 0, dummy, dummy, 0, maxDepth, maxNodes, secsPerPos, moves)) if (!think(pos, true, false, 0, dummy, dummy, 0, maxDepth, maxNodes, secsPerPos, moves))
break; break;
totalNodes += nodes_searched(); totalNodes += nodes_searched();
} }
std::cout << "\nProcessing time (ms) " << get_system_time() - startTime << std::endl cout << "\nProcessing time (ms) " << get_system_time() - startTime
<< "Nodes searched " << totalNodes << std::endl << "\nNodes searched " << totalNodes << "\nPress any key to exit" << endl;
<< "Press any key to exit" << std::endl; cin >> fileName;
std::cin >> fileName;
} }

View file

@ -37,7 +37,6 @@ enum Color {
//// Inline functions //// Inline functions
//// ////
inline Color operator+ (Color c, int i) { return Color(int(c) + i); }
inline void operator++ (Color &c, int) { c = Color(int(c) + 1); } inline void operator++ (Color &c, int) { c = Color(int(c) + 1); }
inline Color opposite_color(Color c) { inline Color opposite_color(Color c) {

View file

@ -29,6 +29,7 @@
#include "material.h" #include "material.h"
using std::string;
//// ////
//// Local definitions //// Local definitions
@ -59,9 +60,9 @@ public:
EndgameScalingFunctionBase* getESF(Key key, Color* c) const; EndgameScalingFunctionBase* getESF(Key key, Color* c) const;
private: private:
void add(const std::string& keyCode, EndgameEvaluationFunctionBase* f); void add(const string& keyCode, EndgameEvaluationFunctionBase* f);
void add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f); void add(const string& keyCode, Color c, EndgameScalingFunctionBase* f);
Key buildKey(const std::string& keyCode); Key buildKey(const string& keyCode);
struct ScalingInfo struct ScalingInfo
{ {
@ -352,7 +353,7 @@ EndgameFunctions::EndgameFunctions() {
add("KRPKRPP", BLACK, &ScaleKRPKRPP); add("KRPKRPP", BLACK, &ScaleKRPKRPP);
} }
Key EndgameFunctions::buildKey(const std::string& keyCode) { Key EndgameFunctions::buildKey(const string& keyCode) {
assert(keyCode.length() > 0 && keyCode[0] == 'K'); assert(keyCode.length() > 0 && keyCode[0] == 'K');
assert(keyCode.length() < 8); assert(keyCode.length() < 8);
@ -373,12 +374,12 @@ Key EndgameFunctions::buildKey(const std::string& keyCode) {
return Position(s.str()).get_material_key(); return Position(s.str()).get_material_key();
} }
void EndgameFunctions::add(const std::string& keyCode, EndgameEvaluationFunctionBase* f) { void EndgameFunctions::add(const string& keyCode, EndgameEvaluationFunctionBase* f) {
EEFmap.insert(std::pair<Key, EndgameEvaluationFunctionBase*>(buildKey(keyCode), f)); EEFmap.insert(std::pair<Key, EndgameEvaluationFunctionBase*>(buildKey(keyCode), f));
} }
void EndgameFunctions::add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f) { void EndgameFunctions::add(const string& keyCode, Color c, EndgameScalingFunctionBase* f) {
ScalingInfo s = {c, f}; ScalingInfo s = {c, f};
ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(keyCode), s)); ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(keyCode), s));

View file

@ -81,6 +81,8 @@ static const string AppTag = "";
//// Variables //// Variables
//// ////
bool Chess960;
uint64_t dbg_cnt0 = 0; uint64_t dbg_cnt0 = 0;
uint64_t dbg_cnt1 = 0; uint64_t dbg_cnt1 = 0;

View file

@ -40,6 +40,13 @@
#define Max(x, y) (((x) < (y))? (y) : (x)) #define Max(x, y) (((x) < (y))? (y) : (x))
////
//// Variables
////
extern bool Chess960;
//// ////
//// Prototypes //// Prototypes
//// ////

View file

@ -27,7 +27,6 @@
#include "move.h" #include "move.h"
#include "piece.h" #include "piece.h"
#include "position.h" #include "position.h"
#include "ucioption.h"
//// ////

View file

@ -35,6 +35,8 @@
#include "san.h" #include "san.h"
#include "ucioption.h" #include "ucioption.h"
using std::string;
//// ////
//// Variables //// Variables
@ -63,7 +65,7 @@ Position::Position(const Position& pos) {
copy(pos); copy(pos);
} }
Position::Position(const std::string& fen) { Position::Position(const string& fen) {
from_fen(fen); from_fen(fen);
} }
@ -72,9 +74,9 @@ Position::Position(const std::string& fen) {
/// string. This function is not very robust - make sure that input FENs are /// string. This function is not very robust - make sure that input FENs are
/// correct (this is assumed to be the responsibility of the GUI). /// correct (this is assumed to be the responsibility of the GUI).
void Position::from_fen(const std::string& fen) { void Position::from_fen(const string& fen) {
static const std::string pieceLetters = "KQRBNPkqrbnp"; static const string pieceLetters = "KQRBNPkqrbnp";
static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP }; static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP };
clear(); clear();
@ -98,7 +100,7 @@ void Position::from_fen(const std::string& fen) {
continue; continue;
} }
size_t idx = pieceLetters.find(fen[i]); size_t idx = pieceLetters.find(fen[i]);
if (idx == std::string::npos) if (idx == string::npos)
{ {
std::cout << "Error in FEN at character " << i << std::endl; std::cout << "Error in FEN at character " << i << std::endl;
return; return;
@ -219,10 +221,10 @@ void Position::from_fen(const std::string& fen) {
/// Position::to_fen() converts the position object to a FEN string. This is /// Position::to_fen() converts the position object to a FEN string. This is
/// probably only useful for debugging. /// probably only useful for debugging.
const std::string Position::to_fen() const { const string Position::to_fen() const {
static const std::string pieceLetters = " PNBRQK pnbrqk"; static const string pieceLetters = " PNBRQK pnbrqk";
std::string fen; string fen;
int skip; int skip;
for (Rank rank = RANK_8; rank >= RANK_1; rank--) for (Rank rank = RANK_8; rank >= RANK_1; rank--)
@ -272,7 +274,7 @@ const std::string Position::to_fen() const {
void Position::print(Move m) const { void Position::print(Move m) const {
static const std::string pieceLetters = " PNBRQK PNBRQK ."; static const string pieceLetters = " PNBRQK PNBRQK .";
// Check for reentrancy, as example when called from inside // Check for reentrancy, as example when called from inside
// MovePicker that is used also here in move_to_san() // MovePicker that is used also here in move_to_san()
@ -284,7 +286,7 @@ void Position::print(Move m) const {
std::cout << std::endl; std::cout << std::endl;
if (m != MOVE_NONE) if (m != MOVE_NONE)
{ {
std::string col = (color_of_piece_on(move_from(m)) == BLACK ? ".." : ""); string col = (color_of_piece_on(move_from(m)) == BLACK ? ".." : "");
std::cout << "Move is: " << col << move_to_san(*this, m) << std::endl; std::cout << "Move is: " << col << move_to_san(*this, m) << std::endl;
} }
for (Rank rank = RANK_8; rank >= RANK_1; rank--) for (Rank rank = RANK_8; rank >= RANK_1; rank--)

View file

@ -31,6 +31,7 @@
#include "movepick.h" #include "movepick.h"
#include "san.h" #include "san.h"
using std::string;
//// ////
//// Local definitions //// Local definitions
@ -51,8 +52,8 @@ namespace {
/// Functions /// Functions
Ambiguity move_ambiguity(const Position& pos, Move m); Ambiguity move_ambiguity(const Position& pos, Move m);
const std::string time_string(int milliseconds); const string time_string(int milliseconds);
const std::string score_string(Value v); const string score_string(Value v);
} }
@ -64,7 +65,7 @@ namespace {
/// that the move is a legal move from the position. The return value is /// that the move is a legal move from the position. The return value is
/// a string containing the move in short algebraic notation. /// a string containing the move in short algebraic notation.
const std::string move_to_san(const Position& pos, Move m) { const string move_to_san(const Position& pos, Move m) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(move_is_ok(m)); assert(move_is_ok(m));
@ -76,7 +77,7 @@ const std::string move_to_san(const Position& pos, Move m) {
to = move_to(m); to = move_to(m);
pt = type_of_piece(pos.piece_on(move_from(m))); pt = type_of_piece(pos.piece_on(move_from(m)));
std::string san = ""; string san = "";
if (m == MOVE_NONE) if (m == MOVE_NONE)
return "(none)"; return "(none)";
@ -138,7 +139,7 @@ const std::string move_to_san(const Position& pos, Move m) {
/// the move is returned. On failure (i.e. if the string is unparsable, or /// the move is returned. On failure (i.e. if the string is unparsable, or
/// if the move is illegal or ambiguous), MOVE_NONE is returned. /// if the move is illegal or ambiguous), MOVE_NONE is returned.
Move move_from_san(const Position& pos, const std::string& movestr) { Move move_from_san(const Position& pos, const string& movestr) {
assert(pos.is_ok()); assert(pos.is_ok());
@ -166,7 +167,7 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
// Normal moves. We use a simple FSM to parse the san string. // Normal moves. We use a simple FSM to parse the san string.
enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END }; enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
static const std::string pieceLetters = "KQRBN"; static const string pieceLetters = "KQRBN";
PieceType pt = NO_PIECE_TYPE, promotion = NO_PIECE_TYPE; PieceType pt = NO_PIECE_TYPE, promotion = NO_PIECE_TYPE;
File fromFile = FILE_NONE, toFile = FILE_NONE; File fromFile = FILE_NONE, toFile = FILE_NONE;
Rank fromRank = RANK_NONE, toRank = RANK_NONE; Rank fromRank = RANK_NONE, toRank = RANK_NONE;
@ -176,7 +177,7 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
for (size_t i = 0; i < movestr.length(); i++) for (size_t i = 0; i < movestr.length(); i++)
{ {
char type, c = movestr[i]; char type, c = movestr[i];
if (pieceLetters.find(c) != std::string::npos) if (pieceLetters.find(c) != string::npos)
type = 'P'; type = 'P';
else if (c >= 'a' && c <= 'h') else if (c >= 'a' && c <= 'h')
type = 'F'; type = 'F';
@ -292,11 +293,11 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
/// length of 80 characters. After a line break, 'startColumn' spaces are /// length of 80 characters. After a line break, 'startColumn' spaces are
/// inserted at the beginning of the new line. /// inserted at the beginning of the new line.
const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) { const string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
StateInfo st; StateInfo st;
std::stringstream s; std::stringstream s;
std::string moveStr; string moveStr;
size_t length = 0; size_t length = 0;
size_t maxLength = 80 - startColumn; size_t maxLength = 80 - startColumn;
Position p(pos); Position p(pos);
@ -325,8 +326,8 @@ const std::string line_to_san(const Position& pos, Move line[], int startColumn,
/// It is used to write search information to the log file (which is created /// It is used to write search information to the log file (which is created
/// when the UCI parameter "Use Search Log" is "true"). /// when the UCI parameter "Use Search Log" is "true").
const std::string pretty_pv(const Position& pos, int time, int depth, const string pretty_pv(const Position& pos, int time, int depth,
uint64_t nodes, Value score, Move pv[]) { uint64_t nodes, Value score, Move pv[]) {
std::stringstream s; std::stringstream s;
// Depth // Depth
@ -396,7 +397,7 @@ namespace {
} }
const std::string time_string(int milliseconds) { const string time_string(int milliseconds) {
std::stringstream s; std::stringstream s;
s << std::setfill('0'); s << std::setfill('0');
@ -413,7 +414,7 @@ namespace {
} }
const std::string score_string(Value v) { const string score_string(Value v) {
std::stringstream s; std::stringstream s;

View file

@ -33,13 +33,7 @@
#include "thread.h" #include "thread.h"
#include "ucioption.h" #include "ucioption.h"
using std::string;
////
//// Variables
////
bool Chess960;
//// ////
//// Local definitions //// Local definitions
@ -53,11 +47,11 @@ namespace {
enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON }; enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON };
typedef std::vector<std::string> ComboValues; typedef std::vector<string> ComboValues;
struct Option { struct Option {
std::string name, defaultValue, currentValue; string name, defaultValue, currentValue;
OptionType type; OptionType type;
size_t idx; size_t idx;
int minValue, maxValue; int minValue, maxValue;
@ -71,7 +65,7 @@ namespace {
bool operator<(const Option& o) const { return this->idx < o.idx; } bool operator<(const Option& o) const { return this->idx < o.idx; }
}; };
typedef std::map<std::string, Option> Options; typedef std::map<string, Option> Options;
/// ///
/// Constants /// Constants
@ -155,7 +149,7 @@ namespace {
// stringify converts a value of type T to a std::string // stringify converts a value of type T to a std::string
template<typename T> template<typename T>
std::string stringify(const T& v) { string stringify(const T& v) {
std::ostringstream ss; std::ostringstream ss;
ss << v; ss << v;
@ -168,7 +162,7 @@ namespace {
// type changes a template seems a proper solution. // type changes a template seems a proper solution.
template<typename T> template<typename T>
T get_option_value(const std::string& optionName) { T get_option_value(const string& optionName) {
T ret = T(); T ret = T();
if (options.find(optionName) == options.end()) if (options.find(optionName) == options.end())
@ -245,9 +239,7 @@ void print_uci_options() {
std::cout << " default " << it->defaultValue; std::cout << " default " << it->defaultValue;
if (it->type == SPIN) if (it->type == SPIN)
std::cout << " min " << it->minValue std::cout << " min " << it->minValue << " max " << it->maxValue;
<< " max " << it->maxValue;
else if (it->type == COMBO) else if (it->type == COMBO)
for (ComboValues::const_iterator itc = it->comboValues.begin(); for (ComboValues::const_iterator itc = it->comboValues.begin();
itc != it->comboValues.end(); ++itc) itc != it->comboValues.end(); ++itc)
@ -260,7 +252,7 @@ void print_uci_options() {
/// get_option_value_bool() returns the current value of a UCI parameter of /// get_option_value_bool() returns the current value of a UCI parameter of
/// type "check". /// type "check".
bool get_option_value_bool(const std::string& optionName) { bool get_option_value_bool(const string& optionName) {
return get_option_value<bool>(optionName); return get_option_value<bool>(optionName);
} }
@ -271,7 +263,7 @@ bool get_option_value_bool(const std::string& optionName) {
/// it could also be used with a "combo" parameter, where all the available /// it could also be used with a "combo" parameter, where all the available
/// values are integers. /// values are integers.
int get_option_value_int(const std::string& optionName) { int get_option_value_int(const string& optionName) {
return get_option_value<int>(optionName); return get_option_value<int>(optionName);
} }
@ -280,9 +272,9 @@ int get_option_value_int(const std::string& optionName) {
/// get_option_value_string() returns the current value of a UCI parameter as /// get_option_value_string() returns the current value of a UCI parameter as
/// a string. It is used with parameters of type "combo" and "string". /// a string. It is used with parameters of type "combo" and "string".
const std::string get_option_value_string(const std::string& optionName) { string get_option_value_string(const string& optionName) {
return get_option_value<std::string>(optionName); return get_option_value<string>(optionName);
} }
@ -290,28 +282,27 @@ const std::string get_option_value_string(const std::string& optionName) {
/// the function does not check that the new value is legal for the given /// the function does not check that the new value is legal for the given
/// parameter: This is assumed to be the responsibility of the GUI. /// parameter: This is assumed to be the responsibility of the GUI.
void set_option_value(const std::string& optionName, void set_option_value(const string& name, const string& value) {
const std::string& newValue) {
// UCI protocol uses "true" and "false" instead of "1" and "0", so convert // UCI protocol uses "true" and "false" instead of "1" and "0", so convert
// newValue according to standard C++ convention before to store it. // value according to standard C++ convention before to store it.
std::string v(newValue); string v(value);
if (v == "true") if (v == "true")
v = "1"; v = "1";
else if (v == "false") else if (v == "false")
v = "0"; v = "0";
if (options.find(optionName) != options.end()) if (options.find(name) != options.end())
options[optionName].currentValue = v; options[name].currentValue = v;
else else
std::cout << "No such option: " << optionName << std::endl; std::cout << "No such option: " << name << std::endl;
} }
/// push_button() is used to tell the engine that a UCI parameter of type /// push_button() is used to tell the engine that a UCI parameter of type
/// "button" has been selected: /// "button" has been selected:
void push_button(const std::string& buttonName) { void push_button(const string& buttonName) {
set_option_value(buttonName, "true"); set_option_value(buttonName, "true");
} }
@ -321,7 +312,7 @@ void push_button(const std::string& buttonName) {
/// been selected since the last time the function was called, in this case /// been selected since the last time the function was called, in this case
/// it also resets the button. /// it also resets the button.
bool button_was_pressed(const std::string& buttonName) { bool button_was_pressed(const string& buttonName) {
if (!get_option_value<bool>(buttonName)) if (!get_option_value<bool>(buttonName))
return false; return false;

View file

@ -27,27 +27,18 @@
#include <string> #include <string>
////
//// Variables
////
extern bool Chess960;
//// ////
//// Prototypes //// Prototypes
//// ////
extern void init_uci_options(); extern void init_uci_options();
extern void print_uci_options(); extern void print_uci_options();
extern bool get_option_value_bool(const std::string &optionName); extern bool get_option_value_bool(const std::string& optionName);
extern int get_option_value_int(const std::string &optionName); extern int get_option_value_int(const std::string& optionName);
extern const std::string get_option_value_string(const std::string &optionName); extern std::string get_option_value_string(const std::string& optionName);
extern bool button_was_pressed(const std::string &buttonName); extern bool button_was_pressed(const std::string& buttonName);
extern void set_option_value(const std::string &optionName, extern void set_option_value(const std::string& optionName,const std::string& newValue);
const std::string &newValue); extern void push_button(const std::string& buttonName);
extern void push_button(const std::string &buttonName);
#endif // !defined(UCIOPTION_H_INCLUDED) #endif // !defined(UCIOPTION_H_INCLUDED)