Skip to content

Commit

Permalink
Add NoobBookMode option (#711)
Browse files Browse the repository at this point in the history
Allows choosing between (poorly named) options 'best' and 'all'.

Bench: 23621947
  • Loading branch information
TerjeKir authored Mar 3, 2024
1 parent 99372c2 commit 25316d1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Weiss appears in most rating lists, and can be seen competing at [TCEC](https://
* #### NoobBook
Allow Weiss to query and play moves suggested by [noobpwnftw's online opening database](https://www.chessdb.cn/queryc_en/).

* #### NoobBookMode
Sets the query mode to use (see [dbcn docs](https://www.chessdb.cn/cloudbookc_api_en.html)):
- best - chooses randomly from the moves with scores close to the best. Stops if all moves' scores are below a threshold.
- all - chooses the move with the highest score (first in the returned list). Only stops if there are no moves with scores.

* #### NoobBookLimit
Limit the use of NoobBook to the first x moves of the game. Only relevant with NoobBook set to true.

Expand Down
18 changes: 15 additions & 3 deletions src/noobprobe/noobprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@


bool NoobBook;
bool NoobModeBest = true;
int NoobLimit;
int failedQueries;


// Probes noobpwnftw's Chess Cloud Database
bool ProbeNoob(Position *pos) {
bool ProbeNoob(const Position *pos) {

// Stop querying after 3 failures or at the specified depth
if ( !NoobBook
Expand All @@ -43,9 +44,11 @@ bool ProbeNoob(Position *pos) {
puts("info string NoobBook: Querying chessdb.cn for a move...");

// Query dbcn
char *msg_fmt = "GET https://www.chessdb.cn/cdb.php?action=querybest&board=%s\n";
char *hostname = "www.chessdb.cn";
char *response = Query(hostname, msg_fmt, pos);
char *msg_fmt = "GET https://www.chessdb.cn/cdb.php?action=query%s&board=%s\n";
char message[256];
snprintf(message, 256, msg_fmt, NoobModeBest ? "best" : "all", BoardToFen(pos));
char *response = Query(hostname, message);

// On success the response will be "move:[MOVE]"
if (strstr(response, "move") != response)
Expand All @@ -57,3 +60,12 @@ bool ProbeNoob(Position *pos) {

return failedQueries = 0, true;
}

void NoobBookSetMode(const char *str) {
if (!strncmp(str, "best", strlen("best")))
NoobModeBest = true;
else if (!strncmp(str, "all", strlen("all")))
NoobModeBest = false;
else
puts("info string NoobBook: Valid modes are 'best' and 'all'");
}
3 changes: 2 additions & 1 deletion src/noobprobe/noobprobe.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ extern int NoobLimit;
extern int failedQueries;


bool ProbeNoob(Position *pos);
bool ProbeNoob(const Position *pos);
void NoobBookSetMode(const char *str);
6 changes: 4 additions & 2 deletions src/onlinesyzygy/onlinesyzygy.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ bool QueryRoot(const Position *pos, Move *move, unsigned *wdl, unsigned *dtz) {
puts("info string OnlineSyzygy: Querying lichess for a tablebase move...");

// Query lichess syzygy api
char *msg_fmt = "GET http://tablebase.lichess.ovh/standard?fen=%s\n";
char *hostname = "tablebase.lichess.ovh";
char *response = Query(hostname, msg_fmt, pos);
char *msg_fmt = "GET http://tablebase.lichess.ovh/standard?fen=%s\n";
char message[256];
snprintf(message, 256, msg_fmt, BoardToFen(pos));
char *response = Query(hostname, message);

// On success the response includes "uci": "[MOVE]"
if (strstr(response, "uci") == NULL)
Expand Down
11 changes: 3 additions & 8 deletions src/query/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,14 @@

static void error(const char *msg) { perror(msg); exit(0); }

char *Query(char *hostname, char *msg_fmt, const Position *pos) {
char *Query(char *hostname, char *message) {

// Setup sockets on windows, does nothing on linux
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
error("WSAStartup failed.");

// Make the message
char message[256] = "";
static char response[16384];

snprintf(message, 256, msg_fmt, BoardToFen(pos));

// Replace spaces with %20
// Replace spaces with +
char *ptr;
while ((ptr = strchr(message + 4, ' ')) != NULL)
*ptr = '+';
Expand Down Expand Up @@ -90,6 +84,7 @@ char *Query(char *hostname, char *msg_fmt, const Position *pos) {
error("ERROR sending");

// Receive response
static char response[16384];
memset(response, 0, sizeof(response));
if (recv(sockfd, response, sizeof(response), 0) == SOCKET_ERROR)
error("ERROR receiving");
Expand Down
2 changes: 1 addition & 1 deletion src/query/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
#include "../board.h"


char *Query(char *hostname, char *msg_fmt, const Position *pos);
char *Query(char *hostname, char *message);
2 changes: 2 additions & 0 deletions src/uci.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static void SetOption(char *str) {
else if (OptionNameIs("SyzygyPath" )) tb_init(optionValue);
else if (OptionNameIs("MultiPV" )) Limits.multiPV = IntValue;
else if (OptionNameIs("NoobBookLimit")) NoobLimit = IntValue;
else if (OptionNameIs("NoobBookMode" )) NoobBookSetMode(optionValue);
else if (OptionNameIs("NoobBook" )) NoobBook = BooleanValue;
else if (OptionNameIs("UCI_Chess960" )) Chess960 = BooleanValue;
else if (OptionNameIs("OnlineSyzygy" )) OnlineSyzygy = BooleanValue;
Expand All @@ -132,6 +133,7 @@ static void Info() {
printf("option name MultiPV type spin default 1 min 1 max %d\n", MULTI_PV_MAX);
printf("option name UCI_Chess960 type check default false\n");
printf("option name NoobBook type check default false\n");
printf("option name NoobBookMode string default <best>\n");
printf("option name NoobBookLimit type spin default 0 min 0 max 1000\n");
printf("option name OnlineSyzygy type check default false\n");
printf("uciok\n"); fflush(stdout);
Expand Down

0 comments on commit 25316d1

Please sign in to comment.