Skip to content

Commit 4e41577

Browse files
committed
Refactoring to comply with latest Stockfish structural changes
1 parent a8e7eb1 commit 4e41577

19 files changed

+622
-499
lines changed

Polyfish.vcxproj

+4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<ClCompile Include="src\benchmark.cpp" />
1515
<ClCompile Include="src\bitboard.cpp" />
1616
<ClCompile Include="src\book\book.cpp" />
17+
<ClCompile Include="src\book\book_manager.cpp" />
1718
<ClCompile Include="src\book\ctg\ctg.cpp" />
19+
<ClCompile Include="src\book\file_mapping.cpp" />
1820
<ClCompile Include="src\book\polyglot\polyglot.cpp" />
1921
<ClCompile Include="src\evaluate.cpp" />
2022
<ClCompile Include="src\main.cpp" />
@@ -37,7 +39,9 @@
3739
<ClInclude Include="src\benchmark.h" />
3840
<ClInclude Include="src\bitboard.h" />
3941
<ClInclude Include="src\book\book.h" />
42+
<ClInclude Include="src\book\book_manager.h" />
4043
<ClInclude Include="src\book\ctg\ctg.h" />
44+
<ClInclude Include="src\book\file_mapping.h" />
4145
<ClInclude Include="src\book\polyglot\polyglot.h" />
4246
<ClInclude Include="src\evaluate.h" />
4347
<ClInclude Include="src\incbin\incbin.h" />

Polyfish.vcxproj.filters

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
<ClCompile Include="src\book\book.cpp">
3535
<Filter>book</Filter>
3636
</ClCompile>
37+
<ClCompile Include="src\book\book_manager.cpp">
38+
<Filter>book</Filter>
39+
</ClCompile>
40+
<ClCompile Include="src\book\file_mapping.cpp">
41+
<Filter>book</Filter>
42+
</ClCompile>
3743
</ItemGroup>
3844
<ItemGroup>
3945
<ClInclude Include="src\bitboard.h" />
@@ -96,6 +102,12 @@
96102
<ClInclude Include="src\book\book.h">
97103
<Filter>book</Filter>
98104
</ClInclude>
105+
<ClInclude Include="src\book\book_manager.h">
106+
<Filter>book</Filter>
107+
</ClInclude>
108+
<ClInclude Include="src\book\file_mapping.h">
109+
<Filter>book</Filter>
110+
</ClInclude>
99111
</ItemGroup>
100112
<ItemGroup>
101113
<None Include="src\Makefile" />

src/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SRCS = benchmark.cpp bitboard.cpp evaluate.cpp main.cpp \
6060
misc.cpp movegen.cpp movepick.cpp position.cpp \
6161
search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
6262
nnue/evaluate_nnue.cpp nnue/features/half_ka_v2_hm.cpp \
63-
book/book.cpp book/polyglot/polyglot.cpp book/ctg/ctg.cpp
63+
book/file_mapping.cpp book/book.cpp book/book_manager.cpp book/polyglot/polyglot.cpp book/ctg/ctg.cpp
6464

6565
#HEADERS = benchmark.h bitboard.h evaluate.h misc.h movegen.h movepick.h \
6666
# nnue/evaluate_nnue.h nnue/features/half_ka_v2_hm.h nnue/layers/affine_transform.h \

src/book/book.cpp

+13-107
Original file line numberDiff line numberDiff line change
@@ -6,116 +6,22 @@
66
#include "ctg/ctg.h"
77
#include "book.h"
88

