Moved the game engine into its own unit and then made a failing test for it.

master
Zed A. Shaw 3 months ago
parent fb5bf9d733
commit 440be444ea
  1. 0
      dbc.hpp
  2. 47
      escape_turings_tarpit.cpp
  3. 39
      game_engine.cpp
  4. 22
      game_engine.hpp
  5. 11
      meson.build
  6. 10
      tests/game_engine.cpp
  7. 2
      tests/test1.cpp

@ -4,64 +4,21 @@
#include <fmt/color.h>
#include <fmt/chrono.h>
#include <string>
#include "dbc.h"
#include "dbc.hpp"
#include "game_engine.hpp"
#include <unistd.h>
#include <stdio.h>
#include <git2.h>
#include <efsw/efsw.hpp>
#include <regex>
#include <filesystem>
#include <map>
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<string, int> 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;

@ -0,0 +1,39 @@
#include <map>
#include <string>
#include <fmt/core.h>
#include <fmt/color.h>
#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;
}

@ -0,0 +1,22 @@
#include <string>
#include <map>
using namespace std;
class GameEngine {
int hit_points = 0;
map<string, int> damage_types{
{"error", 4},
{"warning", 1},
{"note", 1},
};
public:
GameEngine(int hp);
int determine_damage(string &type);
bool hit(string &type);
bool is_dead();
};

@ -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)

@ -0,0 +1,10 @@
#include <doctest.h>
#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);
}

@ -1,5 +1,5 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include <doctest.h>
int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }

Loading…
Cancel
Save