tarot_status.c 3.81 KB
Newer Older
1
2
/* tarot implements the rules of the tarot game
 * Copyright (C) 2019  Vivien Kraus
Vivien Kraus's avatar
Vivien Kraus committed
3
 *
4
5
 * 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
6
 * the Free Software Foundation, version 3 of the License.
Vivien Kraus's avatar
Vivien Kraus committed
7
8
9
10
11
12
13
 *
 * 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
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Vivien Kraus's avatar
Vivien Kraus committed
15
16
 */

17
#include "program_commons.h"
Vivien Kraus's avatar
Vivien Kraus committed
18
19
20
21
22

int
main (int argc, char *argv[])
{
  TarotGame *game = NULL;
23
  tarot_program_setup ();
Vivien Kraus's avatar
Vivien Kraus committed
24
25
  if (argc != 2)
    {
Vivien Kraus's avatar
Vivien Kraus committed
26
      fprintf (stderr, _("Usage:\n\
Vivien Kraus's avatar
Vivien Kraus committed
27
28
29
30
31
- tarot-status --help\n\
- tarot-status --version\n\
- tarot-status next < GAME\n\
- tarot-status at-end < GAME\n\
- tarot-status score < GAME\n\
32
- tarot-status errors < GAME\n\
Vivien Kraus's avatar
Vivien Kraus committed
33
34
35
Please run tarot-status --help.\n"));
      return EXIT_FAILURE;
    }
Vivien Kraus's avatar
Vivien Kraus committed
36
  if (strcmp (argv[1], _("--help")) == 0)
Vivien Kraus's avatar
Vivien Kraus committed
37
    {
Vivien Kraus's avatar
Vivien Kraus committed
38
      printf (_("Usage: tarot-status [OPTION]... ([COMMAND] < GAME)?\n\
Vivien Kraus's avatar
Vivien Kraus committed
39
40
41
42
43
44
45
46
47
48
49
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\
50
51
52
     numbers, one line for each player;\n\
- errors: print the sum of all errors that all known players made in\n\
     the game.\n"));
Vivien Kraus's avatar
Vivien Kraus committed
53
54
      return EXIT_SUCCESS;
    }
Vivien Kraus's avatar
Vivien Kraus committed
55
  if (strcmp (argv[1], _("--version")) == 0)
Vivien Kraus's avatar
Vivien Kraus committed
56
    {
Vivien Kraus's avatar
Vivien Kraus committed
57
      printf (_("%s (libtarot %s)\n\
Vivien Kraus's avatar
Vivien Kraus committed
58
\n\
Vivien Kraus's avatar
Vivien Kraus committed
59
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 ());
Vivien Kraus's avatar
Vivien Kraus committed
60
61
      return EXIT_SUCCESS;
    }
62
  game = tarot_program_read_game ();
Vivien Kraus's avatar
Vivien Kraus committed
63
  if (strcmp (argv[1], _("next")) == 0)
Vivien Kraus's avatar
Vivien Kraus committed
64
65
66
    {
      TarotPlayer next;
      if (tarot_game_get_next (game, &next) == TAROT_GAME_OK)
Vivien Kraus's avatar
Vivien Kraus committed
67
68
69
70
71
        {
          printf ("%u\n", next);
          tarot_game_free (game);
          return EXIT_SUCCESS;
        }
Vivien Kraus's avatar
Vivien Kraus committed
72
      else
Vivien Kraus's avatar
Vivien Kraus committed
73
74
75
76
77
78
        {
          fprintf (stderr,
                   _("Error: all players have played in this game.\n"));
          tarot_game_free (game);
          return EXIT_FAILURE;
        }
Vivien Kraus's avatar
Vivien Kraus committed
79
    }
Vivien Kraus's avatar
Vivien Kraus committed
80
  if (strcmp (argv[1], _("at-end")) == 0)
Vivien Kraus's avatar
Vivien Kraus committed
81
82
    {
      size_t n_scores;
Vivien Kraus's avatar
Vivien Kraus committed
83
84
85
86
87
88
      if (tarot_game_get_scores (game, &n_scores, 0, 0, NULL) ==
          TAROT_GAME_OK)
        {
          tarot_game_free (game);
          return EXIT_SUCCESS;
        }
Vivien Kraus's avatar
Vivien Kraus committed
89
      tarot_game_free (game);
Vivien Kraus's avatar
Vivien Kraus committed
90
91
      return EXIT_FAILURE;
    }
Vivien Kraus's avatar
Vivien Kraus committed
92
  if (strcmp (argv[1], _("score")) == 0)
Vivien Kraus's avatar
Vivien Kraus committed
93
94
95
96
    {
      size_t n_scores;
      int i;
      int *scores;
Vivien Kraus's avatar
Vivien Kraus committed
97
98
99
100
101
102
103
      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;
        }
104
      for (i = 0; i < (int) n_scores; i++)
Vivien Kraus's avatar
Vivien Kraus committed
105
106
107
        {
          printf ("%d\n", scores[i]);
        }
Vivien Kraus's avatar
Vivien Kraus committed
108
      tarot_game_free (game);
Vivien Kraus's avatar
Vivien Kraus committed
109
110
      return EXIT_SUCCESS;
    }
Vivien Kraus's avatar
Vivien Kraus committed
111
  if (strcmp (argv[1], _("errors")) == 0)
112
113
114
    {
      double ret = tarot_features_errors (game);
      printf ("%f\n", ret);
Vivien Kraus's avatar
Vivien Kraus committed
115
116
      tarot_game_free (game);
      return EXIT_SUCCESS;
117
    }
Vivien Kraus's avatar
Vivien Kraus committed
118
  fprintf (stderr, _("Error: unknown command '%s'.\n"), argv[1]);
Vivien Kraus's avatar
Vivien Kraus committed
119
  tarot_game_free (game);
Vivien Kraus's avatar
Vivien Kraus committed
120
121
  return EXIT_FAILURE;
}