Quaternity
opts.cpp
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <unistd.h>
10 #include <stdlib.h>
11 
12 #include "opts.h"
13 #include "settings.h"
14 
20 void usage(std::ostream &out, const char **argv)
21 {
22  out
23  << "Usage: " << argv[0] << " [OPTIONS]" << std::endl
24  << std::endl
25  << " A flexible engine for imaginary quartet!" << std::endl
26  << std::endl
27  << "Note:" << std::endl
28  << " To ensure an equal distribution of the cards" << std::endl
29  << " we enforce: (n * s) mod p == 0." << std::endl
30  << std::endl
31  << "Options:" << std::endl
32  << " -s [int] Size of one set. [default: " << DEFAULT_SET_SIZE << "]" << std::endl
33  << " -n [int] Number of sets. [default: " << DEFAULT_NUM_SETS << "]" << std::endl
34  << " -p [int] Number of players. [default: " << DEFAULT_NUM_PLAYERS << "]" << std::endl
35  << " -h Show this help and exit." << std::endl;
36 
37 }
38 
49 bool valid(const Settings &settings, const char **argv)
50 {
51  if (settings.SET_SIZE < 1) {
52  std::cerr << argv[0] << ": the size of one set has to be at least one" << std::endl;
53  return false;
54  }
55 
56  if (settings.NUM_SETS < 1) {
57  std::cerr << argv[0] << ": the number of sets has to be at least one" << std::endl;
58  return false;
59  }
60 
61  if (settings.NUM_PLAYERS < 2) {
62  std::cerr << argv[0] << ": the number of players has to be at least two" << std::endl;
63  return false;
64  }
65 
66  if ((settings.NUM_SETS * settings.SET_SIZE) % settings.NUM_PLAYERS != 0) {
67  std::cerr << argv[0] << ": enforces (n * s) mod p == 0" << std::endl;
68  return false;
69  }
70 
71  return true;
72 }
73 
85 Settings options(const int argc, const char **argv)
86 {
87  int SET_SIZE = DEFAULT_SET_SIZE;
88  int NUM_SETS = DEFAULT_NUM_SETS;
89  int NUM_PLAYERS = DEFAULT_NUM_PLAYERS;
90 
91  int opt;
92  while ((opt = getopt(argc, (char* const*)argv, "s:n:p:h")) != -1) {
93  switch (opt) {
94  case 's':
95  SET_SIZE = atoi(optarg);
96  break;
97  case 'n':
98  NUM_SETS = atoi(optarg);
99  break;
100  case 'p':
101  NUM_PLAYERS = atoi(optarg);
102  break;
103  case 'h':
104  usage(std::cout, argv);
105  exit(EXIT_SUCCESS);
106  default: // '?'
107  usage(std::cerr, argv);
108  exit(EXIT_FAILURE);
109  }
110  }
111 
112  Settings settings = {SET_SIZE, NUM_SETS, NUM_PLAYERS};
113  if (!valid(settings, argv)) {
114  usage(std::cerr, argv);
115  exit(EXIT_FAILURE);
116  }
117  return settings;
118 }
void usage(std::ostream &out, const char **argv)
Print the usage information for this program on the screen.
Definition: opts.cpp:20
bool valid(const Settings &settings, const char **argv)
Validate the settings given.
Definition: opts.cpp:49
Settings options(const int argc, const char **argv)
Parse the command line arguments and generate the settings from these.
Definition: opts.cpp:85
This file defines the command line argument parsing function header.
This file defines the settings struct.
constexpr int DEFAULT_SET_SIZE
The default value for SET_SIZE.
Definition: settings.h:11
constexpr int DEFAULT_NUM_SETS
The default value for NUM_SETS.
Definition: settings.h:14
constexpr int DEFAULT_NUM_PLAYERS
The default value for NUM_PLAYERS.
Definition: settings.h:17
The settings which characterize a game.
Definition: settings.h:22
const int NUM_SETS
The number of sets.
Definition: settings.h:32
const int SET_SIZE
The size of one set, aka the number of cards in one set.
Definition: settings.h:27
const int NUM_PLAYERS
The number of players playing the game.
Definition: settings.h:37