a06a38e3e6 2012-07-13 kinaba: import std.algorithm; a06a38e3e6 2012-07-13 kinaba: import std.array; a06a38e3e6 2012-07-13 kinaba: import std.conv; a06a38e3e6 2012-07-13 kinaba: import std.stdio; a06a38e3e6 2012-07-13 kinaba: import std.string; b59ee91407 2012-07-13 kinaba: import dfl.all; a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: class Map a06a38e3e6 2012-07-13 kinaba: { a06a38e3e6 2012-07-13 kinaba: private char[][] data; d0766ecd1b 2012-07-13 kinaba: bool dead = false; 246bed04b3 2012-07-13 kinaba: bool cleared = false; a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: this(File input) a06a38e3e6 2012-07-13 kinaba: { a06a38e3e6 2012-07-13 kinaba: foreach(s; input.byLine()) a06a38e3e6 2012-07-13 kinaba: data ~= s.chomp.dup; a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: int width = 0; a06a38e3e6 2012-07-13 kinaba: foreach(s; data) a06a38e3e6 2012-07-13 kinaba: width = max(width, s.length); a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: // space padding and sentinels a06a38e3e6 2012-07-13 kinaba: foreach(ref s; data) { a06a38e3e6 2012-07-13 kinaba: int p = s.length; a06a38e3e6 2012-07-13 kinaba: s.length = width; a06a38e3e6 2012-07-13 kinaba: s[p..$] = ' '; a06a38e3e6 2012-07-13 kinaba: s = '#' ~ s ~ '#'; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: // vertical sentinel a06a38e3e6 2012-07-13 kinaba: char[] sen = new char[width+2]; a06a38e3e6 2012-07-13 kinaba: sen[] = '#'; a06a38e3e6 2012-07-13 kinaba: data = sen.dup ~ data ~ sen; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: @property const a06a38e3e6 2012-07-13 kinaba: { a06a38e3e6 2012-07-13 kinaba: int W() { return data[0].length; } a06a38e3e6 2012-07-13 kinaba: int H() { return data.length; } a06a38e3e6 2012-07-13 kinaba: string toString() { a06a38e3e6 2012-07-13 kinaba: string result; a06a38e3e6 2012-07-13 kinaba: foreach(i,s; data) { a06a38e3e6 2012-07-13 kinaba: if(i) result ~= '\n'; a06a38e3e6 2012-07-13 kinaba: result ~= s.idup; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: return result; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: 8f5135c552 2012-07-13 kinaba: int command_R() { return move(0, +1); } 8f5135c552 2012-07-13 kinaba: int command_L() { return move(0, -1); } 8f5135c552 2012-07-13 kinaba: int command_U() { return move(-1, 0); } 8f5135c552 2012-07-13 kinaba: int command_D() { return move(+1, 0); } d0766ecd1b 2012-07-13 kinaba: int wait() { if(dead)return 0; update(); return -1; } 246bed04b3 2012-07-13 kinaba: int abort() { if(dead)return 0; cleared=true; return gained*25; } a06a38e3e6 2012-07-13 kinaba: 8f5135c552 2012-07-13 kinaba: int move(int dy, int dx) { a06a38e3e6 2012-07-13 kinaba: foreach(y,s; data) a06a38e3e6 2012-07-13 kinaba: foreach(x,c; s) a06a38e3e6 2012-07-13 kinaba: if(c == 'R') a06a38e3e6 2012-07-13 kinaba: return move(dy, dx, y, x); 8f5135c552 2012-07-13 kinaba: assert(false); a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: 8f5135c552 2012-07-13 kinaba: int gained = 0; // TODO: atode naosu 8f5135c552 2012-07-13 kinaba: int move(int dy, int dx, int y, int x) { d0766ecd1b 2012-07-13 kinaba: if(dead) d0766ecd1b 2012-07-13 kinaba: return 0; 8f5135c552 2012-07-13 kinaba: int score = 0; 8f5135c552 2012-07-13 kinaba: if(data[y+dy][x+dx]=='\\') { 8f5135c552 2012-07-13 kinaba: score += 25; 8f5135c552 2012-07-13 kinaba: ++gained; 8f5135c552 2012-07-13 kinaba: } 246bed04b3 2012-07-13 kinaba: if(data[y+dy][x+dx]=='O') { 8f5135c552 2012-07-13 kinaba: score += gained*50; 246bed04b3 2012-07-13 kinaba: cleared = true; 246bed04b3 2012-07-13 kinaba: } 8f5135c552 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: if(data[y+dy][x+dx]==' ' || data[y+dy][x+dx]=='.' a06a38e3e6 2012-07-13 kinaba: || data[y+dy][x+dx]=='\\' || data[y+dy][x+dx]=='O') { a06a38e3e6 2012-07-13 kinaba: data[y][x]=' '; a06a38e3e6 2012-07-13 kinaba: data[y+dy][x+dx]='R'; a06a38e3e6 2012-07-13 kinaba: } else if(dy==0 && data[y+dy][x+dx]=='*' && data[y+2*dy][x+2*dx]==' ') { a06a38e3e6 2012-07-13 kinaba: data[y][x]=' '; a06a38e3e6 2012-07-13 kinaba: data[y+dy][x+dx]='R'; a06a38e3e6 2012-07-13 kinaba: data[y+2*dy][x+2*dx]='*'; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: update(); 8f5135c552 2012-07-13 kinaba: return score-1; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: void update() { a06a38e3e6 2012-07-13 kinaba: char[][] next; a06a38e3e6 2012-07-13 kinaba: foreach(y,s; data) a06a38e3e6 2012-07-13 kinaba: next ~= s.dup; a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: bool lambda = false; a06a38e3e6 2012-07-13 kinaba: for(int y=1; y+1<H; ++y) a06a38e3e6 2012-07-13 kinaba: for(int x=1; x+1<W; ++x) a06a38e3e6 2012-07-13 kinaba: lambda |= (data[y][x] == '\\'); a06a38e3e6 2012-07-13 kinaba: d0766ecd1b 2012-07-13 kinaba: for(int y=H-2; y>=1; --y) a06a38e3e6 2012-07-13 kinaba: for(int x=1; x+1<W; ++x) { a06a38e3e6 2012-07-13 kinaba: if(data[y][x]=='*') { a06a38e3e6 2012-07-13 kinaba: if(data[y+1][x]==' ') { a06a38e3e6 2012-07-13 kinaba: next[y][x]=' '; a06a38e3e6 2012-07-13 kinaba: next[y+1][x]='*'; d0766ecd1b 2012-07-13 kinaba: if(next[y+2][x]=='R') d0766ecd1b 2012-07-13 kinaba: dead=true; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: else if(data[y+1][x]=='*' && data[y][x+1]==' ' && data[y+1][x+1]==' ') { a06a38e3e6 2012-07-13 kinaba: next[y][x]=' '; a06a38e3e6 2012-07-13 kinaba: next[y+1][x+1]='*'; d0766ecd1b 2012-07-13 kinaba: if(next[y+2][x+1]=='R') d0766ecd1b 2012-07-13 kinaba: dead=true; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: else if(data[y+1][x]=='*' && data[y][x-1]==' ' && data[y+1][x-1]==' ') { a06a38e3e6 2012-07-13 kinaba: next[y][x]=' '; a06a38e3e6 2012-07-13 kinaba: next[y+1][x-1]='*'; d0766ecd1b 2012-07-13 kinaba: if(next[y+2][x-1]=='R') d0766ecd1b 2012-07-13 kinaba: dead=true; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: else if(data[y+1][x]=='\\' && data[y][x+1]==' ' && data[y+1][x+1]==' ') { a06a38e3e6 2012-07-13 kinaba: next[y][x]=' '; a06a38e3e6 2012-07-13 kinaba: next[y+1][x+1]='*'; d0766ecd1b 2012-07-13 kinaba: if(next[y+2][x+1]=='R') d0766ecd1b 2012-07-13 kinaba: dead=true; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: else if(data[y][x]=='L') { a06a38e3e6 2012-07-13 kinaba: if(!lambda) a06a38e3e6 2012-07-13 kinaba: next[y][x] = 'O'; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: data = next; a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: b59ee91407 2012-07-13 kinaba: class MyForm : Form a06a38e3e6 2012-07-13 kinaba: { b59ee91407 2012-07-13 kinaba: Map m; 8f5135c552 2012-07-13 kinaba: int score; 8f5135c552 2012-07-13 kinaba: b59ee91407 2012-07-13 kinaba: this(Map m) b59ee91407 2012-07-13 kinaba: { b59ee91407 2012-07-13 kinaba: noMessageFilter(); b59ee91407 2012-07-13 kinaba: this.m = m; b59ee91407 2012-07-13 kinaba: this.text = "Dark Integers"; b59ee91407 2012-07-13 kinaba: this.keyDown ~= &myKey; 8f5135c552 2012-07-13 kinaba: this.score = 0; b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: override void onResize(EventArgs ev) { b59ee91407 2012-07-13 kinaba: invalidate(); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: override void onPaint(PaintEventArgs ev) b59ee91407 2012-07-13 kinaba: { b59ee91407 2012-07-13 kinaba: int Z = min(this.clientSize.width, this.clientSize.height) / max(m.W-2, m.H-2); b59ee91407 2012-07-13 kinaba: Font font = new Font("MS Gothic", Z-4); b59ee91407 2012-07-13 kinaba: Graphics g = ev.graphics; b59ee91407 2012-07-13 kinaba: for(int y=1; y+1<m.H; ++y) b59ee91407 2012-07-13 kinaba: for(int x=1; x+1<m.W; ++x) { b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='*') { b59ee91407 2012-07-13 kinaba: g.drawText("岩", font, Color(0,0,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='\\') { b59ee91407 2012-07-13 kinaba: g.drawText("λ", font, Color(0,255,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='R') { d0766ecd1b 2012-07-13 kinaba: if(m.dead) d0766ecd1b 2012-07-13 kinaba: g.drawText("Я", font, Color(255,0,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); d0766ecd1b 2012-07-13 kinaba: else d0766ecd1b 2012-07-13 kinaba: g.drawText("R", font, Color(128,128,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='L') { 4027d34fe8 2012-07-13 kinaba: g.drawText("扉", font, Color(255,255,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='O') { 4027d34fe8 2012-07-13 kinaba: g.drawText("外", font, Color(255,255,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='#') { b59ee91407 2012-07-13 kinaba: g.drawText("#", font, Color(0,0,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: if(m.data[y][x]=='.') { b59ee91407 2012-07-13 kinaba: g.drawText("・", font, Color(128,40,0), Rect((x-1)*Z, (y-1)*Z, Z, Z)); b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: void myKey(Control c, KeyEventArgs ev) b59ee91407 2012-07-13 kinaba: { b59ee91407 2012-07-13 kinaba: switch(ev.keyCode) b59ee91407 2012-07-13 kinaba: { b59ee91407 2012-07-13 kinaba: case Keys.DOWN: 8f5135c552 2012-07-13 kinaba: score += m.command_D(); 8f5135c552 2012-07-13 kinaba: write("D"); 8f5135c552 2012-07-13 kinaba: stdout.flush(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.UP: 8f5135c552 2012-07-13 kinaba: score += m.command_U(); 8f5135c552 2012-07-13 kinaba: write("U"); 8f5135c552 2012-07-13 kinaba: stdout.flush(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.LEFT: 8f5135c552 2012-07-13 kinaba: score += m.command_L(); 8f5135c552 2012-07-13 kinaba: write("L"); 8f5135c552 2012-07-13 kinaba: stdout.flush(); 8f5135c552 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.RIGHT: 8f5135c552 2012-07-13 kinaba: score += m.command_R(); 8f5135c552 2012-07-13 kinaba: write("R"); 8f5135c552 2012-07-13 kinaba: stdout.flush(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.W: 8f5135c552 2012-07-13 kinaba: score += m.wait(); 8f5135c552 2012-07-13 kinaba: write("W"); 8f5135c552 2012-07-13 kinaba: stdout.flush(); 8f5135c552 2012-07-13 kinaba: break; 246bed04b3 2012-07-13 kinaba: case Keys.A: 246bed04b3 2012-07-13 kinaba: score += m.abort(); 246bed04b3 2012-07-13 kinaba: write("A"); 246bed04b3 2012-07-13 kinaba: stdout.flush(); 246bed04b3 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: default: a06a38e3e6 2012-07-13 kinaba: break; 246bed04b3 2012-07-13 kinaba: } 246bed04b3 2012-07-13 kinaba: if(m.cleared) { 246bed04b3 2012-07-13 kinaba: writeln(); 246bed04b3 2012-07-13 kinaba: writeln("Score: ", score); 246bed04b3 2012-07-13 kinaba: Application.exit(); 8f5135c552 2012-07-13 kinaba: } 8f5135c552 2012-07-13 kinaba: this.text = .text("Score: ", score); b59ee91407 2012-07-13 kinaba: invalidate(); a06a38e3e6 2012-07-13 kinaba: } b59ee91407 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: b59ee91407 2012-07-13 kinaba: void main(string[] args) b59ee91407 2012-07-13 kinaba: { b59ee91407 2012-07-13 kinaba: Form myForm = new MyForm(new Map(File(args[1]))); b59ee91407 2012-07-13 kinaba: Application.run(myForm); a06a38e3e6 2012-07-13 kinaba: }