using System.Diagnostics;
using System.Collections.Generic;
public static void Main()
Console.WriteLine("Room Generator");
Random rnd = new Random(777);
public static void generateTree(int seed)
const int MAX_LEAF_SIZE = 20;
List<Leaf> leaves = new List<Leaf>();
Leaf root = new Leaf(0,0,40,20, seed);
Random rnd = new Random(seed+777);
int splitIdx = rnd.Next(leaves.Count);
Leaf toSplit = leaves[splitIdx];
leaves.Add( toSplit.leftChild );
leaves.Add( toSplit.rightChild );
public static void printDungeons(List<Leaf> leaves) {
char[][] lines = new char[40][];
for( int i = 0; i < 40; i++ )
for( int j = 0; j < 20; j++ )
foreach( Leaf r in leaves )
for( int i = 0; i < d.Height; i++ )
for( int j = 0; j < d.Width; j++ )
lines[ d.X + i ][ d.Y+ j ] = ' ';
for( int j = 0; j < 20; j++ )
for( int i = 0; i < 40; i++ )
Console.Write(lines[i][j]);
public int x, y, width, height;
public Leaf leftChild, rightChild;
public List<Rectangle> halls = new List<Rectangle>();
public Leaf(int inX, int inY, int inWidth, int inHeight, int seed = 0)
if (leftChild != null || rightChild != null)
bool isSplitX = rnd.Next(2)==1;
if (width > height && height / width >= 0.05)
else if (height > width && width / height >= 0.05)
int max = (isSplitX?height:width) - MIN_SIZE;
int splitPos = rnd.Next(MIN_SIZE, max);
leftChild = new Leaf(x, y, width, splitPos, rnd.Next(111));
rightChild = new Leaf(x, y + splitPos, width, height - splitPos, rnd.Next(222));
leftChild = new Leaf(x, y, splitPos, height, rnd.Next(333));
rightChild = new Leaf(x + splitPos, y, width - splitPos, height, rnd.Next(444));
public void generateRoom()
leftChild.generateRoom();
rightChild.generateRoom();
int dungeonHeight = Math.Max(height-1, MIN_SIZE );;
int dungeonWidth = Math.Max(width-1, MIN_SIZE );;
room = new Rectangle( x+dungeonLeft, y + dungeonTop, dungeonHeight, dungeonWidth);