Search

C# 미로 알고리즘

1. Binary Tree 미로 생성 알고리즘

################################### #Program.cs ################################### using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algorithm { class Program { static void Main(string[] args) { Board board = new Board(); board.Initialize(25); Console.CursorVisible = false; // 커서 깜빡깜빡 거리는거 없애기. const int WAIT_TICK = 1000 / 30; const char CIRCLE = '\u25cf'; int lastTick = 0; while (true) { #region 프레임 관리 int currentTick = System.Environment.TickCount; //이건 절대적인 시간 기준은 아님.//ms 단위. //만약에 경과한 시간이 1/30초보다 작다면 if (currentTick - lastTick < WAIT_TICK) continue; lastTick = currentTick; #endregion //FPS 프레임(60프레임 OK 30 프레임 이하로 NO) //입력 //로직 //렌더링 Console.SetCursorPosition(0, 0); //커서 고정 board.Render(); } } } }
C#
복사
################################### #Board.cs ################################### using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algorithm { class Board { const char CIRCLE = '\u25cf'; public TileType[,] _tile; // 배열 public int _size; public enum TileType { Empty, Wall, } public void Initialize(int size) { _tile = new TileType[size, size]; _size = size; for(int y = 0; y < _size; y++) { for(int x = 0; x < _size; x++) { if (x == 0 || x == _size - 1 || y == 0 || y == size - 1) _tile[y, x] = TileType.Wall; else _tile[y, x] = TileType.Empty; } } } public void Render() { ConsoleColor prevColor = Console.ForegroundColor; for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { Console.ForegroundColor = GetTileColor(_tile[y, x]); Console.Write(CIRCLE); } Console.WriteLine(); } Console.ForegroundColor = prevColor; } ConsoleColor GetTileColor(TileType type) { switch (type) { case TileType.Empty: return ConsoleColor.Green; case TileType.Wall: return ConsoleColor.Red; default: return ConsoleColor.Green; } } } }
C#
복사
public void Initialize(int size) { _tile = new TileType[size, size]; _size = size; //일단 길을 다 막아버리는 작업 for(int y = 0; y < _size; y++) { for(int x = 0; x < _size; x++) { if (x % 2 == 0 || y % 2 == 0) _tile[y, x] = TileType.Wall; else _tile[y, x] = TileType.Empty; } }
C#
복사
//랜덤으로 우측 혹은 아래로 길을 뚫는 작업 Random rand = new Random(); for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { if (x % 2 == 0 || y % 2 == 0) continue; //1/2 확률로 길 뚫기 if(rand.Next(0, 2) == 0) { _tile[y, x+1] = TileType.Empty; } else { _tile[y + 1, x] = TileType.Empty; } } }
C#
복사
Random rand = new Random(); for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { if (x % 2 == 0 || y % 2 == 0) continue; if (y == _size - 2 && x == _size - 2) continue; if (y == _size - 2 ) { _tile[y, x + 1] = TileType.Empty; continue; } if(x == _size - 2) { _tile[y + 1, x] = TileType.Empty; continue; } //1/2 확률로 길 뚫기 if(rand.Next(0, 2) == 0) { _tile[y, x+1] = TileType.Empty; } else { _tile[y + 1, x] = TileType.Empty; } } } }
C#
복사
public void Initialize(int size) { _tile = new TileType[size, size]; _size = size; GenerateByBinaryTree(); } void GenerateByBinaryTree() { //일단 길을 다 막아버리는 작업 for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { if (x % 2 == 0 || y % 2 == 0) _tile[y, x] = TileType.Wall; else _tile[y, x] = TileType.Empty; } } //랜덤으로 우측 혹은 아래로 길을 뚫는 작업 Random rand = new Random(); for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { if (x % 2 == 0 || y % 2 == 0) continue; if (y == _size - 2 && x == _size - 2) continue; if (y == _size - 2) { _tile[y, x + 1] = TileType.Empty; continue; } if (x == _size - 2) { _tile[y + 1, x] = TileType.Empty; continue; } //1/2 확률로 길 뚫기 if (rand.Next(0, 2) == 0) { _tile[y, x + 1] = TileType.Empty; } else { _tile[y + 1, x] = TileType.Empty; } } } }
C#
복사

플레이어 이동

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algorithm { class Player { public int PosX { get; set; } public int PosY { get; set; } } }
C#
복사
public void Render() { ConsoleColor prevColor = Console.ForegroundColor; for (int y = 0; y < _size; y++) { for (int x = 0; x < _size; x++) { //플레이어 좌표를 갖고 와서, 그 좌표랑 현재 y, x가 일치하면 플레이어 전용 색상으로 표시. if (y == _player.PosY && x == _player.PosX) { Console.ForegroundColor = ConsoleColor.Blue; } else Console.ForegroundColor = GetTileColor(_tile[y, x]); Console.Write(CIRCLE); } Console.WriteLine(); } Console.ForegroundColor = prevColor; }
C#
복사