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; 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: a06a38e3e6 2012-07-13 kinaba: void command_R() { move(0, +1); } a06a38e3e6 2012-07-13 kinaba: void command_L() { move(0, -1); } a06a38e3e6 2012-07-13 kinaba: void command_U() { move(-1, 0); } a06a38e3e6 2012-07-13 kinaba: void command_D() { move(+1, 0); } a06a38e3e6 2012-07-13 kinaba: void wait() { update(); } a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: void 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); a06a38e3e6 2012-07-13 kinaba: } a06a38e3e6 2012-07-13 kinaba: a06a38e3e6 2012-07-13 kinaba: void move(int dy, int dx, int y, int x) { 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(); 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: 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: 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]='*'; 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]='*'; 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]='*'; 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]='*'; 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; 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; 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') { b59ee91407 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') { b59ee91407 2012-07-13 kinaba: g.drawText("L", 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') { b59ee91407 2012-07-13 kinaba: g.drawText("O", 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: b59ee91407 2012-07-13 kinaba: m.command_D(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.UP: b59ee91407 2012-07-13 kinaba: m.command_U(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.LEFT: b59ee91407 2012-07-13 kinaba: m.command_L(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.RIGHT: b59ee91407 2012-07-13 kinaba: m.command_R(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: case Keys.W: b59ee91407 2012-07-13 kinaba: m.wait(); b59ee91407 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: default: a06a38e3e6 2012-07-13 kinaba: break; b59ee91407 2012-07-13 kinaba: } 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: }