view trunk/mazegen.d @ 0:4b2e8e8a633e

Repository setup.
author revcompgeek
date Mon, 03 Mar 2008 19:28:10 -0700
parents
children
line wrap: on
line source

/++++++++++++++++++++++++++++++++++++++++++++++++
 +               Maze Generation                +
 +               Author: Matt P.                +
 +                 Version: 1.0                 +
 +                                              +
 + Generates a simple maze with odd dimensions. +
 ++++++++++++++++++++++++++++++++++++++++++++++++/

module mazegen;

import tango.math.Random;
import tango.io.Stdout;
import Integer = tango.text.convert.Integer;

void usage(){
	Stdout(
"Usage: mazegen width height [options]\n"
"Width and height must be odd and greater than or equal to 5.\n"
"Options:\n"
"  -e char\n"
"     Set the empty block character to char.\n"
"  -f char\n"
"     Set the filled block character to char.\n"
"  -b\n"
"     Disables printing of the border.\n"
"  -i\n"
"     Prints out how many possible mazes there are for that size and quit.\n").flush;
}

bool[][] generateMaze(uint width, uint height){
	bool[][] maze;
	
	maze.length = width;
	
	for (auto i = 0; i < width; i++){
		maze[i].length = height;
	}
	
	bool rowStart = true;
	bool vertical;
	for (auto i = 1; i < width; i+=2){
		vertical = rowStart;
		rowStart = !rowStart;
		
		for (auto j = 1; j < height; j+=2){
			maze[i][j] = true;
			if (vertical)
				maze[i + ((Random.shared.next(2)-1)?-1:1)][j] = true;
			else
				maze[i][j + ((Random.shared.next(2)-1)?-1:1)] = true;
			vertical = !vertical;
		}
	}
	
	return maze;
}
void printMaze(bool[][] m,char e = ' ', char f = 'X', bool outline = true){
	if (outline){
		Stdout("+");
		for (auto j = 0; j < m[0].length; j++){
			Stdout("-");
		}
		Stdout("+").newline;
	}
	
	for (auto i = 0; i < m.length; i++){
		if (outline) Stdout("|");
		for (auto j = 0; j < m[i].length; j++){
			if(m[i][j]) Stdout(""~f).flush;
			else Stdout(""~e).flush;
		}
		if (outline) Stdout("|").newline;
	}
	if (outline){
		Stdout("+");
		for (auto j = 0; j < m[0].length; j++){
			Stdout("-");
		}
		Stdout("+").newline;
	}
}

void main(char[][] args){
	char empty = ' ';
	char filled = 'X';
	
	if(args.length < 3){
		usage();
		return;
	}
	
	uint width = Integer.parse(args[1]);
	uint height = Integer.parse(args[2]);
	bool frame = true;
	
	if (width % 2 != 1 || height % 2 != 1 || width < 5 || height < 5){
		usage();
		return;
	}
	
	for (auto i = 3; i < args.length; i++){
		char[] arg = args[i];
		switch (arg){
			case "-h":
				usage();
				return;
			case "-e":
				empty = args[++i][0];
				break;
			case "-f":
				filled = args[++i][0];
				break;
			case "-i":
				Stdout("There are 2^")( (( width - 1 ) / 2 ) * (( height - 1 ) / 2 ) )(" different maze types for that size.").newline;
				return;
			case "-b":
				frame = false;
			default:
				Stdout.format("Unknown parameter: {}",arg).newline;
				usage();
				return;
		}
	}
	
	bool[][] maze = generateMaze(width,height);
	
	printMaze(maze,empty,filled,frame);
}