comparison trunk/mazegen.d @ 0:4b2e8e8a633e

Repository setup.
author revcompgeek
date Mon, 03 Mar 2008 19:28:10 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4b2e8e8a633e
1 /++++++++++++++++++++++++++++++++++++++++++++++++
2 + Maze Generation +
3 + Author: Matt P. +
4 + Version: 1.0 +
5 + +
6 + Generates a simple maze with odd dimensions. +
7 ++++++++++++++++++++++++++++++++++++++++++++++++/
8
9 module mazegen;
10
11 import tango.math.Random;
12 import tango.io.Stdout;
13 import Integer = tango.text.convert.Integer;
14
15 void usage(){
16 Stdout(
17 "Usage: mazegen width height [options]\n"
18 "Width and height must be odd and greater than or equal to 5.\n"
19 "Options:\n"
20 " -e char\n"
21 " Set the empty block character to char.\n"
22 " -f char\n"
23 " Set the filled block character to char.\n"
24 " -b\n"
25 " Disables printing of the border.\n"
26 " -i\n"
27 " Prints out how many possible mazes there are for that size and quit.\n").flush;
28 }
29
30 bool[][] generateMaze(uint width, uint height){
31 bool[][] maze;
32
33 maze.length = width;
34
35 for (auto i = 0; i < width; i++){
36 maze[i].length = height;
37 }
38
39 bool rowStart = true;
40 bool vertical;
41 for (auto i = 1; i < width; i+=2){
42 vertical = rowStart;
43 rowStart = !rowStart;
44
45 for (auto j = 1; j < height; j+=2){
46 maze[i][j] = true;
47 if (vertical)
48 maze[i + ((Random.shared.next(2)-1)?-1:1)][j] = true;
49 else
50 maze[i][j + ((Random.shared.next(2)-1)?-1:1)] = true;
51 vertical = !vertical;
52 }
53 }
54
55 return maze;
56 }
57 void printMaze(bool[][] m,char e = ' ', char f = 'X', bool outline = true){
58 if (outline){
59 Stdout("+");
60 for (auto j = 0; j < m[0].length; j++){
61 Stdout("-");
62 }
63 Stdout("+").newline;
64 }
65
66 for (auto i = 0; i < m.length; i++){
67 if (outline) Stdout("|");
68 for (auto j = 0; j < m[i].length; j++){
69 if(m[i][j]) Stdout(""~f).flush;
70 else Stdout(""~e).flush;
71 }
72 if (outline) Stdout("|").newline;
73 }
74 if (outline){
75 Stdout("+");
76 for (auto j = 0; j < m[0].length; j++){
77 Stdout("-");
78 }
79 Stdout("+").newline;
80 }
81 }
82
83 void main(char[][] args){
84 char empty = ' ';
85 char filled = 'X';
86
87 if(args.length < 3){
88 usage();
89 return;
90 }
91
92 uint width = Integer.parse(args[1]);
93 uint height = Integer.parse(args[2]);
94 bool frame = true;
95
96 if (width % 2 != 1 || height % 2 != 1 || width < 5 || height < 5){
97 usage();
98 return;
99 }
100
101 for (auto i = 3; i < args.length; i++){
102 char[] arg = args[i];
103 switch (arg){
104 case "-h":
105 usage();
106 return;
107 case "-e":
108 empty = args[++i][0];
109 break;
110 case "-f":
111 filled = args[++i][0];
112 break;
113 case "-i":
114 Stdout("There are 2^")( (( width - 1 ) / 2 ) * (( height - 1 ) / 2 ) )(" different maze types for that size.").newline;
115 return;
116 case "-b":
117 frame = false;
118 default:
119 Stdout.format("Unknown parameter: {}",arg).newline;
120 usage();
121 return;
122 }
123 }
124
125 bool[][] maze = generateMaze(width,height);
126
127 printMaze(maze,empty,filled,frame);
128 }