Check-in [f18bc9b7dd]
Not logged in
Overview
SHA1 Hash:f18bc9b7dd64b31aac9914f58cc4004ce320861c
Date: 2012-07-15 20:22:38
User: kinaba
Comment:have I never added this file??? ???? ???????
Timelines: family | ancestors | descendants | both | trunk
Diffs: redesign
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Added src/driver.d version [3d15ff20e76164b8]

1 +import game; 2 +import util; 3 + 4 +interface GameObserver 5 +{ 6 + // this(in Game g); 7 + void on_game_changed(char c, in Game g, bool finished); 8 +} 9 + 10 +class Driver 11 +{ 12 + this(Game g) { this.game = g; } 13 + this(File game_data) { this(new Game(game_data)); } 14 + 15 + void command(char c) 16 + { 17 + if( finished ) 18 + return; 19 + if( c == 'A' ) 20 + aborted = true; 21 + else 22 + game.command(c); 23 + foreach(ob; observers) 24 + ob.on_game_changed(c, game, finished); 25 + } 26 + 27 + T addObserver(T)() 28 + { 29 + T t = new T(game); 30 + observers ~= t; 31 + return t; 32 + } 33 + 34 + @property bool finished() 35 + { 36 + return game.cleared || game.dead || aborted; 37 + } 38 + 39 +private: 40 + Game game; 41 + GameObserver[] observers; 42 + bool aborted = false; 43 +} 44 + 45 +unittest 46 +{ 47 + static class MockGame : Game { 48 + this() { super(null,null,null); } 49 + string log; 50 + void command(char c) { log ~= c; } 51 + } 52 + static class MockObserver : GameObserver { 53 + string log; 54 + this(in Game g) {} 55 + void on_game_changed(char c, in Game g, bool fin) { log~=c; if(fin)log~="$"; } 56 + } 57 + 58 + auto g = new MockGame; 59 + auto d = new Driver(g); 60 + auto o = d.addObserver!MockObserver(); 61 + foreach(char c; "UDLRSAUDLRSA") 62 + d.command(c); 63 + assert(g.log == "UDLRS"); 64 + assert(o.log == "UDLRSA$"); 65 +}