diff --git a/src/main.cpp b/src/main.cpp index 5826175..35132b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,147 +14,6 @@ // the source template at `configured_files/config.hpp.in`. #include -template struct GameBoard -{ - static constexpr std::size_t width = Width; - static constexpr std::size_t height = Height; - - std::array, width> strings; - std::array, width> values{}; - - std::size_t move_count{ 0 }; - - std::string &get_string(std::size_t x, std::size_t y) { return strings.at(x).at(y); } - - - void set(std::size_t x, std::size_t y, bool new_value) - { - get(x, y) = new_value; - - if (new_value) { - get_string(x, y) = " ON"; - } else { - get_string(x, y) = "OFF"; - } - } - - void visit(auto visitor) - { - for (std::size_t x = 0; x < width; ++x) { - for (std::size_t y = 0; y < height; ++y) { visitor(x, y, *this); } - } - } - - [[nodiscard]] bool get(std::size_t x, std::size_t y) const { return values.at(x).at(y); } - - [[nodiscard]] bool &get(std::size_t x, std::size_t y) { return values.at(x).at(y); } - - GameBoard() - { - visit([](const auto x, const auto y, auto &gameboard) { gameboard.set(x, y, true); }); - } - - void update_strings() - { - for (std::size_t x = 0; x < width; ++x) { - for (std::size_t y = 0; y < height; ++y) { set(x, y, get(x, y)); } - } - } - - void toggle(std::size_t x, std::size_t y) { set(x, y, !get(x, y)); } - - void press(std::size_t x, std::size_t y) - { - ++move_count; - toggle(x, y); - if (x > 0) { toggle(x - 1, y); } - if (y > 0) { toggle(x, y - 1); } - if (x < width - 1) { toggle(x + 1, y); } - if (y < height - 1) { toggle(x, y + 1); } - } - - [[nodiscard]] bool solved() const - { - for (std::size_t x = 0; x < width; ++x) { - for (std::size_t y = 0; y < height; ++y) { - if (!get(x, y)) { return false; } - } - } - - return true; - } -}; - - -void consequence_game() -{ - auto screen = ftxui::ScreenInteractive::TerminalOutput(); - - GameBoard<3, 3> gb; - - std::string quit_text; - - const auto update_quit_text = [&quit_text](const auto &game_board) { - quit_text = fmt::format("Quit ({} moves)", game_board.move_count); - if (game_board.solved()) { quit_text += " Solved!"; } - }; - - const auto make_buttons = [&] { - std::vector buttons; - for (std::size_t x = 0; x < gb.width; ++x) { - for (std::size_t y = 0; y < gb.height; ++y) { - buttons.push_back(ftxui::Button(&gb.get_string(x, y), [=, &gb] { - if (!gb.solved()) { gb.press(x, y); } - update_quit_text(gb); - })); - } - } - return buttons; - }; - - auto buttons = make_buttons(); - - auto quit_button = ftxui::Button(&quit_text, screen.ExitLoopClosure()); - - auto make_layout = [&] { - std::vector rows; - - std::size_t idx = 0; - - for (std::size_t x = 0; x < gb.width; ++x) { - std::vector row; - for (std::size_t y = 0; y < gb.height; ++y) { - row.push_back(buttons[idx]->Render()); - ++idx; - } - rows.push_back(ftxui::hbox(std::move(row))); - } - - rows.push_back(ftxui::hbox({ quit_button->Render() })); - - return ftxui::vbox(std::move(rows)); - }; - - - static constexpr int randomization_iterations = 100; - static constexpr int random_seed = 42; - - std::mt19937 gen32{ random_seed };// NOLINT fixed seed - std::uniform_int_distribution x(static_cast(0), gb.width - 1); - std::uniform_int_distribution y(static_cast(0), gb.height - 1); - - for (int i = 0; i < randomization_iterations; ++i) { gb.press(x(gen32), y(gen32)); } - gb.move_count = 0; - update_quit_text(gb); - - auto all_buttons = buttons; - all_buttons.push_back(quit_button); - auto container = ftxui::Container::Horizontal(all_buttons); - - auto renderer = ftxui::Renderer(container, make_layout); - - screen.Loop(renderer); -} struct Color { @@ -312,8 +171,7 @@ int main(int argc, const char **argv) R"(intro Usage: - intro turn_based - intro loop_based + intro intro (-h | --help) intro --version Options: @@ -329,11 +187,7 @@ int main(int argc, const char **argv) my_awesome_game::cmake::project_version));// version string, acquired // from config.hpp via CMake - if (args["turn_based"].asBool()) { - consequence_game(); - } else { - game_iteration_canvas(); - } + game_iteration_canvas(); // consequence_game(); } catch (const std::exception &e) {