6293256fec 2012-07-14 kinaba: import dfl.all; 6293256fec 2012-07-14 kinaba: import util; 6293256fec 2012-07-14 kinaba: import game; 6293256fec 2012-07-14 kinaba: import output; bee0596f0f 2012-07-14 kinaba: import driver; 6293256fec 2012-07-14 kinaba: //import solver; bee0596f0f 2012-07-14 kinaba: pragma(lib, "dfl.lib"); 6293256fec 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: class GUI : Form, GameObserver 6293256fec 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: bool on_game_changed(char c, const(Game) g, bool finished) { bee0596f0f 2012-07-14 kinaba: draw(gr, g); bee0596f0f 2012-07-14 kinaba: invalidate(); bee0596f0f 2012-07-14 kinaba: return false; bee0596f0f 2012-07-14 kinaba: } bee0596f0f 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: private { 6293256fec 2012-07-14 kinaba: int cell; 6293256fec 2012-07-14 kinaba: int turn = 0; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: Font font; 6293256fec 2012-07-14 kinaba: Color[char] colors; 6293256fec 2012-07-14 kinaba: string[char] render; bee0596f0f 2012-07-14 kinaba: void delegate(char c) fn; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: this(const(Game) g) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: noMessageFilter(); 6293256fec 2012-07-14 kinaba: this.setStyle(ControlStyles.OPAQUE, true); bee0596f0f 2012-07-14 kinaba: this.fn = fn; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this.paint ~= &my_paint; 6293256fec 2012-07-14 kinaba: this.keyDown ~= &my_keydown; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this.formBorderStyle = FormBorderStyle.FIXED_DIALOG; 6293256fec 2012-07-14 kinaba: this.maximizeBox = false; 6293256fec 2012-07-14 kinaba: this.minimizeBox = false; 6293256fec 2012-07-14 kinaba: this.cell = min(1024/g.map.W, 640/g.map.H); 6293256fec 2012-07-14 kinaba: this.clientSize = Size(g.map.W*cell, g.map.H*cell); bee0596f0f 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: const scrH = this.clientSize.height; bee0596f0f 2012-07-14 kinaba: const scrW = this.clientSize.width; bee0596f0f 2012-07-14 kinaba: this.gr = new MemoryGraphics(scrW, scrH); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Resources 6293256fec 2012-07-14 kinaba: this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL); 6293256fec 2012-07-14 kinaba: this.backColor = Color(255,255,255); 6293256fec 2012-07-14 kinaba: this.colors['#'] = 6293256fec 2012-07-14 kinaba: this.colors['.'] = Color(255,191,127); 6293256fec 2012-07-14 kinaba: this.colors['*'] = Color(255,127,127); 6293256fec 2012-07-14 kinaba: this.colors['R'] = Color(128,128,0); 6293256fec 2012-07-14 kinaba: this.colors['D'] = Color(255,0,0); // Dead 6293256fec 2012-07-14 kinaba: this.colors['\\'] = 6293256fec 2012-07-14 kinaba: this.colors['L'] = 6293256fec 2012-07-14 kinaba: this.colors['O'] = Color(127,255,127); 6293256fec 2012-07-14 kinaba: this.colors['W'] = Color(204,229,255); // water 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this.render['#'] = "■"; 6293256fec 2012-07-14 kinaba: this.render['*'] = "✹"; 6293256fec 2012-07-14 kinaba: this.render['.'] = "♒"; 6293256fec 2012-07-14 kinaba: this.render['\\'] = "λ"; 6293256fec 2012-07-14 kinaba: this.render['R'] = "☃"; 6293256fec 2012-07-14 kinaba: this.render['D'] = "☠"; 6293256fec 2012-07-14 kinaba: this.render['L'] = "☒"; 6293256fec 2012-07-14 kinaba: this.render['O'] = "☐"; bee0596f0f 2012-07-14 kinaba: draw(gr, g); bee0596f0f 2012-07-14 kinaba: } bee0596f0f 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: void set_fn(F)(F f) { this.fn = f; } bee0596f0f 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: void run() { bee0596f0f 2012-07-14 kinaba: Application.run(this); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: private: bee0596f0f 2012-07-14 kinaba: Graphics gr; bee0596f0f 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void my_paint(Control, PaintEventArgs ev) 6293256fec 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: gr.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clientSize.height)); bee0596f0f 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: void draw(Graphics gr, const(Game) g) bee0596f0f 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: int scrW = this.clientSize.width; bee0596f0f 2012-07-14 kinaba: int scrH = this.clientSize.height; 6293256fec 2012-07-14 kinaba: // Fill bg. 6293256fec 2012-07-14 kinaba: gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH)); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Fill water. 6293256fec 2012-07-14 kinaba: int w = g.water_level(); 6293256fec 2012-07-14 kinaba: gr.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW, cell*w+1)); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Paint map. 6293256fec 2012-07-14 kinaba: for(int y=1; y<=g.map.H; ++y) 6293256fec 2012-07-14 kinaba: for(int x=1; x<=g.map.W; ++x) { 6293256fec 2012-07-14 kinaba: Rect r = Rect(cell*(x-1), scrH-cell*y, cell, cell); 6293256fec 2012-07-14 kinaba: char c = g.map[y,x]; 6293256fec 2012-07-14 kinaba: if( c != ' ' ) { 6293256fec 2012-07-14 kinaba: if( c == 'R' && g.dead ) 6293256fec 2012-07-14 kinaba: c = 'D'; 6293256fec 2012-07-14 kinaba: gr.drawText(this.render[c], font, this.colors[c], r); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } bee0596f0f 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: set_text(g); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void my_keydown(Control c, KeyEventArgs ev) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: switch(ev.keyCode) 6293256fec 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: case Keys.DOWN: fn('D'); break; bee0596f0f 2012-07-14 kinaba: case Keys.UP: fn('U'); break; bee0596f0f 2012-07-14 kinaba: case Keys.LEFT: fn('L'); break; bee0596f0f 2012-07-14 kinaba: case Keys.RIGHT: fn('R'); break; bee0596f0f 2012-07-14 kinaba: case Keys.W: fn('W'); break; bee0596f0f 2012-07-14 kinaba: case Keys.A: fn('A'); break; 6293256fec 2012-07-14 kinaba: default: break; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: bee0596f0f 2012-07-14 kinaba: void set_text(const(Game) g) { 6293256fec 2012-07-14 kinaba: this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", g.water_until_rise); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void main(string[] args) 6293256fec 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: auto d = new Driver(File(args[1])); bee0596f0f 2012-07-14 kinaba: d.addObserver!(GuardedOutput)(); bee0596f0f 2012-07-14 kinaba: GUI g = d.addObserver!(GUI)(); bee0596f0f 2012-07-14 kinaba: g.set_fn(&d.command); bee0596f0f 2012-07-14 kinaba: g.run(); 6293256fec 2012-07-14 kinaba: }