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

Merge branch '178-purge-the-useless-code' into 'develop'

Resolve "Purge the useless code"

Closes #178

See merge request !184
parents 83bd66de 2d31cf61
Pipeline #206694 passed with stages
in 43 minutes and 6 seconds
// tarot implements the rules of the tarot game
// Copyright (C) 2019 Vivien Kraus
// 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
......@@ -77,10 +77,8 @@ namespace Tarot {
GLib.Environment.set_variable ("SYSCONFDIR", absolute_sysconfdir, true);
GLib.Environment.set_variable ("LOCALEDIR", absolute_localedir, true);
GLib.Environment.set_variable ("DATADIR", absolute_datadir, true);
GLib.Environment.set_variable ("SAVEDGAMESDIR", absolute_localdatadir + "/" + PACKAGE + "/games", true);
stderr.printf ("Sysconfdir: '%s'\nLocaledir: '%s'\nDatadir: '%s'\nLocaldatadir: '%s'\n",
absolute_sysconfdir, absolute_localedir, absolute_datadir, absolute_localdatadir);
GLib.DirUtils.create_with_parents (GLib.Environment.get_variable ("SAVEDGAMESDIR"), 0755);
Tarot.set_datadir (absolute_datadir);
if (Tarot.init (absolute_localedir) != 0) {
stderr.printf (_ ("ERROR: could not initialize libtarot.\n"));
......
/* tarot implements the rules of the tarot game
* Copyright (C) 2019 Vivien Kraus
* 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
......@@ -16,23 +16,6 @@
#include "program_commons.h"
static char *read_stdin (void);
TarotGame *
tarot_program_read_game (void)
{
TarotGame *ret = tarot_game_alloc ();
char *input = read_stdin ();
if (tarot_game_load_from_xml (ret, input) == TAROT_GAME_OK)
{
free (input);
return ret;
}
fprintf (stderr, _("Error: invalid game.\n"));
tarot_game_free (ret);
exit (1);
}
void
tarot_program_setup (void)
{
......@@ -45,296 +28,3 @@ tarot_program_setup (void)
fprintf (stderr, _("ERROR: could not initialize libtarot.\n"));
}
}
static size_t parse_unit (const char *str, char **end);
size_t
tarot_strtosz (const char *str, char **end)
{
char *end_round;
char *end_flt;
size_t as_round = strtoul (str, &end_round, 0);
double as_flt = strtod (str, &end_flt);
size_t unit = 1;
if (end_round >= end_flt)
{
/* Keep as_round, because this is an integer */
unit = parse_unit (end_round, end);
return as_round * unit;
}
else
{
/* This is a decimal number */
unit = parse_unit (end_flt, end);
return (size_t) (as_flt * unit);
}
}
void
tarot_program_iterate (const TarotGame * game,
void (*event_handler) (const TarotGame *, TarotPlayer,
const TarotGameEvent *))
{
TarotGame *rebuilt[5] = { NULL, NULL, NULL, NULL, NULL };
size_t n_players = tarot_game_n_players (game);
assert (n_players <= 5);
size_t player;
for (player = 0; player < n_players; player++)
{
rebuilt[player] = tarot_game_alloc ();
}
TarotGameIterator *it = tarot_game_iterator (game);
const TarotGameEvent *event;
while ((event = tarot_game_iterator_next_value (it)) != NULL)
{
TarotPlayer next;
int has_next = 0;
for (player = 0; player < n_players; player++)
{
if (rebuilt[player] != NULL)
{
has_next =
(tarot_game_get_next (rebuilt[player], &next) ==
TAROT_GAME_OK);
}
}
switch (tarot_game_event_type (event))
{
case TAROT_DEAL_EVENT:
case TAROT_DEAL_ALL_EVENT:
for (player = 0; player < n_players; player++)
{
TarotCard hand[78];
size_t n_hand;
TarotGameEvent *deal_local;
if (tarot_game_get_deal_of (game, player, &n_hand, 0, 78, hand)
!= TAROT_GAME_OK)
{
/* We will not call event_handler on this player's
* decisions */
tarot_game_free (rebuilt[player]);
rebuilt[player] = NULL;
}
else
{
assert (n_hand < sizeof (hand) / sizeof (hand[0]));
deal_local =
tarot_game_event_alloc_deal (player, n_hand, hand);
if (tarot_game_add_event (rebuilt[player], deal_local) !=
TAROT_GAME_OK)
{
assert (0);
}
tarot_game_event_free (deal_local);
}
}
break;
case TAROT_DISCARD_EVENT:
if (1)
{
size_t n_full;
size_t n_part;
TarotCard full[78];
TarotCard part[78];
static const size_t max_full = sizeof (full) / sizeof (full[0]);
static const size_t max_part = sizeof (part) / sizeof (part[0]);
TarotPlayer taker;
TarotGameEvent *e_full;
TarotGameEvent *e_part;
tarot_game_get_taker (game, &taker);
if ((tarot_game_get_full_discard
(game, &n_full, 0, max_full, full) != TAROT_GAME_OK)
||
(tarot_game_get_public_discard
(game, &n_part, 0, max_part, part) != TAROT_GAME_OK))
{
/* We're iterating over the events of game, and the
* current event is a discard. So game has a
* discard. */
assert (0);
}
assert (n_full <= max_full);
assert (n_part <= max_part);
e_full = tarot_game_event_alloc_discard (n_full, full);
e_part = tarot_game_event_alloc_discard (n_part, part);
assert (has_next);
assert (next == taker);
event_handler (rebuilt[taker], taker, e_full);
for (player = 0; player < n_players; player++)
{
if (rebuilt[player] != NULL && player == taker)
{
if (tarot_game_add_event (rebuilt[player], e_full) !=
TAROT_GAME_OK)
{
assert (0);
}
}
else if (rebuilt[player] != NULL)
{
if (tarot_game_add_event (rebuilt[player], e_part) !=
TAROT_GAME_OK)
{
assert (0);
}
}
}
tarot_game_event_free (e_full);
tarot_game_event_free (e_part);
}
break;
default:
if (has_next && rebuilt[next] != NULL)
{
event_handler (rebuilt[next], next, event);
}
for (player = 0; player < n_players; player++)
{
if (rebuilt[player] != NULL
&& tarot_game_add_event (rebuilt[player],
event) != TAROT_GAME_OK)
{
assert (0);
}
}
break;
}
}
tarot_game_iterator_free (it);
for (player = 0; player < n_players; player++)
{
tarot_game_free (rebuilt[player]);
}
}
struct buffer;
typedef struct buffer buffer;
static buffer *buffer_alloc (void);
static void buffer_free (buffer * buffer);
static void buffer_push_back (buffer * b, char c);
static char *buffer_data (buffer * b); /* NOT nul-terminated */
static char *
read_stdin (void)
{
buffer *buffer = buffer_alloc ();
int c;
char *ret;
while ((c = getchar ()) != EOF)
{
unsigned int u = c;
buffer_push_back (buffer, (unsigned char) u);
}
buffer_push_back (buffer, '\0');
ret = strdup (buffer_data (buffer));
buffer_free (buffer);
return ret;
}
struct buffer
{
size_t allocated;
size_t used;
char *data;
};
static buffer *
buffer_alloc (void)
{
buffer *ret = xmalloc (sizeof (buffer));
ret->allocated = 1;
ret->used = 0;
ret->data = xmalloc (1);
return ret;
}
static void
buffer_free (buffer * buffer)
{
free (buffer->data);
free (buffer);
}
static void
buffer_push_back (buffer * b, char c)
{
if (b->used >= b->allocated)
{
b->allocated *= 2;
b->data = xrealloc (b->data, b->allocated);
}
assert (b->used < b->allocated);
b->data[b->used++] = c;
}
static char *
buffer_data (buffer * b)
{
return b->data;
}
static int parse_whitespace (const char *str, char **end);
#define UNIT_B (1UL)
#define UNIT_KiB (1024UL * UNIT_B)
#define UNIT_MiB (1024UL * UNIT_KiB)
#define UNIT_GiB (1024UL * UNIT_MiB)
static size_t
parse_unit (const char *str, char **end)
{
char *my_end = (char *) str;
char *test;
if (parse_whitespace (my_end, &my_end))
{
return parse_unit (my_end, end);
}
test = _("GiB");
if (strncmp (my_end, test, strlen (test)) == 0)
{
*end = my_end + strlen (test);
return UNIT_GiB;
}
test = _("MiB");
if (strncmp (my_end, test, strlen (test)) == 0)
{
*end = my_end + strlen (test);
return UNIT_MiB;
}
test = _("KiB");
if (strncmp (my_end, test, strlen (test)) == 0)
{
*end = my_end + strlen (test);
return UNIT_KiB;
}
test = _("B");
if (strncmp (my_end, test, strlen (test)) == 0)
{
*end = my_end + strlen (test);
return UNIT_B;
}
*end = my_end;
return UNIT_B;
}
static int
parse_whitespace (const char *str, char **end)
{
if (strncmp (str, " ", strlen (" ")) == 0)
{
if (end)
{
*end = ((char *) str) + strlen (" ");
}
return 1;
}
if (strncmp (str, " ", strlen (" ")) == 0)
{
if (end)
{
*end = ((char *) str) + strlen (" ");
}
return 1;
}
return 0;
}
/* tarot implements the rules of the tarot game
* Copyright (C) 2019 Vivien Kraus
* 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
......@@ -31,11 +31,5 @@
#define _(String) gettext (String)
#define N_(String) String
TarotGame *tarot_program_read_game (void);
void tarot_program_iterate (const TarotGame * game,
void (*event_handler) (const TarotGame *,
TarotPlayer,
const TarotGameEvent *));
void tarot_program_setup (void);
size_t tarot_strtosz (const char *str, char **end);
#endif /* not H_PROGRAM_COMMONS_INCLUDED */
# 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-cnn-features
%canon_reldir%_tarot_cnn_features_SOURCES = \
%reldir%/tarot_cnn_features.c
%canon_reldir%_tarot_cnn_features_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(AM_LDADD)
INDENTED += %reldir%/tarot_cnn_features.c
# 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-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 \
$(AM_LDADD)
INDENTED += %reldir%/tarot_cnn.c
# 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-deal
%canon_reldir%_tarot_deal_SOURCES = \
%reldir%/tarot_deal.c
%canon_reldir%_tarot_deal_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(AM_LDADD)
INDENTED += %reldir%/tarot_deal.c
# 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-features
%canon_reldir%_tarot_features_SOURCES = \
%reldir%/tarot_features.c
%canon_reldir%_tarot_features_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(AM_LDADD)
INDENTED += %reldir%/tarot_features.c
# 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-mcts
%canon_reldir%_tarot_mcts_SOURCES = \
%reldir%/tarot_mcts.c
%canon_reldir%_tarot_mcts_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(AM_LDADD)
INDENTED += %reldir%/tarot_mcts.c
# 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-stacking
%canon_reldir%_tarot_stacking_SOURCES = \
%reldir%/tarot_stacking.c
%canon_reldir%_tarot_stacking_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(AM_LDADD)
INDENTED += %reldir%/tarot_stacking.c
# 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 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 ();
}