Cleanup the engine since I won't do the brainfuck thing, then fix up the log a bit.

master
Zed A. Shaw 3 months ago
parent 8edb37ceb4
commit fdb3f24377
  1. 6
      Makefile
  2. 6
      builder.cpp
  3. 117
      game_engine.cpp
  4. 2
      game_engine.hpp
  5. 2
      sfmlgui.cpp
  6. 37
      tests/game_engine.cpp

@ -7,3 +7,9 @@ build:
test:
meson test -C builddir --suite turings_tarpit
install: build test
powershell "cp builddir/escape_turings_tarpit.exe ."
run: install
./escape_turings_tarpit.exe

@ -66,12 +66,12 @@ void Builder::run_build() {
string type = err[4].str();
string message = err[5].str();
string result = format("{:%FT%T},{},{},{},{},{}\n",
string result = format("{:%FT%T},{},{},{},{},{}",
fmt::localtime(tstamp), file_name,
lnumber, col, type, message);
stats_out << result;
gui.output(format("\nHIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
game.hit(type);
@ -112,7 +112,7 @@ void Builder::run() {
UpdateListener* listener = new UpdateListener(repo);
dbc::check(listener != nullptr, "Failed to create listener.");
gui.output(format("Watching directory {} for changes...\n", git_path));
gui.output(format("Watching directory {} for changes...", git_path));
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
int rc = gui.main_loop(game, [&] {

@ -59,120 +59,3 @@ void GameEngine::heal() {
bool GameEngine::is_dead() {
return hit_points <= 0;
}
Brainfucker::Brainfucker() {
}
/*
* Just a jank way to use a counter to find
* the previous matching brace rather than
* using a stack. Should work for now.
*/
void Brainfucker::jump_forward() {
int counter = 1;
for(++ip; // when counter hits 0 we found it
counter > 0 && ip < code.size();
++ip)
{
char op = code.at(ip);
if(op == '[') {
// new branch so increment
++counter;
} else if(op == ']') {
// close branch so decrement
--counter;
}
}
++ip; // need to go 1 after
}
/*
* Same jank style of using a counter to
* jump back.
*/
void Brainfucker::jump_backward() {
int counter = 1;
for(--ip; // when counter hits 0 we found it
counter > 0 && ip > 0;
--ip)
{
char op = code.at(ip);
if(op == '[') {
// close branch so decrement
--counter;
} else if(op == ']') {
// new branch so increment
++counter;
}
}
}
/*
* Totally fragile but it seems to be working
* enough to test the idea.
*/
void Brainfucker::run(int ticks) {
while(ip >= 0 && ip < code.size() && ticks > 0) {
char op = code.at(ip);
switch(op) {
case '>':
++dp;
break;
case '<':
--dp;
break;
case '+':
data[dp] = data[dp] + 1;
break;
case '-':
data[dp] = data[dp] - 1;
break;
case '.':
out << (char)data.at(dp);
break;
case ',':
print(ERROR, "Not implemented.\n");
break;
case '[': {
// [ - if 0 jump to after ]
if(data.at(dp) == 0) {
jump_forward();
}
}
break;
case ']': {
// ] if not 0 jump to after [
if(data.at(dp) != 0) {
jump_backward();
}
}
break;
default:
print(ERROR, "Unknown operation {}\n", op);
}
--ticks;
++ip; // not really used but do it anyway
}
}
void Brainfucker::set_code(string &new_code) {
code.assign(new_code);
}
void Brainfucker::reset() {
dp=0;
ip=0;
code.erase();
data.fill(0);
}
string Brainfucker::to_string() {
return out.str();
}

@ -13,7 +13,7 @@ class GameEngine {
map<string, int> damage_types{
{"error", 4},
{"warning", 1},
{"note", 1},
{"note", 0},
};
public:

@ -24,7 +24,7 @@ void SFMLGui::ImGui_update() {
SFML::Update(window, deltaClock.restart());
SetNextWindowPos(ImVec2(0, 0));
SetNextWindowSize(ImVec2(size.x, size.y / 2));
SetNextWindowSize(ImVec2(size.x, size.y));
Begin("Build Status", &window_active_out);

@ -4,43 +4,6 @@
using namespace fmt;
TEST_CASE("basic brainfuck test", "[brainfuck]") {
Brainfucker bf;
string code{"+.>+.>+.>"};
bf.set_code(code);
bf.run(code.size());
REQUIRE(bf.data[0] == 1);
REQUIRE(bf.data[1] == 1);
REQUIRE(bf.data[2] == 1);
bf.reset();
REQUIRE(bf.data[0] == 0);
REQUIRE(bf.data[1] == 0);
REQUIRE(bf.data[2] == 0);
REQUIRE(bf.code.empty());
}
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);
// have it run a bunch of times
bf.run(10000);
string output = bf.to_string();
REQUIRE(output == expected);
}
TEST_CASE("game engine can start and take hit", "[game_engine]") {
// test fails on purpose right now
GameEngine game{4};

Loading…
Cancel
Save