9-
using namespace std;
10-
119
namespace Polyfish::Book
1210
{
13-
namespace
14-
{
15-
Book* create_book(const std::string& filename)
16-
{
17-
size_t extIndex = filename.find_last_of('.');
18-
if (extIndex == string::npos)
19-
return nullptr;
20-
21-
string ext = filename.substr(extIndex + 1);
22-
23-
if (ext == "ctg" || ext == "cto" || ext == "ctb")
24-
return new CTG::CtgBook();
25-
else if (ext == "bin")
26-
return new Polyglot::PolyglotBook();
27-
else
28-
return nullptr;
29-
}
30-
}
31-
32-
constexpr size_t NumBooks = 2;
33-
Book* books[NumBooks];
34-
35-
void init(const OptionsMap& options)
11+
/*static*/ Book* Book::create_book(const std::string& filename)
3612
{
37-
for (size_t i = 0; i < NumBooks; ++i)
38-
books[i] = nullptr;
39-
40-
on_book(0, options);
41-
on_book(1, options);
42-
}
43-
44-
void finalize()
45-
{
46-
for (size_t i = 0; i < NumBooks; ++i)
47-
{
48-
delete books[i];
49-
books[i] = nullptr;
50-
}
51-
}
52-
53-
void on_book(int index, const OptionsMap& options)
54-
{
55-
//Close previous book if any
56-
delete books[index];
57-
books[index] = nullptr;
58-
59-
60-
std::string filename = std::string(options[Utility::format_string("CTG/BIN Book %d File", index + 1)]);
61-
62-
//Load new book
63-
if (Utility::is_empty_filename(filename))
64-
return;
65-
66-
//Create book object for the given book type
67-
string fn = Utility::map_path(filename);
68-
Book* book = create_book(fn);
69-
if (book == nullptr)
70-
{
71-
sync_cout << "info string Unknown book type: " << filename << sync_endl;
72-
return;
73-
}
74-
75-
//Open/Initialize the book
76-
if (!book->open(fn))
77-
{
78-
delete book;
79-
return;
80-
}
81-
82-
books[index] = book;
83-
}
84-
85-
Move probe(const Position& pos, const OptionsMap& options)
86-
{
87-
int moveNumber = 1 + pos.game_ply() / 2;
88-
Move bookMove = Move::none();
89-
90-
for (size_t i = 0; i < NumBooks; ++i)
91-
{
92-
if (books[i] != nullptr && int(options[Utility::format_string("Book %d Depth", i + 1)]) >= moveNumber)
93-
{
94-
bookMove = books[i]->probe(pos, size_t(int(options[Utility::format_string("Book %d Width", i + 1)])), bool(options[Utility::format_string("(CTG) Book %d Only Green", i + 1)]));
95-
if (bookMove != Move::none())
96-
break;
97-
}
98-
}
99-
100-
return bookMove;
101-
}
102-
103-
void show_moves(const Position& pos, const OptionsMap& options)
104-
{
105-
cout << pos << endl << endl;
106-
107-
for (size_t i = 0; i < NumBooks; ++i)
108-
{
109-
if (books[i] == nullptr)
110-
{
111-
cout << "Book " << i + 1 << ": No book loaded" << endl;
112-
}
113-
else
114-
{
115-
cout << "Book " << i + 1 << " (" << books[i]->type() << "): " << std::string(options[Utility::format_string("CTG/BIN Book %d File", i + 1)]) << endl;
116-
books[i]->show_moves(pos);
117-
}
118-
}
13+
size_t extIndex = filename.find_last_of('.');
14+
if (extIndex == std::string::npos)
15+
return nullptr;
16+
17+
std::string ext = filename.substr(extIndex + 1);
18+
19+
if (ext == "ctg" || ext == "cto" || ext == "ctb")
20+
return new CTG::CtgBook();
21+
else if (ext == "bin")
22+
return new Polyglot::PolyglotBook();
23+
else
24+
return nullptr;
11925
}
12026
}
12127

src/book/book.h

+13-15
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
#ifndef BOOK_H_INCLUDED
44
#define BOOK_H_INCLUDED
55

6-
#include <string>
7-
#include "../types.h"
8-
#include "../position.h"
9-
106
namespace Polyfish::Book
117
{
12-
namespace BookUtil
8+
namespace
139
{
1410
static const union { uint32_t i; char c[4]; } Le = { 0x01020304 };
1511
static const bool IsBigEndian = (Le.c[0] == 1);
12+
}
1613

14+
class BookUtil
15+
{
16+
public:
1717
template <typename IntType>
18-
IntType read_big_endian(const unsigned char* buffer, size_t& offset, size_t bufferLen)
18+
static IntType read_big_endian(const unsigned char* buffer, size_t& offset, size_t bufferLen)
1919
{
2020
IntType result;
2121
constexpr size_t typeSize = sizeof(IntType);
@@ -46,15 +46,20 @@ namespace Polyfish::Book
4646
}
4747

4848
template <typename IntType>
49-
IntType read_big_endian(const unsigned char* buffer, size_t bufferLen)
49+
static IntType read_big_endian(const unsigned char* buffer, size_t bufferLen)
5050
{
5151
size_t offset = 0;
5252
return read_big_endian<IntType>(buffer, offset, bufferLen);
5353
}
54-
}
54+
};
5555

5656
class Book
5757
{
58+
friend class Polyfish::BookManager;
59+
60+
private:
61+
static Book* create_book(const std::string& filename);
62+
5863
public:
5964
Book() { }
6065
virtual ~Book() { }
@@ -70,13 +75,6 @@ namespace Polyfish::Book
7075
virtual Move probe(const Position& pos, size_t width, bool onlyGreen) const = 0;
7176
virtual void show_moves(const Position& pos) const = 0;
7277
};
73-
74-
void init(const OptionsMap& options);
75-
void finalize();
76-
77-
void on_book(int index, const OptionsMap& options);
78-
Move probe(const Position& pos, const OptionsMap& options);
79-
void show_moves(const Position& pos, const OptionsMap& options);
8078
}
8179

