diff --git a/dbc.h b/dbc.hpp similarity index 100% rename from dbc.h rename to dbc.hpp diff --git a/escape_turings_tarpit.cpp b/escape_turings_tarpit.cpp index 33a40bf..64241e4 100644 --- a/escape_turings_tarpit.cpp +++ b/escape_turings_tarpit.cpp @@ -4,64 +4,21 @@ #include #include #include -#include "dbc.h" +#include "dbc.hpp" +#include "game_engine.hpp" #include #include #include #include #include #include -#include using namespace std; using namespace fmt; namespace fs = std::filesystem; -const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); - #define BUF_MAX 1024 -class GameEngine { - int hit_points = 0; - map damage_types{ - {"error", 4}, - {"warning", 1}, - {"note", 1}, - }; - - public: - GameEngine(int hp) : hit_points(hp) {}; - - int determine_damage(string &type) { - try { - return damage_types.at(type); - } catch(std::out_of_range &err) { - print(ERROR, "BAD DAMAGE TYPE {}\n", type); - return 1000; - } - } - - bool hit(string &type) { - int damage = determine_damage(type); - hit_points -= damage; - - if(is_dead()) { - print(ERROR, "YOU DIED!\n"); - } else { - println("DAMAGE {}, HP: {}", damage, hit_points); - } - - // super dumb but I'll clean it up later - return is_dead(); - } - - bool is_dead() { - return hit_points <= 0; - } -}; - - - class UpdateListener : public efsw::FileWatchListener { public: bool changes = false; diff --git a/game_engine.cpp b/game_engine.cpp new file mode 100644 index 0000000..30a4bec --- /dev/null +++ b/game_engine.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include "game_engine.hpp" + +using namespace fmt; +using namespace std; + +const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); + +GameEngine::GameEngine(int hp) : hit_points(hp) {}; + +int GameEngine::determine_damage(string &type) { + try { + return damage_types.at(type); + } catch(std::out_of_range &err) { + print(ERROR, "BAD DAMAGE TYPE {}\n", type); + return 1000; + } +} + +bool GameEngine::hit(string &type) { + int damage = determine_damage(type); + hit_points -= damage; + + if(is_dead()) { + print(ERROR, "YOU DIED!\n"); + } else { + println("DAMAGE {}, HP: {}", damage, hit_points); + } + + // super dumb but I'll clean it up later + return is_dead(); +} + +bool GameEngine::is_dead() { + return hit_points <= 0; +} diff --git a/game_engine.hpp b/game_engine.hpp new file mode 100644 index 0000000..f8347d2 --- /dev/null +++ b/game_engine.hpp @@ -0,0 +1,22 @@ +#include +#include + +using namespace std; + +class GameEngine { + int hit_points = 0; + map damage_types{ + {"error", 4}, + {"warning", 1}, + {"note", 1}, + }; + + public: + GameEngine(int hp); + + int determine_damage(string &type); + + bool hit(string &type); + + bool is_dead(); +}; diff --git a/meson.build b/meson.build index a011829..b467296 100644 --- a/meson.build +++ b/meson.build @@ -28,7 +28,8 @@ dependencies = [ ] executable('escape_turings_tarpit', - 'escape_turings_tarpit.cpp', + ['game_engine.cpp', + 'escape_turings_tarpit.cpp'], dependencies: dependencies) executable('regtest', 'regtest.cpp', @@ -40,6 +41,10 @@ executable('ftxtest', 'ftxtest.cpp', executable('ftx_thread_test', 'ftx_thread_test.cpp', dependencies: dependencies) -executable('tests', [ - 'tests/test1.cpp'], +runtests = executable('runtests', [ + 'game_engine.cpp', + 'tests/test1.cpp', + 'tests/game_engine.cpp'], dependencies: dependencies + [doctest]) + +test('the tests', runtests) diff --git a/tests/game_engine.cpp b/tests/game_engine.cpp new file mode 100644 index 0000000..e69d8c4 --- /dev/null +++ b/tests/game_engine.cpp @@ -0,0 +1,10 @@ +#include +#include "../game_engine.hpp" + +TEST_CASE("game engine can start and take hit") { + // test fails on purpose right now + GameEngine game{100}; + string err{"error"}; + game.hit(err); + CHECK(game.is_dead() == true); +} diff --git a/tests/test1.cpp b/tests/test1.cpp index 5406cd1..c4aa842 100644 --- a/tests/test1.cpp +++ b/tests/test1.cpp @@ -1,5 +1,5 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN -#include "doctest.h" +#include int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }