Commit 03670104 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Purge the XML code

parent c509fa11
# tarot implements the rules of the tarot game
# Copyright (C) 2019, 2020 Vivien Kraus
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
bin_PROGRAMS += %reldir%/tarot-status
%canon_reldir%_tarot_status_SOURCES = \
%reldir%/tarot_status.c
%canon_reldir%_tarot_status_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(AM_LDADD)
INDENTED += %reldir%/tarot_status.c
/* tarot implements the rules of the tarot game
* Copyright (C) 2019 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "program_commons.h"
static char *default_seed = "default";
static size_t n_players = 4;
static int with_call = 0;
static char **seed = &default_seed;
static void parse_options (int argc, char *argv[]);
int
main (int argc, char *argv[])
{
TarotGame *game = NULL;
TarotGameEvent *setup_event;
TarotGameEvent *deal_event;
char *output;
tarot_program_setup ();
parse_options (argc, argv);
game = tarot_game_alloc ();
setup_event = tarot_game_event_alloc_setup (n_players, with_call);
deal_event =
tarot_game_event_alloc_deal_all_random (n_players, strlen (*seed), *seed);
if (tarot_game_add_event (game, setup_event) != TAROT_GAME_OK)
{
fprintf (stderr,
_
("Error: could not set the game up for %lu players (call: %d)\n"),
n_players, with_call);
return EXIT_FAILURE;
}
if (tarot_game_add_event (game, deal_event) != TAROT_GAME_OK)
{
fprintf (stderr,
_
("Error: could not use this random seed, '%s', to deal. Please retry with a different seed.\n"),
*seed);
return EXIT_FAILURE;
}
output = tarot_game_save_to_xml_alloc (game);
printf ("%s", output);
return EXIT_SUCCESS;
}
static void
parse_options (int argc, char *argv[])
{
int c;
char *end;
while (1)
{
static struct option long_options[] = {
{N_("help"), no_argument, 0, 'h'},
{N_("version"), no_argument, 0, 'v'},
{N_("n-players"), required_argument, 0, 'n'},
{N_("call-allowed"), no_argument, 0, 'c'},
{N_("no-call-allowed"), no_argument, &with_call, 0},
{N_("seed"), required_argument, 0, 's'},
{0, 0, 0, 0}
};
int option_index = 0;
for (option_index = 0; long_options[option_index].name != NULL;
option_index++)
{
long_options[option_index].name =
_(long_options[option_index].name);
}
option_index = 0;
c = getopt_long (argc, argv, "hvn:cs:", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'h':
printf (_("Usage: tarot-deal [OPTIONS]...\n\
Create a new game with a random deal for all players. Options:\n\
- -h, --help: print this message and exit\n\
- -v, --version: print the program version and exit\n\
- -n N_PLAYERS, --n-players=N_PLAYERS: set the number of players\n\
(default: %lu)\n\
- -c, --call-allowed: the game allows a call\n\
- --no-call-allowed: the game does not allow a call (default)\n\
- -s, --seed=\"SEED\": set the seed for the random deal. SEED must\n\
not be empty.\n\
\n\
If the seed leads to a petit sec, then the deal fails. So in case of\n\
a failure you may want to retry for a few different seeds.\n\
"), n_players);
exit (EXIT_SUCCESS);
break;
case 'v':
printf (_("%s (libtarot %s)\n\
\n\
Copyright © 2019 Vivien Kraus\nThis is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), "tarot-deal", tarot_libtarot_version ());
exit (EXIT_SUCCESS);
break;
case 'n':
n_players = strtoul (optarg, &end, 10);
if (strcmp (end, optarg) == 0)
{
fprintf (stderr,
_
("Error: the number of players should be a number, not '%s'.\n"),
optarg);
exit (EXIT_FAILURE);
}
else if (strcmp (end, "") != 0)
{
fprintf (stderr,
_
("Error: the number of players (%lu) has an unexpected suffix, '%s': '%s'.\n"),
n_players, end, optarg);
exit (EXIT_FAILURE);
}
break;
case 'c':
with_call = 1;
break;
case 's':
*seed = strdup (optarg);
if (strlen (*seed) == 0)
{
fprintf (stderr,
_
("Error: the seed must not be empty. The default is usually \"default\".\n"));
exit (EXIT_FAILURE);
}
break;
case '?':
/* getopt_long already printed an error message. */
exit (EXIT_FAILURE);
break;
default:
abort ();
}
}
if (optind < argc)
{
fprintf (stderr, _("Error: extra arguments:\n"));
while (optind < argc)
fprintf (stderr, _("- %s\n"), argv[optind++]);
exit (EXIT_FAILURE);
}
}
/* tarot implements the rules of the tarot game
* Copyright (C) 2019 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "program_commons.h"
static char *default_seed = "default";
static size_t mem = 2 * 1024 * 1024;
static char **seed = &default_seed;
static size_t n_iterations = 100;
static int generate_whole_game = 0;
static void parse_options (int argc, char *argv[]);
static void check_error (TarotMctsError err);
int
main (int argc, char *argv[])
{
TarotGame *game = NULL;
char *output;
TarotMcts *mcts = NULL;
const TarotGameEvent *event = NULL;
TarotGameEvent *dog_event;
size_t n_cards;
TarotCard *cards;
tarot_program_setup ();
parse_options (argc, argv);
game = tarot_program_read_game ();
mcts = tarot_mcts_alloc (mem);
tarot_mcts_seed (mcts, strlen (*seed), *seed);
while (1)
{
if (generate_whole_game)
{
TarotPlayer next;
if (tarot_game_get_next (game, &next) != TAROT_GAME_OK)
{
break;
}
}
check_error (tarot_mcts_set_base (mcts, game));
check_error (tarot_mcts_run (mcts, n_iterations));
event = tarot_mcts_get_best (mcts);
if (tarot_game_add_event (game, event) != TAROT_GAME_OK)
{
abort ();
}
if (tarot_game_can_autoreveal_alloc (game, &n_cards, &cards))
{
dog_event = tarot_game_event_alloc_dog (n_cards, cards);
if (tarot_game_add_event (game, dog_event) != TAROT_GAME_OK)
{
abort ();
}
}
if (!generate_whole_game)
{
break;
}
}
output = tarot_game_save_to_xml_alloc (game);
printf ("%s", output);
return EXIT_SUCCESS;
}
static void
parse_options (int argc, char *argv[])
{
int c;
char *end;
while (1)
{
static struct option long_options[] = {
{N_("help"), no_argument, 0, 'h'},
{N_("version"), no_argument, 0, 'v'},
{N_("max-memory"), required_argument, 0, 'm'},
{N_("seed"), required_argument, 0, 's'},
{N_("n-iterations"), required_argument, 0, 'i'},
{N_("mcts-parameter"), required_argument, 0, 'p'},
{N_("parameter-random"), required_argument, 0, 'r'},
{N_("parameter-agreed"), required_argument, 0, 'a'},
{N_("generate"), no_argument, 0, 'g'},
{0, 0, 0, 0}
};
int option_index = 0;
for (option_index = 0; long_options[option_index].name != NULL;
option_index++)
{
long_options[option_index].name =
_(long_options[option_index].name);
}
option_index = 0;
c = getopt_long (argc, argv, "hvm:s:i:g", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'h':
printf (_("Usage: tarot-mcts [OPTIONS]... < GAME\n\
Play as the MCTS in GAME. Options:\n\
- -h, --help: print this message and exit;\n\
- -v, --version: print the program version and exit;\n\
- -m SZ, --max-memory=SZ: define the maximum memory (default: %lu B).\n\
You can use the following suffixes: 'GiB', 'MiB', 'KiB', or 'B'.\n\
By default, numbers will be treated as bytes;\n\
- -s SEED, --seed=\"SEED\": set the seed for the random deal. SEED\n\
must not be empty;\n\
- -i N, --n-iterations=N: set the number of iterations. The higher\n\
the better. Default: %lu;\n\
- -g, --generate: play with the same player parameters until the end\n\
of the game.\n\
\n\
If the number of iterations is too low, or the algorithm is extremely\n\
unlucky, then the operation will fail.\n\
"), mem, n_iterations);
exit (EXIT_SUCCESS);
break;
case 'v':
printf (_("%s (libtarot %s)\n\
\n\
Copyright © 2019 Vivien Kraus\nThis is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), "tarot-mcts", tarot_libtarot_version ());
exit (EXIT_SUCCESS);
break;
case 'm':
mem = tarot_strtosz (optarg, &end);
if (strcmp (end, optarg) == 0)
{
fprintf (stderr,
_
("Error: the memory limit should be a size in bytes, not '%s'.\n"),
optarg);
exit (EXIT_FAILURE);
}
else if (strcmp (end, "") != 0)
{
fprintf (stderr,
_
("Error: the memory limit (%lu bytes) has an unexpected suffix, '%s': '%s'.\n"),
mem, end, optarg);
exit (EXIT_FAILURE);
}
break;
case 's':
*seed = strdup (optarg);
if (strlen (*seed) == 0)
{
fprintf (stderr,
_
("Error: the seed must not be empty. The default is usually \"default\".\n"));
exit (EXIT_FAILURE);
}
break;
case 'i':
n_iterations = strtoul (optarg, &end, 10);
if (strcmp (end, optarg) == 0)
{
fprintf (stderr,
_
("Error: the number of iterations should be a number, not '%s'.\n"),
optarg);
exit (EXIT_FAILURE);
}
else if (strcmp (end, "") != 0)
{
fprintf (stderr,
_
("Error: the number of iterations (%lu) has an unexpected suffix, '%s': '%s'.\n"),
n_iterations, end, optarg);
exit (EXIT_FAILURE);
}
break;
case 'g':
generate_whole_game = 1;
break;
case '?':
/* getopt_long already printed an error message. */
exit (EXIT_FAILURE);
break;
default:
abort ();
}
}
if (optind < argc)
{
fprintf (stderr, _("Error: extra arguments:\n"));
while (optind < argc)
fprintf (stderr, _("- %s\n"), argv[optind++]);
exit (EXIT_FAILURE);
}
}
static void
check_error (TarotMctsError err)
{
switch (err)
{
case TAROT_MCTS_NONEXT:
fprintf (stderr, _("Error: no player needs to play in this game.\n"));
exit (EXIT_FAILURE);
case TAROT_MCTS_UNKNOWNNEXT:
fprintf (stderr,
_("Error: we don't know the cards of the next player.\n"));
exit (EXIT_FAILURE);
case TAROT_MCTS_NODATA:
fprintf (stderr,
_
("Error: too few experiments. Please use more iterations.\n"));
exit (EXIT_FAILURE);
case TAROT_MCTS_NOMEM:
fprintf (stderr,
_
("Error: memory exceeded. Please pass --max-memory=SIZE.\n"));
exit (EXIT_FAILURE);
case TAROT_MCTS_OK:
break;
}
}
/* tarot implements the rules of the tarot game
* Copyright (C) 2019, 2020 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "program_commons.h"
int
main (int argc, char *argv[])
{
TarotGame *game = NULL;
tarot_program_setup ();
if (argc != 2)
{
fprintf (stderr, _("Usage:\n\
- tarot-status --help\n\
- tarot-status --version\n\
- tarot-status next < GAME\n\
- tarot-status at-end < GAME\n\
- tarot-status score < GAME\n\
Please run tarot-status --help.\n"));
return EXIT_FAILURE;
}
if (strcmp (argv[1], _("--help")) == 0)
{
printf (_("Usage: tarot-status [OPTION]... ([COMMAND] < GAME)?\n\
Print some status information of GAME. Options are:\n\
- --help: print this message and exit\n\
- --version: print program version and exit\n\
\n\
Commands:\n\
- next: print the number of the next player. The number of a player\n\
is the number of players that have made a bid before this player in\n\
the bidding phase;\n\
- at-end: exit sucessfully if the game is finished, or exit with code\n\
1 if the game is not finished;\n\
- score: for a finished game, print the score, in the order of player\n\
numbers, one line for each player;\n"));
return EXIT_SUCCESS;
}
if (strcmp (argv[1], _("--version")) == 0)
{
printf (_("%s (libtarot %s)\n\
\n\
Copyright © 2019 Vivien Kraus\nThis is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), "tarot-status", tarot_libtarot_version ());
return EXIT_SUCCESS;
}
game = tarot_program_read_game ();
if (strcmp (argv[1], _("next")) == 0)
{
TarotPlayer next;
if (tarot_game_get_next (game, &next) == TAROT_GAME_OK)
{
printf ("%u\n", next);
tarot_game_free (game);
return EXIT_SUCCESS;
}
else
{
fprintf (stderr,
_("Error: all players have played in this game.\n"));
tarot_game_free (game);
return EXIT_FAILURE;
}
}
if (strcmp (argv[1], _("at-end")) == 0)
{
size_t n_scores;
if (tarot_game_get_scores (game, &n_scores, 0, 0, NULL) ==
TAROT_GAME_OK)
{
tarot_game_free (game);
return EXIT_SUCCESS;
}
tarot_game_free (game);
return EXIT_FAILURE;
}
if (strcmp (argv[1], _("score")) == 0)
{
size_t n_scores;
int i;
int *scores;
if (tarot_game_get_scores_alloc (game, &n_scores, &scores) !=
TAROT_GAME_OK)
{
fprintf (stderr, _("Error: this game is not finished yet.\n"));
tarot_game_free (game);
return EXIT_FAILURE;
}
for (i = 0; i < (int) n_scores; i++)
{
printf ("%d\n", scores[i]);
}
tarot_game_free (game);
return EXIT_SUCCESS;
}
fprintf (stderr, _("Error: unknown command '%s'.\n"), argv[1]);
tarot_game_free (game);
return EXIT_FAILURE;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment