• Home
  • About
    • Hanna's Blog photo

      Hanna's Blog

      I wanna be a global developer.

    • Learn More
    • Email
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[C#] Binary Tree Maze

25 Dec 2020

Reading time ~2 minutes

Reference by [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part2: 자료구조와 알고리즘

Code

  • Program.cs
  using System;

  namespace Maze
  {
    class Program
    {
      static void Main(string[] args)
      {
        Board board = new Board();
        board.GeneratedByBinaryTree(25);

        Console.CursorVisible = false;

        while (true)
        {
          Console.SetCursorPosition(0, 0);
          board.Render();
        }
      }
    }
  }
  • Board.cs
  using System;
  using System.Collections.Generic;
  using System.Text;

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

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

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

        for(int y = 0; y < _size; y++)
        {
          for(int x = 0; x < _size; x++)
          {
            // Make wall when x or y is enum
            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++)
          {
            // Pass in wall
            if (x % 2 == 0 || y % 2 == 0)
              continue;

            // Pass in [size - 2, size - 2]
            if (x == _size - 2 && y == _size - 2)
              continue;

            // Change only right direction
            if(y == _size - 2)
            {
              _tile[y, x + 1] = TileType.Empty;
              continue;
            }

            // Change only bottom direction
            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;

          }
        }
      }

      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();
        }
      }

      ConsoleColor GetTileColor(TileType type)
      {
        switch (type)
        {
          case TileType.Empty:
            return ConsoleColor.Green;
          case TileType.Wall:
            return ConsoleColor.Red;
          default:
            return ConsoleColor.Green;
        }
      }
    }
  }

Algorithm

  • Initialize
    • Make first row and column and last row and column to wall
    • Make every enum-th row and column to wall
  • Binary Tree Maze
    • Change next row and column of enum-th to empty randomly
    • If 2nd last row, change only right direction
    • If 2nd last column, change only bottom direction

Console

  • CursorVisible
    • Can show/hide cursor
  Console.CursorVisible = false;
  • SetCursorPosition
    • Set position of cursor with number;
  Console.SetCursorPosition(0, 0);

Result

C# Binary Tree Maze

Download



C#GameAlgorithm Share Tweet +1