diff --git a/game_engine.cpp b/game_engine.cpp index 16a296c..893890b 100644 --- a/game_engine.cpp +++ b/game_engine.cpp @@ -121,7 +121,7 @@ void Brainfucker::run(int ticks) { data[dp] = data[dp] - 1; break; case '.': - cout << (char)data.at(dp); + out << (char)data.at(dp); break; case ',': print(ERROR, "Not implemented.\n"); @@ -162,7 +162,6 @@ void Brainfucker::reset() { data.fill(0); } - GameEngine::GameEngine(int hp) : hit_points(hp) {}; int GameEngine::determine_damage(string &type) { diff --git a/game_engine.hpp b/game_engine.hpp index d33101f..112890e 100644 --- a/game_engine.hpp +++ b/game_engine.hpp @@ -2,14 +2,16 @@ #include #include #include +#include using namespace std; class Brainfucker { - size_t dp = 0; - size_t ip = 0; public: + size_t dp = 0; + size_t ip = 0; + ostringstream out; array data = {}; string code = {}; diff --git a/tests/game_engine.cpp b/tests/game_engine.cpp index 76480e0..b5ceb9b 100644 --- a/tests/game_engine.cpp +++ b/tests/game_engine.cpp @@ -1,5 +1,8 @@ #include #include "../game_engine.hpp" +#include + +using namespace fmt; TEST_CASE("basic brainfuck test", "[brainfuck]") { Brainfucker bf; @@ -26,11 +29,16 @@ TEST_CASE("basic brainfuck test", "[brainfuck]") { TEST_CASE("brainfuck loop test", "[brainfuck]") { Brainfucker bf; + const string expected{"Hello World!\n"}; // this is a hello world program from wikipedia + // but at the end I rewind dp so I can analyze it string code{"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."}; bf.set_code(code); bf.run(code.size()); + + string output = bf.out.str(); + REQUIRE(output == expected); } TEST_CASE("game engine can start and take hit", "[brainfuck]") {