전체 코드
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; }
public int DestY { get; private set; }
public int DestX { 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;
DestY = Size - 2;
DestX = Size - 2;
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;
else if (y == DestY && x == DestX)
Console.ForegroundColor = ConsoleColor.Yellow;
else
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;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
class Pos
{
public Pos(int y, int x) { Y = y; X = x; }
public int Y;
public int X;
}
class Player
{
public int PosY { get; private set; }
public int PosX { get; private set; }
Random _random = new Random();
Board _board;
enum Dir
{
Up = 0,
Left = 1,
Down = 2,
Right = 3
}
int _dir = (int)Dir.Up;
List<Pos> _points = new List<Pos>();
public void Initialize(int posY, int posX, Board board)
{
PosX = posX;
PosY = posY;
_board = board;
int[] frontY = new int[] { -1, 0, 1, 0 };
int[] frontX = new int[] { 0, -1, 0, 1 };
int[] rightY = new int[] { 0, -1, 0, 1 };
int[] rightX = new int[] { 1, 0, -1, 0 };
_points.Add(new Pos(PosY, PosX));
while (PosY != board.DestY || PosX != board.DestX)
{
if (_board.Tile[PosY + rightY[_dir], PosX + rightX[_dir]] == Board.TileType.Empty)
{
_dir = (_dir - 1 + 4) % 4;
PosY = PosY + frontY[_dir];
PosX = PosX + frontX[_dir];
_points.Add(new Pos(PosY, PosX));
}
else if (_board.Tile[PosY + frontY[_dir], PosX + frontX[_dir]] == Board.TileType.Empty)
{
PosY = PosY + frontY[_dir];
PosX = PosX + frontX[_dir];
_points.Add(new Pos(PosY, PosX));
}
else
{
_dir = (_dir + 1 + 4) % 4;
}
}
}
const int MOVE_TICK = 10;
int _sumTick = 0;
int _lastIndex = 0;
public void Update(int deltaTick)
{
if (_lastIndex >= _points.Count)
return;
_sumTick += deltaTick;
if (_sumTick >= MOVE_TICK)
{
_sumTick = 0;
PosY = _points[_lastIndex].Y;
PosX = _points[_lastIndex].X;
_lastIndex++;
}
}
}
}
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);
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();
}
}
}
}
1. 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 및 Player 객체를 생성하고 초기화합니다.
while (true): 무한 루프를 통해 일정한 프레임 속도로 게임을 실행합니다.
player.Update(deltaTick): 플레이어 이동을 갱신합니다.
board.Render(): 미로와 플레이어를 화면에 출력합니다.
2. Player 클래스 (플레이어 이동 구현)
class Player
{
public int PosY { get; private set; }
public int PosX { get; private set; }
Board _board;
enum Dir { Up = 0, Left = 1, Down = 2, Right = 3 }
int _dir = (int)Dir.Up;
List<Pos> _points = new List<Pos>();
public void Initialize(int posY, int posX, Board board)
{
PosX = posX;
PosY = posY;
_board = board;
int[] frontY = { -1, 0, 1, 0 };
int[] frontX = { 0, -1, 0, 1 };
int[] rightY = { 0, -1, 0, 1 };
int[] rightX = { 1, 0, -1, 0 };
_points.Add(new Pos(PosY, PosX));
while (PosY != board.DestY || PosX != board.DestX)
{
if (_board.Tile[PosY + rightY[_dir], PosX + rightX[_dir]] == Board.TileType.Empty)
{
_dir = (_dir - 1 + 4) % 4;
PosY += frontY[_dir];
PosX += frontX[_dir];
_points.Add(new Pos(PosY, PosX));
}
else if (_board.Tile[PosY + frontY[_dir], PosX + frontX[_dir]] == Board.TileType.Empty)
{
PosY += frontY[_dir];
PosX += frontX[_dir];
_points.Add(new Pos(PosY, PosX));
}
else
{
_dir = (_dir + 1 + 4) % 4;
}
}
}
const int MOVE_TICK = 10;
int _sumTick = 0;
int _lastIndex = 0;
public void Update(int deltaTick)
{
if (_lastIndex >= _points.Count) return;
_sumTick += deltaTick;
if (_sumTick >= MOVE_TICK)
{
_sumTick = 0;
PosY = _points[_lastIndex].Y;
PosX = _points[_lastIndex].X;
_lastIndex++;
}
}
}
설명
- 오른손 법칙을 적용하여 미리 경로를 계산합니다.
_points 리스트에 경로를 저장하고, Update()에서 해당 경로를 따라 이동합니다.
Initialize()에서 미로 탐색 경로를 미리 계산하여 성능을 최적화합니다.
- 프레임 단위 이동을 위해
MOVE_TICK 값을 사용합니다.
3. Board 클래스 (미로 생성 및 출력)
class Board
{
const char CIRCLE = '\u25cf';
public TileType[,] Tile { get; private set; }
public int Size { get; private set; }
public int DestY { get; private set; }
public int DestX { 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;
DestY = Size - 2;
DestX = Size - 2;
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 if (y == DestY && x == DestX)
Console.ForegroundColor = ConsoleColor.Yellow;
else
Console.ForegroundColor = GetTileColor(Tile[y, x]);
Console.Write(CIRCLE);
}
Console.WriteLine();
}
Console.ForegroundColor = prevColor;
}
}
설명
GenerateBySideWinder(): Sidewinder 알고리즘을 사용해 미로를 생성합니다.
Render(): 미로와 플레이어의 현재 위치를 출력합니다.
DestY, DestX: 목적지 좌표를 저장하여 플레이어가 목표 지점을 인식할 수 있도록 합니다.
프레임 관리 및 최적화
const int MOVE_TICK = 10;
int _sumTick = 0;
int _lastIndex = 0;
public void Update(int deltaTick)
{
if (_lastIndex >= _points.Count) return;
_sumTick += deltaTick;
if (_sumTick >= MOVE_TICK)
{
_sumTick = 0;
PosY = _points[_lastIndex].Y;
PosX = _points[_lastIndex].X;
_lastIndex++;
}
}
설명
- 프레임마다 이동이 일어나지 않도록 일정한 속도로 이동합니다.
- 미리 계산된 경로를 따라가므로 실시간 탐색보다 성능이 우수합니다.