File Annotation
Not logged in
6293256fec 2012-07-14        kinaba: import util;
6293256fec 2012-07-14        kinaba: import game;
bee0596f0f 2012-07-14        kinaba: import driver;
6293256fec 2012-07-14        kinaba: import core.stdc.signal;
6293256fec 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: class NilOutput : GameObserver
6293256fec 2012-07-14        kinaba: {
a0c3529225 2012-07-14        kinaba: 	this(in Game g) {}
a0c3529225 2012-07-14        kinaba: 	override void on_game_changed(char c, in Game g, bool finished) {}
6293256fec 2012-07-14        kinaba: }
6293256fec 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: class StdOutput : GameObserver
6293256fec 2012-07-14        kinaba: {
a0c3529225 2012-07-14        kinaba: 	this(in Game g) {}
a0c3529225 2012-07-14        kinaba: 	override void on_game_changed(char c, in Game g, bool finished)
6293256fec 2012-07-14        kinaba: 	{
bee0596f0f 2012-07-14        kinaba: 		stdout.write(c);
6293256fec 2012-07-14        kinaba: 		stdout.flush();
6293256fec 2012-07-14        kinaba: 	}
6293256fec 2012-07-14        kinaba: }
6293256fec 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: class GuardedOutput : GameObserver
6293256fec 2012-07-14        kinaba: {
a0c3529225 2012-07-14        kinaba: 	this(in Game g)
6293256fec 2012-07-14        kinaba: 	{
bee0596f0f 2012-07-14        kinaba: 		setup_sigint_handling();
bee0596f0f 2012-07-14        kinaba: 		ideal_log ~= g.score_if_abort_now;
95915ac93f 2012-07-14        kinaba: 		flushed = false;
6293256fec 2012-07-14        kinaba: 	}
6293256fec 2012-07-14        kinaba: 
a0c3529225 2012-07-14        kinaba: 	override void on_game_changed(char c, in Game g, bool finished)
6293256fec 2012-07-14        kinaba: 	{
95915ac93f 2012-07-14        kinaba: 		if(flushed)
95915ac93f 2012-07-14        kinaba: 			return;
95915ac93f 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: 		log ~= c;
bee0596f0f 2012-07-14        kinaba: 		score_log ~= g.score;
bee0596f0f 2012-07-14        kinaba: 		ideal_log ~= g.score_if_abort_now;
95915ac93f 2012-07-14        kinaba: 		if(finished || log.length+1==g.map.W*g.map.H)
bee0596f0f 2012-07-14        kinaba: 			flush();
6293256fec 2012-07-14        kinaba: 	}
6293256fec 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: private:
6293256fec 2012-07-14        kinaba: 	string log;
6293256fec 2012-07-14        kinaba: 	long[] score_log;
6293256fec 2012-07-14        kinaba: 	long[] ideal_log;
95915ac93f 2012-07-14        kinaba: 	bool   flushed;
6293256fec 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: 	void flush()
6293256fec 2012-07-14        kinaba: 	{
6293256fec 2012-07-14        kinaba: 		Tuple!(long, int, int) cand;
6293256fec 2012-07-14        kinaba: 		cand[0] = long.min;
bee0596f0f 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: 		for(int i=0; i<score_log.length; ++i)
bee0596f0f 2012-07-14        kinaba: 			if(cand[0] < score_log[i])
bee0596f0f 2012-07-14        kinaba: 				cand = tuple(score_log[i],i,0);
bee0596f0f 2012-07-14        kinaba: 		for(int i=0; i<ideal_log.length; ++i)
bee0596f0f 2012-07-14        kinaba: 			if(cand[0] < ideal_log[i])
bee0596f0f 2012-07-14        kinaba: 				cand = tuple(ideal_log[i],i,1);
bee0596f0f 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: 		if(cand[2]==0) {
bee0596f0f 2012-07-14        kinaba: 			string str = log[0..cand[1]+1];
bee0596f0f 2012-07-14        kinaba: 			std.c.stdio.printf("%.*s\n", str.length, str.ptr);
bee0596f0f 2012-07-14        kinaba: 		} else {
bee0596f0f 2012-07-14        kinaba: 			string str = log[0..cand[1]];
bee0596f0f 2012-07-14        kinaba: 			std.c.stdio.printf("%.*sA\n", str.length, str.ptr);
bee0596f0f 2012-07-14        kinaba: 		}
bee0596f0f 2012-07-14        kinaba: 		std.c.stdio.fflush(std.c.stdio.stdout);
95915ac93f 2012-07-14        kinaba: 		flushed = true;
bee0596f0f 2012-07-14        kinaba: 	}
bee0596f0f 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: private:
bee0596f0f 2012-07-14        kinaba: 	static __gshared GuardedOutput g_output;
bee0596f0f 2012-07-14        kinaba: 
bee0596f0f 2012-07-14        kinaba: 	void setup_sigint_handling()
bee0596f0f 2012-07-14        kinaba: 	{
bee0596f0f 2012-07-14        kinaba: 		assert(g_output is null);
bee0596f0f 2012-07-14        kinaba: 		g_output = this;
bee0596f0f 2012-07-14        kinaba: 		extern(C) static void catch_sigint(int) { g_output.flush(); application_exit(); }
bee0596f0f 2012-07-14        kinaba: 		core.stdc.signal.signal(SIGINT, &catch_sigint);
6293256fec 2012-07-14        kinaba: 	}
6293256fec 2012-07-14        kinaba: }