aboutsummaryrefslogtreecommitdiff
path: root/src/game_of_life.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-02-19 17:30:16 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:53 +0200
commitcc3869ac0b24a97dbadc6b837b3c23af28baa627 (patch)
tree3fa7156e1734de03d6b8152bfae219774bb38196 /src/game_of_life.cpp
parent4210c8d2f8fc36e614b8bfb669511c00383002f0 (diff)
feat: add basic program stuff
Diffstat (limited to 'src/game_of_life.cpp')
-rw-r--r--src/game_of_life.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/game_of_life.cpp b/src/game_of_life.cpp
index e69de29..32d393e 100644
--- a/src/game_of_life.cpp
+++ b/src/game_of_life.cpp
@@ -0,0 +1,108 @@
+#include "conversion.hpp"
+#include "game/options.hpp"
+#include "randomization.hpp"
+
+#include <getopt.h>
+#include <iostream>
+#include <memory>
+#include <string_view>
+#include <vector>
+
+namespace
+{
+void optarg_error(char arg, const std::string_view &error)
+{
+ std::cout << "Error: Invalid option argument for -" << arg << ". " << error
+ << std::endl;
+ exit(EXIT_FAILURE);
+}
+
+/**
+ * Returns the current optarg as a string view.
+ */
+std::string_view get_str_optarg()
+{
+ return std::string_view(optarg);
+}
+
+/**
+ * Returns the current optarg as a unsigned integer.
+ *
+ * @param arg The current command-line argument character
+ * @param check_zero Whether or not to make sure that the result is not zero
+ */
+unsigned int get_uint_optarg(char arg, bool check_zero = false)
+{
+ unsigned int num = 0;
+
+ try
+ {
+ num = str_to_uint(get_str_optarg());
+
+ if (check_zero && num == 0)
+ {
+ throw "It should not be 0";
+ }
+ }
+ catch (const char *error)
+ {
+ optarg_error(arg, std::string_view(error));
+ }
+
+ return num;
+}
+} // namespace
+
+constexpr std::array<option, 8> options = {
+ option({"seed", required_argument, nullptr, 's'}),
+ option({"help", no_argument, nullptr, 0}), option({nullptr, 0, nullptr, 0})};
+
+int main(int argc, char *argv[])
+{
+ auto args = std::vector<std::string_view>(argv, argv + argc);
+
+ GameOptions game_options;
+
+ char arg = 0;
+ while ((arg = static_cast<char>(
+ getopt_long(argc, argv, "s:", options.data(), nullptr))) != -1)
+ {
+ switch (arg)
+ {
+ case 's':
+ {
+ auto seed = get_uint_optarg(arg, true);
+
+ game_options.random_gen = std::make_shared<RandomNumberGenerator>(seed);
+ break;
+ }
+ case 0:
+ {
+ std::cout << "Usage: " << args[0]
+ << " [OPTION]...\n\n"
+ "Options:\n"
+ << " -s, --seed SEED The randomization seed used\n"
+ " --help Displays usage information"
+ << std::endl;
+ return EXIT_SUCCESS;
+ }
+ case '?':
+ {
+ std::cout << "\nTry '" << args[0] << " --help' for more information"
+ << std::endl;
+ return EXIT_FAILURE;
+ }
+ default:
+ abort();
+ }
+ }
+
+ if (game_options.random_gen == nullptr)
+ {
+ game_options.random_gen = std::make_shared<RandomNumberGenerator>();
+ }
+
+ // auto game = Game(game_options);
+
+ // game.run();
+}