File Annotation
Not logged in
f18bc9b7dd 2012-07-15        kinaba: import game;
f18bc9b7dd 2012-07-15        kinaba: import util;
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: interface GameObserver
f18bc9b7dd 2012-07-15        kinaba: {
f18bc9b7dd 2012-07-15        kinaba: 	// this(in Game g);
f18bc9b7dd 2012-07-15        kinaba: 	void on_game_changed(char c, in Game g, bool finished);
f18bc9b7dd 2012-07-15        kinaba: }
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: class Driver
f18bc9b7dd 2012-07-15        kinaba: {
f18bc9b7dd 2012-07-15        kinaba: 	this(Game g) { this.game = g; }
f18bc9b7dd 2012-07-15        kinaba: 	this(File game_data) { this(new Game(game_data)); }
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: 	void command(char c)
f18bc9b7dd 2012-07-15        kinaba: 	{
f18bc9b7dd 2012-07-15        kinaba: 		if( finished )
f18bc9b7dd 2012-07-15        kinaba: 			return;
f18bc9b7dd 2012-07-15        kinaba: 		if( c == 'A' )
f18bc9b7dd 2012-07-15        kinaba: 			aborted = true;
f18bc9b7dd 2012-07-15        kinaba: 		else
f18bc9b7dd 2012-07-15        kinaba: 			game.command(c);
f18bc9b7dd 2012-07-15        kinaba: 		foreach(ob; observers)
f18bc9b7dd 2012-07-15        kinaba: 			ob.on_game_changed(c, game, finished);
f18bc9b7dd 2012-07-15        kinaba: 	}
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: 	T addObserver(T)()
f18bc9b7dd 2012-07-15        kinaba: 	{
f18bc9b7dd 2012-07-15        kinaba: 		T t = new T(game);
f18bc9b7dd 2012-07-15        kinaba: 		observers ~= t;
f18bc9b7dd 2012-07-15        kinaba: 		return t;
f18bc9b7dd 2012-07-15        kinaba: 	}
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: 	@property bool finished()
f18bc9b7dd 2012-07-15        kinaba: 	{
f18bc9b7dd 2012-07-15        kinaba: 		return game.cleared || game.dead || aborted;
f18bc9b7dd 2012-07-15        kinaba: 	}
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: private:
f18bc9b7dd 2012-07-15        kinaba: 	Game           game;
f18bc9b7dd 2012-07-15        kinaba: 	GameObserver[] observers;
f18bc9b7dd 2012-07-15        kinaba: 	bool           aborted = false;
f18bc9b7dd 2012-07-15        kinaba: }
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: unittest
f18bc9b7dd 2012-07-15        kinaba: {
f18bc9b7dd 2012-07-15        kinaba: 	static class MockGame : Game {
f18bc9b7dd 2012-07-15        kinaba: 		this() { super(null,null,null); }
f18bc9b7dd 2012-07-15        kinaba: 		string log;
f18bc9b7dd 2012-07-15        kinaba: 		void command(char c) { log ~= c; }
f18bc9b7dd 2012-07-15        kinaba: 	}
f18bc9b7dd 2012-07-15        kinaba: 	static class MockObserver : GameObserver {
f18bc9b7dd 2012-07-15        kinaba: 		string log;
f18bc9b7dd 2012-07-15        kinaba: 		this(in Game g) {}
f18bc9b7dd 2012-07-15        kinaba: 		void on_game_changed(char c, in Game g, bool fin) { log~=c; if(fin)log~="$"; }
f18bc9b7dd 2012-07-15        kinaba: 	}
f18bc9b7dd 2012-07-15        kinaba: 
f18bc9b7dd 2012-07-15        kinaba: 	auto g = new MockGame;
f18bc9b7dd 2012-07-15        kinaba: 	auto d = new Driver(g);
f18bc9b7dd 2012-07-15        kinaba: 	auto o = d.addObserver!MockObserver();
f18bc9b7dd 2012-07-15        kinaba: 	foreach(char c; "UDLRSAUDLRSA")
f18bc9b7dd 2012-07-15        kinaba: 		d.command(c);
f18bc9b7dd 2012-07-15        kinaba: 	assert(g.log == "UDLRS");
f18bc9b7dd 2012-07-15        kinaba: 	assert(o.log == "UDLRSA$");
f18bc9b7dd 2012-07-15        kinaba: }