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