Moteur : videau de l'ordinateur comme ComputerTurn, match après une partie, Match ID d'un match

Fenêtre de marché et double facultatif à 0 pli (play.c), réponse au
double, règle de Crawford automatique ; commandes double, take et
aftergame d'engine-cli, matchid étendu aux matchs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pierre MARTIN
2026-11-09 18:42:35 +01:00
co-authored by Claude Opus 5.5
parent 71bff4def1
commit c0f827c5ba
4 changed files with 393 additions and 19 deletions
+116 -18
View File
@@ -19,9 +19,19 @@
* vérifie des demi-coups saisis un par
* un (ex. « 8/5 6/5 », « bar/22 »,
* « 6/off »)
* matchid VIDEAU PROPRIÉTAIRE Match ID d'une partie libre avec la
* règle Jacoby, joueur 0 au trait ;
* matchid VIDEAU PROPRIÉTAIRE [LONGUEUR SCORE0 SCORE1 CRAWFORD]
* Match ID joueur 0 au trait : partie
* libre avec la règle Jacoby, ou match
* en LONGUEUR points (CRAWFORD : 1
* pour la partie Crawford, sinon 0) ;
* PROPRIÉTAIRE : -1 (centre), 0 ou 1
* double POSITION_ID MATCH_ID (avec --level) l'ordinateur au trait
* double-t-il ? (ComputerTurn de gnubg)
* take POSITION_ID MATCH_ID (avec --level) l'ordinateur prend-il
* le double du joueur au trait ?
* aftergame MATCH_ID GAGNANT POINTS le match après une partie : score,
* fin du match, partie Crawford
* suivante
* reference POSITION_ID MATCH_ID [DÉS]
* un niveau au format des positions de
* référence (tests/reference/README.md) :
@@ -42,8 +52,9 @@
*
* --ply : niveau de gnubg, de 0 (défaut) à 4 (meilleur coup comme « hint »,
* analyses comme l'analyse de partie de gnubg).
* --level : niveau de jeu de l'ordinateur (voir levels), pour bestmove, moves
* et cube : coups classés comme l'ordinateur de gnubg les joue, bruit compris.
* --level : niveau de jeu de l'ordinateur (voir levels), pour bestmove, moves,
* cube, double et take : coups classés et videau comme l'ordinateur de gnubg
* les joue, bruit compris.
* --data : dossier des données ; par défaut « data » à côté du programme.
*
* En WebAssembly (engine-wasm/), le même programme sert d'interface : le
@@ -71,13 +82,13 @@
/* Liste des commandes, pour les messages d'erreur. */
#define COMMANDS "bestmove, moves, legal, cube, eval, position, halfmoves, matchid, reference, " \
"levels, analysemove, analysecube ou luck"
"levels, analysemove, analysecube, luck, double, take ou aftergame"
/* Coups gardés dans le classement de la commande reference (comme hint). */
#define REFERENCE_MAX_MOVES 10
/* Arguments de la ligne de commande, une fois lus. */
#define MAX_ARGS 4
#define MAX_ARGS 6
typedef struct {
int ply;
@@ -125,6 +136,8 @@ status_code(engine_status status)
return "invalid_move";
case ENGINE_ERROR_CUBE_ACTION:
return "invalid_cube_action";
case ENGINE_ERROR_RESULT:
return "invalid_result";
}
return "unknown_error";
}
@@ -475,24 +488,45 @@ run_half_moves_command(const options *opt)
return EXIT_SUCCESS;
}
/* matchid : Match ID d'une partie libre avec la règle Jacoby, joueur 0 au
* trait. */
/* Lit un entier ; renvoie 0 si le texte n'en est pas un. */
static int
parse_int(const char *text, int *value)
{
char *end;
*value = (int) strtol(text, &end, 10);
return !*end && end != text;
}
/* matchid : Match ID joueur 0 au trait, sans dés. Partie libre avec la
* règle Jacoby, ou match (LONGUEUR SCORE0 SCORE1 CRAWFORD, ce dernier à 0
* ou 1), sans Jacoby. */
static int
run_match_id_command(const options *opt)
{
engine_match match = { .cube_value = 1, .cube_owner = -1, .jacoby = 1 };
char match_id[13];
char *end;
engine_status status;
if (opt->arg_count != 2)
return usage_error("arguments attendus : VIDEAU PROPRIÉTAIRE");
match.cube_value = (int) strtol(opt->args[0], &end, 10);
if (*end || end == opt->args[0])
if (opt->arg_count != 2 && opt->arg_count != 6)
return usage_error("arguments attendus : VIDEAU PROPRIÉTAIRE "
"[LONGUEUR SCORE0 SCORE1 CRAWFORD]");
if (!parse_int(opt->args[0], &match.cube_value))
return usage_error("VIDEAU attend un nombre");
match.cube_owner = (int) strtol(opt->args[1], &end, 10);
if (*end || end == opt->args[1])
if (!parse_int(opt->args[1], &match.cube_owner))
return usage_error("PROPRIÉTAIRE attend -1, 0 ou 1");
if (opt->arg_count == 6) {
if (!parse_int(opt->args[2], &match.match_length) || match.match_length < 1
|| !parse_int(opt->args[3], &match.score[0]) || match.score[0] < 0
|| !parse_int(opt->args[4], &match.score[1]) || match.score[1] < 0
|| match.score[0] >= match.match_length || match.score[1] >= match.match_length)
return usage_error("LONGUEUR attend un nombre positif, SCORE0 et SCORE1 des "
"scores inférieurs à la longueur");
if (!parse_int(opt->args[5], &match.crawford) || match.crawford < 0
|| match.crawford > 1)
return usage_error("CRAWFORD attend 0 ou 1");
match.jacoby = 0;
}
status = engine_encode_match(&match, match_id);
if (status != ENGINE_OK)
@@ -503,8 +537,67 @@ run_match_id_command(const options *opt)
return EXIT_SUCCESS;
}
/* double et take (avec --level) : le videau de l'ordinateur, comme
* ComputerTurn de gnubg (voir engine.h). */
static int
run_level_cube_command(const options *opt)
{
const char *position_id = opt->args[0], *match_id = opt->args[1];
engine_status status;
if (opt->arg_count != 2)
return usage_error("arguments attendus : POSITION_ID MATCH_ID");
if (opt->level < 0)
return usage_error("double et take demandent --level");
if (strcmp(opt->command, "double") == 0) {
engine_level_double d;
status = engine_level_double_decision(position_id, match_id, opt->level, &d);
if (status != ENGINE_OK)
return print_engine_error(status);
printf("{\"position_id\": \"%s\", \"match_id\": \"%s\"", position_id, match_id);
print_level_or_ply(opt);
printf(", \"available\": %s, \"market_window\": %s, \"double\": %s, \"decision\": ",
d.available ? "true" : "false", d.market_window ? "true" : "false",
d.doubles ? "true" : "false");
if (d.decision)
printf("\"%s\"}\n", d.decision);
else
printf("null}\n");
} else {
int takes;
const char *decision;
status = engine_level_take_decision(position_id, match_id, opt->level, &takes, &decision);
if (status != ENGINE_OK)
return print_engine_error(status);
printf("{\"position_id\": \"%s\", \"match_id\": \"%s\"", position_id, match_id);
print_level_or_ply(opt);
printf(", \"take\": %s, \"decision\": \"%s\"}\n", takes ? "true" : "false", decision);
}
return EXIT_SUCCESS;
}
/* aftergame : le match après une partie (voir engine_match_after_game). */
static int
run_after_game_command(const options *opt)
{
engine_match_after after;
engine_status status;
int winner, points;
if (opt->arg_count != 3)
return usage_error("arguments attendus : MATCH_ID GAGNANT POINTS");
if (!parse_int(opt->args[1], &winner) || !parse_int(opt->args[2], &points))
return usage_error("GAGNANT et POINTS attendent des nombres");
status = engine_match_after_game(opt->args[0], winner, points, &after);
if (status != ENGINE_OK)
return print_engine_error(status);
printf("{\"score\": [%d, %d], \"match_over\": %s, \"crawford\": %s}\n", after.score[0],
after.score[1], after.match_over ? "true" : "false", after.crawford ? "true" : "false");
return EXIT_SUCCESS;
}
/*
* reference : un niveau d'un fichier de résultat de tests/reference/, de
* reference :un niveau d'un fichier de résultat de tests/reference/, de
* même structure que plies["<n>"] (voir tests/reference/README.md).
*/
static int
@@ -731,8 +824,9 @@ parse_options(int argc, char **argv, options *opt)
if (opt->level >= 0 && has_ply)
return "--ply et --level ne vont pas ensemble";
if (opt->level >= 0 && strcmp(opt->command, "bestmove") != 0
&& strcmp(opt->command, "moves") != 0 && strcmp(opt->command, "cube") != 0)
return "--level ne s'applique qu'à bestmove, moves et cube";
&& strcmp(opt->command, "moves") != 0 && strcmp(opt->command, "cube") != 0
&& strcmp(opt->command, "double") != 0 && strcmp(opt->command, "take") != 0)
return "--level ne s'applique qu'à bestmove, moves, cube, double et take";
return NULL;
}
@@ -772,6 +866,10 @@ main(int argc, char **argv)
result = run_half_moves_command(&opt);
else if (strcmp(opt.command, "matchid") == 0)
result = run_match_id_command(&opt);
else if (strcmp(opt.command, "double") == 0 || strcmp(opt.command, "take") == 0)
result = run_level_cube_command(&opt);
else if (strcmp(opt.command, "aftergame") == 0)
result = run_after_game_command(&opt);
else if (strcmp(opt.command, "levels") == 0)
result = run_levels_command(&opt);
else if (strcmp(opt.command, "analysemove") == 0)