Check-in [a06a38e3e6]
Not logged in
Overview
SHA1 Hash:a06a38e3e6d81ba687e30cbfd0baabdbf539bf7c
Date: 2012-07-13 22:17:25
User: kinaba
Comment:Hand playable.
Timelines: family | ancestors | descendants | both | trunk
Diffs: redesign
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Added test.d version [53cab87f74c4871d]

1 +import std.algorithm; 2 +import std.array; 3 +import std.conv; 4 +import std.stdio; 5 +import std.string; 6 + 7 +class Map 8 +{ 9 + private char[][] data; 10 + 11 + this(File input) 12 + { 13 + foreach(s; input.byLine()) 14 + data ~= s.chomp.dup; 15 + 16 + int width = 0; 17 + foreach(s; data) 18 + width = max(width, s.length); 19 + 20 + // space padding and sentinels 21 + foreach(ref s; data) { 22 + int p = s.length; 23 + s.length = width; 24 + s[p..$] = ' '; 25 + s = '#' ~ s ~ '#'; 26 + } 27 + 28 + // vertical sentinel 29 + char[] sen = new char[width+2]; 30 + sen[] = '#'; 31 + data = sen.dup ~ data ~ sen; 32 + } 33 + 34 + @property const 35 + { 36 + int W() { return data[0].length; } 37 + int H() { return data.length; } 38 + string toString() { 39 + string result; 40 + foreach(i,s; data) { 41 + if(i) result ~= '\n'; 42 + result ~= s.idup; 43 + } 44 + return result; 45 + } 46 + } 47 + 48 + void command_R() { move(0, +1); } 49 + void command_L() { move(0, -1); } 50 + void command_U() { move(-1, 0); } 51 + void command_D() { move(+1, 0); } 52 + void wait() { update(); } 53 + 54 + void move(int dy, int dx) { 55 + foreach(y,s; data) 56 + foreach(x,c; s) 57 + if(c == 'R') 58 + return move(dy, dx, y, x); 59 + } 60 + 61 + void move(int dy, int dx, int y, int x) { 62 + if(data[y+dy][x+dx]==' ' || data[y+dy][x+dx]=='.' 63 + || data[y+dy][x+dx]=='\\' || data[y+dy][x+dx]=='O') { 64 + data[y][x]=' '; 65 + data[y+dy][x+dx]='R'; 66 + } else if(dy==0 && data[y+dy][x+dx]=='*' && data[y+2*dy][x+2*dx]==' ') { 67 + data[y][x]=' '; 68 + data[y+dy][x+dx]='R'; 69 + data[y+2*dy][x+2*dx]='*'; 70 + } 71 + update(); 72 + } 73 + 74 + void update() { 75 + char[][] next; 76 + foreach(y,s; data) 77 + next ~= s.dup; 78 + 79 + bool lambda = false; 80 + for(int y=1; y+1<H; ++y) 81 + for(int x=1; x+1<W; ++x) 82 + lambda |= (data[y][x] == '\\'); 83 + 84 + for(int y=1; y+1<H; ++y) 85 + for(int x=1; x+1<W; ++x) { 86 + if(data[y][x]=='*') { 87 + if(data[y+1][x]==' ') { 88 + next[y][x]=' '; 89 + next[y+1][x]='*'; 90 + } 91 + else if(data[y+1][x]=='*' && data[y][x+1]==' ' && data[y+1][x+1]==' ') { 92 + next[y][x]=' '; 93 + next[y+1][x+1]='*'; 94 + } 95 + else if(data[y+1][x]=='*' && data[y][x-1]==' ' && data[y+1][x-1]==' ') { 96 + next[y][x]=' '; 97 + next[y+1][x-1]='*'; 98 + } 99 + else if(data[y+1][x]=='\\' && data[y][x+1]==' ' && data[y+1][x+1]==' ') { 100 + next[y][x]=' '; 101 + next[y+1][x+1]='*'; 102 + } 103 + } 104 + else if(data[y][x]=='L') { 105 + if(!lambda) 106 + next[y][x] = 'O'; 107 + } 108 + } 109 + data = next; 110 + } 111 +} 112 + 113 +void main(string[] args) 114 +{ 115 + Map m = new Map(File(args[1])); 116 + for(;;) { 117 + writeln(m); 118 + write("> "); 119 + string s = readln(); 120 + if(s.length == 0) 121 + break; 122 + s = s.chomp().toUpper(); 123 + if(s=="R")m.command_R(); 124 + if(s=="L")m.command_L(); 125 + if(s=="U")m.command_U(); 126 + if(s=="D")m.command_D(); 127 + if(s=="W")m.wait(); 128 + } 129 + 130 +}