Maze solving: Difference between revisions
Content deleted Content added
Updated D entry |
|||
Line 667: | Line 667: | ||
This entry reads a maze generated by http://rosettacode.org/wiki/Maze_generation#D and chooses two random start-end points. |
This entry reads a maze generated by http://rosettacode.org/wiki/Maze_generation#D and chooses two random start-end points. |
||
<lang d>import std.stdio, std.random, std.string, std.array, std.algorithm, |
<lang d>import std.stdio, std.random, std.string, std.array, std.algorithm, |
||
std.file; |
std.file, std.conv; |
||
enum int cx = 4, cy = 2; // Cell size x and y. |
enum int cx = 4, cy = 2; // Cell size x and y. |
||
Line 674: | Line 674: | ||
struct V2 { int x, y; } |
struct V2 { int x, y; } |
||
bool solveMaze(char[][] maze, in V2 s, in V2 end) pure nothrow { |
bool solveMaze(char[][] maze, in V2 s, in V2 end) pure nothrow @safe @nogc { |
||
if (s == end) |
if (s == end) |
||
return true; |
return true; |
||
foreach (d; [V2(0, -cy), V2(+cx, 0), V2(0, +cy), V2(-cx, 0)]) |
foreach (immutable d; [V2(0, -cy), V2(+cx, 0), V2(0, +cy), V2(-cx, 0)]) |
||
if (maze[s.y + (d.y / 2)][s.x + (d.x / 2)] == ' ' && |
if (maze[s.y + (d.y / 2)][s.x + (d.x / 2)] == ' ' && |
||
maze[s.y + d.y][s.x + d.x] == ' ') { |
maze[s.y + d.y][s.x + d.x] == ' ') { |
||
Line 691: | Line 691: | ||
void main() { |
void main() { |
||
auto maze = "maze.txt". |
auto maze = "maze.txt".File.byLine.map!(r => r.chomp.dup).array; |
||
immutable |
immutable h = (maze.length.signed - 1) / cy; |
||
assert (h > 0); |
assert (h > 0); |
||
immutable |
immutable w = (maze[0].length.signed - 1) / cx; |
||
immutable start = V2(cx2 + cx * uniform(0, w), |
immutable start = V2(cx2 + cx * uniform(0, w), cy2 + cy * uniform(0, h)); |
||
immutable end = V2(cx2 + cx * uniform(0, w), cy2 + cy * uniform(0, h)); |
|||
immutable end = V2(cx2 + cx * uniform(0, w), |
|||
cy2 + cy * uniform(0, h)); |
|||
maze[start.y][start.x] = pathSymbol; |
maze[start.y][start.x] = pathSymbol; |