Created
March 6, 2019 15:13
-
-
Save Bradshaw/8118226ad71789b5ae1693bf815cc016 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Maze : MonoBehaviour | |
{ | |
Node[,] maze; | |
public int width; | |
public int height; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
maze = new Node[width,height]; | |
for (int i=0; i<maze.GetLength(0); i++){ | |
for (int j=0; j<maze.GetLength(1); j++){ | |
maze[i,j] = new Node(); | |
if (i>0) { | |
maze[i,j].leftNeighbor = maze[i-1,j]; | |
maze[i-1,j].rightNeighbor = maze[i,j]; | |
} | |
if (j>0) { | |
maze[i,j].upNeighbor = maze[i,j-1]; | |
maze[i,j-1].downNeighbor = maze[i,j]; | |
} | |
} | |
} | |
maze[0,0].MakeMaze(); | |
string mazeDisplay = ""; | |
for (int j=0; j<maze.GetLength(1); j++){ | |
for (int i=0; i<maze.GetLength(0); i++){ | |
mazeDisplay += maze[i,j].PrintMe(); | |
} | |
mazeDisplay+="\n"; | |
} | |
Debug.Log(mazeDisplay); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
} | |
public enum Direction { | |
left, | |
down, | |
up, | |
right | |
} | |
public class Node{ | |
public bool marked; | |
public Node leftNeighbor; | |
public Node rightNeighbor; | |
public Node downNeighbor; | |
public Node upNeighbor; | |
public bool connectUp; | |
public bool connectDown; | |
public bool connectLeft; | |
public bool connectRight; | |
public void MakeMaze(){ | |
marked = true; | |
List<Direction> directions = new List<Direction>(); | |
directions.Add(Direction.left); | |
directions.Add(Direction.down); | |
directions.Add(Direction.up); | |
directions.Add(Direction.right); | |
Shuffle(directions); | |
foreach (Direction direction in directions){ | |
Node next = null; | |
switch (direction){ | |
case Direction.up: | |
next = upNeighbor; | |
break; | |
case Direction.down: | |
next = downNeighbor; | |
break; | |
case Direction.left: | |
next = leftNeighbor; | |
break; | |
case Direction.right: | |
next = rightNeighbor; | |
break; | |
} | |
if (next!=null && !next.marked){ | |
switch (direction){ | |
case Direction.up: | |
connectUp = true; | |
next.connectDown = true; | |
break; | |
case Direction.down: | |
connectDown = true; | |
next.connectUp = true; | |
break; | |
case Direction.left: | |
connectLeft = true; | |
next.connectRight = true; | |
break; | |
case Direction.right: | |
connectRight = true; | |
next.connectLeft = true; | |
break; | |
} | |
next.MakeMaze(); | |
} | |
} | |
} | |
public string PrintMe(){ | |
Dictionary<string,string> pictos = new Dictionary<string,string>(); | |
pictos.Add("ur", "└"); | |
pictos.Add("ulr", "┴"); | |
pictos.Add("dlr", "┬"); | |
pictos.Add("udr", "├"); | |
pictos.Add("lr", "─"); | |
pictos.Add("udlr", "┼"); | |
pictos.Add("dr", "┌"); | |
pictos.Add("dl", "┐"); | |
pictos.Add("ul", "┘"); | |
pictos.Add("ud", "│"); | |
pictos.Add("udl", "┤"); | |
pictos.Add("u", "╹"); | |
pictos.Add("d", "╻"); | |
pictos.Add("l", "╸"); | |
pictos.Add("r", "╺"); | |
string selectPicto = ""; | |
if (connectUp) selectPicto+="u"; | |
if (connectDown) selectPicto+="d"; | |
if (connectLeft) selectPicto+="l"; | |
if (connectRight) selectPicto+="r"; | |
if (pictos.ContainsKey(selectPicto)){ | |
return pictos[selectPicto]; | |
} else { | |
return "░"; | |
} | |
} | |
void Shuffle<T>(List<T> list){ | |
for (int i = 0; i<list.Count; i++){ | |
int u = Random.Range(i,list.Count); | |
T moving = list[u]; | |
list.RemoveAt(u); | |
list.Insert(i,moving); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment