b6daa1efa1 2012-07-14 kinaba: import util; b6daa1efa1 2012-07-14 kinaba: import game; b6daa1efa1 2012-07-14 kinaba: import core.stdc.signal; b6daa1efa1 2012-07-14 kinaba: import std.c.stdlib; b6daa1efa1 2012-07-14 kinaba: b6daa1efa1 2012-07-14 kinaba: abstract class Output b6daa1efa1 2012-07-14 kinaba: { b6daa1efa1 2012-07-14 kinaba: void command(char c); a5e6c99b3d 2012-07-14 kinaba: void flush(); b6daa1efa1 2012-07-14 kinaba: } b6daa1efa1 2012-07-14 kinaba: b6daa1efa1 2012-07-14 kinaba: class NilOutput : Output b6daa1efa1 2012-07-14 kinaba: { b6daa1efa1 2012-07-14 kinaba: override void command(char c) {} a5e6c99b3d 2012-07-14 kinaba: override void flush() {} b6daa1efa1 2012-07-14 kinaba: } b6daa1efa1 2012-07-14 kinaba: b6daa1efa1 2012-07-14 kinaba: class StdOutput : Output a5e6c99b3d 2012-07-14 kinaba: { a5e6c99b3d 2012-07-14 kinaba: override void command(char c) a5e6c99b3d 2012-07-14 kinaba: { a5e6c99b3d 2012-07-14 kinaba: write(c); a5e6c99b3d 2012-07-14 kinaba: stdout.flush(); a5e6c99b3d 2012-07-14 kinaba: } a5e6c99b3d 2012-07-14 kinaba: override void flush() {} a5e6c99b3d 2012-07-14 kinaba: } a5e6c99b3d 2012-07-14 kinaba: a5e6c99b3d 2012-07-14 kinaba: // TODO: clean it up. a5e6c99b3d 2012-07-14 kinaba: __gshared Output g_output; a5e6c99b3d 2012-07-14 kinaba: a5e6c99b3d 2012-07-14 kinaba: class GuardedOutput : StdOutput b6daa1efa1 2012-07-14 kinaba: { b6daa1efa1 2012-07-14 kinaba: // Handle SIGINT: force abort and exit. b6daa1efa1 2012-07-14 kinaba: static this() b6daa1efa1 2012-07-14 kinaba: { b6daa1efa1 2012-07-14 kinaba: signal(SIGINT, &sigint); b6daa1efa1 2012-07-14 kinaba: } a5e6c99b3d 2012-07-14 kinaba: a5e6c99b3d 2012-07-14 kinaba: extern(C) static void sigint(int) a5e6c99b3d 2012-07-14 kinaba: { a5e6c99b3d 2012-07-14 kinaba: if(g_output !is null) a5e6c99b3d 2012-07-14 kinaba: g_output.flush(); a5e6c99b3d 2012-07-14 kinaba: else { a5e6c99b3d 2012-07-14 kinaba: write("A"); a5e6c99b3d 2012-07-14 kinaba: stdout.flush(); a5e6c99b3d 2012-07-14 kinaba: } b6daa1efa1 2012-07-14 kinaba: exit(0); b6daa1efa1 2012-07-14 kinaba: } b6daa1efa1 2012-07-14 kinaba: a5e6c99b3d 2012-07-14 kinaba: Game g; a5e6c99b3d 2012-07-14 kinaba: this(Game ini) { this.g = ini.clone(); ideal_log ~= g.score_if_abort_now; g_output = this; } a5e6c99b3d 2012-07-14 kinaba: a5e6c99b3d 2012-07-14 kinaba: string log; a5e6c99b3d 2012-07-14 kinaba: long[] score_log; a5e6c99b3d 2012-07-14 kinaba: long[] ideal_log; a5e6c99b3d 2012-07-14 kinaba: b6daa1efa1 2012-07-14 kinaba: override void command(char c) b6daa1efa1 2012-07-14 kinaba: { a5e6c99b3d 2012-07-14 kinaba: g.command(c); a5e6c99b3d 2012-07-14 kinaba: log ~= c; a5e6c99b3d 2012-07-14 kinaba: score_log ~= g.score; a5e6c99b3d 2012-07-14 kinaba: ideal_log ~= g.score_if_abort_now; a5e6c99b3d 2012-07-14 kinaba: } a5e6c99b3d 2012-07-14 kinaba: override void flush() a5e6c99b3d 2012-07-14 kinaba: { a5e6c99b3d 2012-07-14 kinaba: Tuple!(long, int, int) cand; a5e6c99b3d 2012-07-14 kinaba: cand[0] = long.min; a5e6c99b3d 2012-07-14 kinaba: foreach(int i, long s; score_log) a5e6c99b3d 2012-07-14 kinaba: if(cand[0] < s) a5e6c99b3d 2012-07-14 kinaba: cand = tuple(s,i,0); a5e6c99b3d 2012-07-14 kinaba: foreach(int i, long s; ideal_log) a5e6c99b3d 2012-07-14 kinaba: if(cand[0] < s) a5e6c99b3d 2012-07-14 kinaba: cand = tuple(s,i,1); a5e6c99b3d 2012-07-14 kinaba: if(cand[2]==0) a5e6c99b3d 2012-07-14 kinaba: writeln(log[0..cand[1]+1]); a5e6c99b3d 2012-07-14 kinaba: else a5e6c99b3d 2012-07-14 kinaba: writeln(log[0..cand[1]]~"A"); b6daa1efa1 2012-07-14 kinaba: stdout.flush(); b6daa1efa1 2012-07-14 kinaba: } b6daa1efa1 2012-07-14 kinaba: }