본문 바로가기
알고리즘 및 디자인패턴

Binary Tree

by Mostlove 2023. 7. 13.
728x90
반응형

using System;


namespace Algorithm
{
    
    class Board
    {
        public TileType[,] _tile;
        public int _size;
        const char CIRCLE = '\u25cf';
        public enum TileType
        {
            Empty,
            Wall
        }

        public void Initialize(int size)
        {
            if (size % 2 == 0) 
                return;

            _tile = new TileType[size, size];
            _size = size;

            //Mazes for Programmers
            //일단 길을 다 막아버리는 작업
            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;
                }
            }
            //랜덤으로 우측 혹은 아래로 길을 뚫는 작업
            GenerateByBinaryTree();
            
        }
        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();
            }
        }
        void GenerateByBinaryTree()
        {
            //Binary Tree Algorithm
            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;
                    }
                    if (rand.Next(0, 2) == 0)
                    {
                        _tile[y, x + 1] = TileType.Empty;
                    }
                    else
                    {
                        _tile[y + 1, x] = TileType.Empty;
                    }
                }
            }
        }
        ConsoleColor GetTileColor(TileType type)
        {
            switch (type)
            {
                case TileType.Empty:
                    return ConsoleColor.Green;
                case TileType.Wall:
                    return ConsoleColor.Red;
                default:
                    return ConsoleColor.Green;
            }
        }
    }
}

반응형

'알고리즘 및 디자인패턴' 카테고리의 다른 글

플레이어 이동  (0) 2023.07.13
SideWinder 미로 생성 알고리즘  (0) 2023.07.13
맵 만들기  (0) 2023.07.13
연결 리스트 구현 연습  (0) 2023.07.13
동적 배열 구현 연습  (0) 2023.07.13