Artifact 95ed42a2d89e1fef9a37a76c14dc1db755211492
import util;
import game;
import driver;
import core.stdc.signal;
class NilOutput : GameObserver
{
this(in Game g) {}
override void on_game_changed(char c, in Game g, bool finished) {}
}
class StdOutput : GameObserver
{
this(in Game g) {}
override void on_game_changed(char c, in Game g, bool finished)
{
stdout.write(c);
stdout.flush();
}
}
class GuardedOutput : GameObserver
{
this(in Game g)
{
setup_sigint_handling();
score_log ~= g.score;
flushed = false;
}
override void on_game_changed(char c, in Game g, bool finished)
{
if(flushed)
return;
log ~= c;
score_log ~= g.score;
if(finished || log.length+1==g.map.W*g.map.H)
flush();
if(log.length+1==g.map.W*g.map.H)
application_exit();
}
private:
string log;
long[] score_log;
bool flushed;
void flush()
{
if(flushed)
return;
Tuple!(long, int) cand;
cand[0] = long.min;
for(int i=0; i<score_log.length; ++i)
if(cand[0] < score_log[i])
cand = tuple(score_log[i],i);
std.c.stdio.printf("%.*sA\n", cand[1], log.ptr);
std.c.stdio.fflush(std.c.stdio.stdout);
flushed = true;
}
private:
static __gshared GuardedOutput g_output;
void setup_sigint_handling()
{
assert(g_output is null);
g_output = this;
extern(C) static void catch_sigint(int) { g_output.flush(); application_exit(); }
core.stdc.signal.signal(SIGINT, &catch_sigint);
}
}