8280
#endif

src/book/book_manager.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#if defined(POLYFISH)
2+
3+
#include "../misc.h"
4+
#include "../uci.h"
5+
#include "polyglot/polyglot.h"
6+
#include "ctg/ctg.h"
7+
#include "book.h"
8+
9+
namespace Polyfish
10+
{
11+
BookManager::BookManager()
12+
{
13+
for (int i = 0; i < NumberOfBooks; ++i)
14+
books[i] = nullptr;
15+
}
16+
17+
BookManager::~BookManager()
18+
{
19+
for (int i = 0; i < NumberOfBooks; ++i)
20+
delete books[i];
21+
}
22+
23+
void BookManager::init(const OptionsMap& options)
24+
{
25+
for (size_t i = 0; i < NumberOfBooks; ++i)
26+
init(i, options);
27+
}
28+
29+
void BookManager::init(int index, const OptionsMap& options)
30+
{
31+
assert(index < NumberOfBooks);
32+
33+
//Close previous book if any
34+
delete books[index];
35+
books[index] = nullptr;
36+
37+
std::string filename = std::string(options[Utility::format_string("CTG/BIN Book %d File", index + 1)]);
38+
39+
//Load new book
40+
if (Utility::is_empty_filename(filename))
41+
return;
42+
43+
//Create book object for the given book type
44+
std::string fn = Utility::map_path(filename);
45+
Book::Book* book = Book::Book::create_book(fn);
46+
if (book == nullptr)
47+
{
48+
sync_cout << "info string Unknown book type: " << filename << sync_endl;
49+
return;
50+
}
51+
52+
//Open/Initialize the book
53+
if (!book->open(fn))
54+
{
55+
delete book;
56+
return;
57+
}
58+
59+
books[index] = book;
60+
}
61+
62+
Move BookManager::probe(const Position& pos, const OptionsMap& options) const
63+
{
64+
int moveNumber = 1 + pos.game_ply() / 2;
65+
Move bookMove = Move::none();
66+
67+
for (size_t i = 0; i < NumberOfBooks; ++i)
68+
{
69+
if (books[i] != nullptr && int(options[Utility::format_string("Book %d Depth", i + 1)]) >= moveNumber)
70+
{
71+
bookMove = books[i]->probe(pos, size_t(int(options[Utility::format_string("Book %d Width", i + 1)])), bool(options[Utility::format_string("(CTG) Book %d Only Green", i + 1)]));
72+
if (bookMove != Move::none())
73+
break;
74+
}
75+
}
76+
77+
return bookMove;
78+
}
79+
80+
void BookManager::show_moves(const Position& pos, const OptionsMap& options) const
81+
{
82+
std::cout << pos << std::endl << std::endl;
83+
84+
for (size_t i = 0; i < NumberOfBooks; ++i)
85+
{
86+
if (books[i] == nullptr)
87+
{
88+
std::cout << "Book " << i + 1 << ": No book loaded" << std::endl;
89+
}
90+
else
91+
{
92+
std::cout << "Book " << i + 1 << " (" << books[i]->type() << "): " << std::string(options[Utility::format_string("CTG/BIN Book %d File", i + 1)]) << std::endl;
93+
books[i]->show_moves(pos);
94+
}
95+
}
96+
}
97+
}
98+
99+
#endif

src/book/book_manager.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#if defined(POLYFISH)
2+
3+
#ifndef BOOKMANAGER_H_INCLUDED
4+
#define BOOKMANAGER_H_INCLUDED
5+
6+
namespace Polyfish
7+
{
8+
namespace Book
9+
{
10+
class Book;
11+
}
12+
13+
class BookManager
14+
{
15+
public:
16+
static constexpr int NumberOfBooks = 2;
17+
18+
private:
19+
Book::Book* books[NumberOfBooks];
20+
21+
public:
22+
BookManager();
23+
virtual ~BookManager();
24+
25+
BookManager(const BookManager&) = delete;
26+
BookManager& operator=(const BookManager&) = delete;
27+
28+
void init(const OptionsMap& options);
29+
void init(int index, const OptionsMap& options);
30+
Move probe(const Position& pos, const OptionsMap& options) const;
31+
void show_moves(const Position& pos, const OptionsMap& options) const;
32+
};
33+
}
34+
35+
#endif
36+
37+
#endif // #ifndef BOOKMANAGER_H_INCLUDED

0 commit comments

Comments
 (0)