Maze Generation + Astar pathfinding

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
					
public class Program
{
	public static int size =10;
	public static int[] astarStart = {3,2};
	public static int[] astarEnd = {5,6};
	
	public static Cell[,] grid = new Cell[size,size]; 
	
	public static void Main()
	{
		for (int x =0;x<size;x++) {
			for (int y =0;y<size;y++){
					grid[y,x] = new Cell(x,y);
			}
		}
		mazeGen();
		List<Cell> path = astar();
		outputToConsole(path);
	}
	
	public static List<Cell> astar() {
		List<Cell> cellQue = new List<Cell>{grid[astarStart[1],astarStart[0]]};
		while (cellQue.Count > 0) {
			if (cellQue[0].x == astarEnd[0] && cellQue[0].y == astarEnd[1]) {
				break;
			}
			foreach (Cell c in cellQue[0].connections) {
				if (c.direction == null) {
					cellQue.Add(c);
					c.direction = cellQue[0];
				}
			}
			cellQue.RemoveAt(0); 
		}
		Cell currentCell = grid[astarEnd[1],astarEnd[0]];
		List<Cell> path=new List<Cell>{currentCell};
		while (currentCell != grid[astarStart[1],astarStart[0]]) {
			currentCell = currentCell.direction;
			path.Add(currentCell);
		}
		return path;
	}
	

	
	public static void mazeGen() {
		Cell currentCell = mazeGenStep(grid[0,0]);
		while ((currentCell = mazeGenStep(currentCell)) != null) {}
	}
	
	public static Cell mazeGenStep(Cell currentCell) {
		currentCell.visited = true;
		List<int> avalableDir = new List<int> {1,2,3,4};
		Random ran = new Random();
		while (avalableDir.Count>0) {
			int dirIndex = ran.Next(0,avalableDir.Count);
			switch (avalableDir[dirIndex]) { 
				case 1:
					if (currentCell.up != null && !currentCell.up.visited) {
						currentCell.up.back = currentCell;
						return currentCell.up;
					} 
				break;
				case 2:
					if (currentCell.right != null && !currentCell.right.visited) {
						currentCell.right.back = currentCell;
						return currentCell.right;
					} 
				break;
				case 3:
					if (currentCell.down != null && !currentCell.down.visited) {
						currentCell.down.back = currentCell;
						return currentCell.down;
					} 
				break;
				case 4:
					if (currentCell.left != null && !currentCell.left.visited) {
						currentCell.left.back = currentCell;
						return currentCell.left;
					} 
				break;
			}
			avalableDir.RemoveAt(dirIndex);
		}
		if (currentCell.back == null ) {
			return null;
		}
		currentCell.connections.Add(currentCell.back);
		currentCell.back.connections.Add(currentCell);
		if (currentCell.back.x ==0 && currentCell.back.y ==0) {
			return null;
		}
		return currentCell.back;
	}
	
	public static void outputToConsole(List<Cell> path) {
		for (int y =size-1;y>=0;y--) {
			for (int x =0;x<size;x++){//top
				Console.Write('#');
				if (!grid[y,x].connections.Contains(grid[y,x].up)) {
					Console.Write('#');
				} else if (path.Contains(grid[y,x]) && path.Contains(grid[y,x].up)) {
					Console.Write(':');
				} else {
					Console.Write(' ');	
				}
				Console.Write('#');
			}
			Console.WriteLine(' ');
			for (int x =0;x<size;x++){//mid
				if (!grid[y,x].connections.Contains(grid[y,x].left)) {
					Console.Write('#');
				} else if (path.Contains(grid[y,x]) && path.Contains(grid[y,x].left)) {
					Console.Write(':');
				} else {
					Console.Write(' ');	
				}
				if (path[0]==grid[y,x]||path[path.Count-1]==grid[y,x]){
					Console.Write('E');
				}else if (path.Contains(grid[y,x])) {
					Console.Write(':');
				} else {
					Console.Write(' ');
				}
				if (!grid[y,x].connections.Contains(grid[y,x].right)) {
					Console.Write('#');
				} else if (path.Contains(grid[y,x]) && path.Contains(grid[y,x].right)) {
					Console.Write(':');
				} else {
					Console.Write(' ');	
				}
			}
			Console.WriteLine(' ');
			for (int x =0;x<size;x++){//bottom
				Console.Write('#');
				if (!grid[y,x].connections.Contains(grid[y,x].down)) {
					Console.Write('#');
				} else if (path.Contains(grid[y,x]) && path.Contains(grid[y,x].down)) {
					Console.Write(':');
				} else {
					Console.Write(' ');	
				}
				Console.Write('#');
			}
			Console.WriteLine(' ');
		}
	}
	
	
	
	public class Cell {
		public int x;
		public int y;
		public bool visited = false; 
		public Cell direction;//for astar
		public Cell back;//for maze gen
		public List<Cell> connections = new List<Cell>();
		
		public Cell up {
			get {
				if (y+1>=size) {return null;}
				return grid[y+1,x];
			}
			set {}
		}
		public Cell right {
			get {
				if (x+1>=size) {return null;}
				return grid[y,x+1];
			}
			set {}
		}
		public Cell down {
			get {
				if (y-1<0) {return null;}
				return grid[y-1,x];
			}
			set {}
		}
		public Cell left {
			get {
				if (x-1<0) {return null;}
				return grid[y,x-1];
			}
			set {}
		}
		
		public Cell(int x,int y) {
			this.x = x;
			this.y = y;
		}
	}
}
;