Board
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
class Board
{
public TileType[,] Tile { get; private set; }
public int Size { get; private set; }
const char CIRCLE = '\u25cf';
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;
//Mazes for Programmers
//랜덤으로 우측 혹은 아래로 길을 뚫는 작업
//GenerateByBinaryTree();
GenerateBySideWinder();
}
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;
}
}
}
}
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;
}
}
//Binary Tree Algorithm
Random rand = new Random();
for (int y = 0; y < Size; y++)
{
int count = 1;
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;
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++)
{
//플레이어 좌표를 갖고 와서, 그 좌표랑 현재 y, 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;
}
ConsoleColor GetTileColor(TileType type)
{
switch (type)
{
case TileType.Empty:
return ConsoleColor.Green;
case TileType.Wall:
return ConsoleColor.Red;
default:
return ConsoleColor.Green;
}
}
}
}
Player
using System;
using System.Collections.Generic;
using System.Text;
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)
{
PosY = posY;
PosX = posX;
_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)
{
// 1. 현재 바라보는 방향을 기준으로 오른쪽으로 갈 수 있는지 확인.
if (_board.Tile[PosY + rightY[_dir], PosX + rightX[_dir]] == Board.TileType.Empty)
{
// 오른쪽 방향으로 90도 회전
_dir = (_dir - 1 + 4) % 4;
// 앞으로 한 보 전진.
PosY = PosY + frontY[_dir];
PosX = PosX + frontX[_dir];
_points.Add(new Pos(PosY, PosX));
}
// 2. 현재 바라보는 방향을 기준으로 전진할 수 있는지 확인.
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
{
// 왼쪽 방향으로 90도 회전
_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++;
}
}
}
}
Program
using System;
namespace Algorithm
{
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 WAIT_TICK = 1000 / 30;
int lastTick = 0;
while (true)
{
#region 프레임 관리
// 만약에 경과한 시간이 1/30초보다 작다면
int currentTick = System.Environment.TickCount;
if (currentTick - lastTick < WAIT_TICK)
continue;
int deltaTick = currentTick - lastTick;
lastTick = currentTick;
#endregion
// 입력
// 로직
player.Update(deltaTick);
// 렌더링
Console.SetCursorPosition(0, 0);
board.Render();
}
}
}
}
'프로그래밍 언어 > 알고리즘 및 디자인패턴' 카테고리의 다른 글
그래프 생성 (0) | 2023.07.16 |
---|---|
그래프 이론 (0) | 2023.07.16 |
플레이어 이동 (0) | 2023.07.13 |
SideWinder 미로 생성 알고리즘 (0) | 2023.07.13 |
Binary Tree (0) | 2023.07.13 |