전체 코드
namespace Algorithm
{
class Program
{
static void Main(string[] args)
{
Board board = new Board();
Player player = new Player();
board.Initialize(25,player);
Console.CursorVisible = false;
player.Initialize(1, 1,board.Size-2, board.Size-2, board);
const int MAX_TICK = 1000 / 30;
int lastTick = 0;
while (true)
{
#region 프레임 관리
int currentTick = System.Environment.TickCount;
int elapsedTick = currentTick - lastTick;
if (elapsedTick < MAX_TICK)
{
continue;
}
int deltaTick = currentTick - lastTick;
lastTick = currentTick;
#endregion
player.Update(deltaTick);
Console.SetCursorPosition(0, 0);
board.Render();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
class Board
{
public enum TileType
{
Empty,
Wall,
}
const char CIRCLE = '\u25cf';
public TileType[,] Tile { get; private set; }
public int Size { get; private set; }
Player _player;
public void Initialize(int size, Player player)
{
if (size % 2 == 0)
{
return;
}
_player = player;
Tile = new TileType[size, size];
Size = size;
GenerateBySideWinder();
}
public void GenrateByBinaryTree()
{
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;
}
if (rand.Next(0, 2) == 0)
{
Tile[y, x + 1] = TileType.Empty;
}
else
{
Tile[y + 1, x] = TileType.Empty;
}
}
}
}
public void GenerateBySideWinder()
{
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++)
{
int count = 0;
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;
count++;
}
else
{
int randomIndex = rand.Next(0, count);
Tile[y + 1, x - randomIndex * 2] = TileType.Empty;
count = 1;
}
}
}
}
public void Render()
{
ConsoleColor prevColor = Console.ForegroundColor;
for (int y = 0; y < Size; y++)
{
for (int x = 0; x < Size; x++)
{
if (y == _player.PosY && x == _player.PosX)
Console.ForegroundColor = ConsoleColor.Blue;
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;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
class Player
{
public int PosY { get; private set; }
public int PosX { get; private set; }
Random _random = new Random();
Board _board;
public void Initialize(int posY, int posX, int destX, int destY, Board board)
{
PosX = posX;
PosY = posY;
_board = board;
}
const int MOVE_TICK = 100;
int _sumTick = 0;
public void Update(int deltaTick)
{
_sumTick += deltaTick;
if (_sumTick >= MOVE_TICK)
{
_sumTick = 0;
int randValue = _random.Next(0, 4);
switch (randValue)
{
case 0:
if (PosY - 1 >= 0 && _board.Tile[PosY - 1, PosX] == Board.TileType.Empty)
PosY = PosY - 1;
break;
case 1:
if (PosY + 1 < _board.Size && _board.Tile[PosY + 1, PosX] == Board.TileType.Empty)
PosY = PosY + 1;
break;
case 2:
if (PosX - 1 >= 0 && _board.Tile[PosY, PosX - 1] == Board.TileType.Empty)
PosX = PosX - 1;
break;
case 3:
if (PosX + 1 < _board.Size && _board.Tile[PosY, PosX + 1] == Board.TileType.Empty)
PosX = PosX + 1;
break;
}
}
}
}
}
Player 클래스 (플레이어 이동 구현)
class Player
{
public int PosY { get; private set; }
public int PosX { get; private set; }
Random _random = new Random();
Board _board;
const int MOVE_TICK = 100;
int _sumTick = 0;
public void Initialize(int posY, int posX, Board board)
{
PosX = posX;
PosY = posY;
_board = board;
}
public void Update(int deltaTick)
{
_sumTick += deltaTick;
if (_sumTick >= MOVE_TICK)
{
_sumTick = 0;
int randValue = _random.Next(0, 4);
switch (randValue)
{
case 0:
if (PosY - 1 >= 0 && _board.Tile[PosY - 1, PosX] == Board.TileType.Empty)
PosY -= 1;
break;
case 1:
if (PosY + 1 < _board.Size && _board.Tile[PosY + 1, PosX] == Board.TileType.Empty)
PosY += 1;
break;
case 2:
if (PosX - 1 >= 0 && _board.Tile[PosY, PosX - 1] == Board.TileType.Empty)
PosX -= 1;
break;
case 3:
if (PosX + 1 < _board.Size && _board.Tile[PosY, PosX + 1] == Board.TileType.Empty)
PosX += 1;
break;
}
}
}
}
설명
PosX, PosY: 플레이어 위치를 나타냄.
MOVE_TICK = 100: 0.1초마다 이동하도록 설정.
Update(int deltaTick): 일정 시간이 지나면 4방향 중 하나를 랜덤 선택해 이동.
Initialize(int posY, int posX, Board board): 초기 위치 및 보드 정보를 저장.
Board 클래스 (미로 보드 구현)
class Board
{
const char CIRCLE = '\u25cf';
public TileType[,] Tile { get; private set; }
public int Size { get; private set; }
Player _player;
public enum TileType { Empty, Wall }
public void Initialize(int size, Player player)
{
if (size % 2 == 0) return;
_player = player;
Tile = new TileType[size, size];
Size = size;
GenerateBySideWinder();
}
public void Render()
{
ConsoleColor prevColor = Console.ForegroundColor;
for (int y = 0; y < Size; y++)
{
for (int x = 0; x < Size; 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;
}
}
설명
TileType[,] Tile: 미로 데이터를 저장하는 2차원 배열.
Initialize(int size, Player player): 보드 크기를 초기화하고 미로 생성 알고리즘 실행.
Render(): 콘솔 화면에 미로와 플레이어를 출력.
Program 클래스 (메인 게임 루프 구현)
class Program
{
static void Main(string[] args)
{
Board board = new Board();
Player player = new Player();
board.Initialize(25, player);
player.Initialize(1, 1, board);
Console.CursorVisible = false;
const int MAX_TICK = 1000 / 30;
int lastTick = 0;
while (true)
{
int currentTick = Environment.TickCount;
int deltaTick = currentTick - lastTick;
if (deltaTick < MAX_TICK) continue;
lastTick = currentTick;
player.Update(deltaTick);
Console.SetCursorPosition(0, 0);
board.Render();
}
}
}
설명
board.Initialize(25, player): 미로 크기 25×25로 설정.
player.Initialize(1, 1, board): 플레이어 시작 위치 설정.
MAX_TICK = 1000 / 30: 30 프레임을 유지하도록 설정.
while (true): 무한 루프로 프레임 관리.
deltaTick을 계산해 이동 로직 실행 후 Render() 호출.
프레임 관리 원리
const int MAX_TICK = 1000 / 30;
int lastTick = 0;
while (true)
{
int currentTick = Environment.TickCount;
int deltaTick = currentTick - lastTick;
if (deltaTick < MAX_TICK) continue;
lastTick = currentTick;
}
설명
Environment.TickCount: 현재 시간을 밀리초 단위로 반환.
MAX_TICK = 1000 / 30: 한 프레임당 약 33ms (30FPS) 유지.
- 만약 경과 시간이
MAX_TICK보다 작으면 루프를 건너뛰어 프레임 속도를 조절.