Commit 2db36368 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Create a tarot-cnn program to play as the CNN

parent 3ff4a63d
Pipeline #162156 passed with stages
in 27 minutes and 24 seconds
......@@ -46,6 +46,7 @@
/doc/players-and-cards
/doc/players-and-cards.c
/doc/tarot-cnn-features.man
/doc/tarot-cnn.man
/doc/tarot-deal.man
/doc/tarot-features.man
/doc/tarot-figures
......
......@@ -23,7 +23,8 @@ dist_man6_MANS += \
%reldir%/tarot-mcts.man \
%reldir%/tarot-stacking.man \
%reldir%/tarot-features.man \
%reldir%/tarot-cnn-features.man
%reldir%/tarot-cnn-features.man \
%reldir%/tarot-cnn.man
MAINTAINERCLEANFILES += \
$(srcdir)/%reldir%/tarot.man \
$(srcdir)/%reldir%/tarot-status.man \
......@@ -31,7 +32,8 @@ MAINTAINERCLEANFILES += \
$(srcdir)/%reldir%/tarot-mcts.man \
$(srcdir)/%reldir%/tarot-stacking.man \
$(srcdir)/%reldir%/tarot-features.man \
$(srcdir)/%reldir%/tarot-cnn-features.man
$(srcdir)/%reldir%/tarot-cnn-features.man \
$(srcdir)/%reldir%/tarot-cnn.man
help2man_verbose = $(help2man_verbose_@AM_V@)
help2man_verbose_ = $(help2man_verbose_@AM_DEFAULT_V@)
......@@ -64,3 +66,7 @@ $(srcdir)/%reldir%/tarot-features.man: src/tarot-app/tarot_features.c
$(srcdir)/%reldir%/tarot-cnn-features.man: src/tarot-app/tarot_cnn_features.c
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot-cnn-features
$(help2man_verbose) ./pre-inst-env help2man tarot-cnn-features > $@
$(srcdir)/%reldir%/tarot-cnn.man: src/tarot-app/tarot_cnn.c
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot-cnn
$(help2man_verbose) ./pre-inst-env help2man tarot-cnn > $@
......@@ -27,3 +27,4 @@ include %reldir%/tarot-mcts.am
include %reldir%/tarot-stacking.am
include %reldir%/tarot-features.am
include %reldir%/tarot-cnn-features.am
include %reldir%/tarot-cnn.am
# tarot-cnn.am
#
# Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
#
# 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, either version 3 of the
# License, or (at your option) any later version.
#
# 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-cnn
%canon_reldir%_tarot_cnn_SOURCES = \
%reldir%/tarot_cnn.c
%canon_reldir%_tarot_cnn_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(LTLIBINTL) \
$(LTLIBTHREAD) \
@INTL_MACOSX_LIBS@
/*
* tarot_cnn.c
*
* Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
*
* 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, either version 3 of the
* License, or (at your option) any later version.
*
* 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"
#include <tuneconfig.h>
static int generate_whole_game = 0;
static void parse_options (int argc, char *argv[]);
int
main (int argc, char *argv[])
{
TarotGame *game = NULL;
char *output;
TarotPredictor *cnn = 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 ();
cnn = tarot_cnn_predictor_alloc ();
while (1)
{
double score;
if (generate_whole_game)
{
TarotPlayer next;
if (tarot_game_get_next (game, &next) != TAROT_GAME_OK)
{
break;
}
}
tarot_predictor_set_game (cnn, game);
event = tarot_predictor_best (cnn, &score);
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;
while (1)
{
static struct option long_options[] =
{
{N_ ("help"), no_argument, 0, 'h'},
{N_ ("version"), no_argument, 0, 'v'},
{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, "hvg", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'h':
printf (_ ("Usage: tarot-cnn [OPTIONS]... < GAME\n\
Play as the deep learner in GAME. Options:\n\
- -h, --help: print this message and exit;\n\
- -v, --version: print the program version and exit;\n\
- -g, --generate: play with the same player parameters until the end\n\
of the game.\n\
"));
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-cnn",
tarot_libtarot_version ());
exit (EXIT_SUCCESS);
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);
}
}
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