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