본문 바로가기
프로그래밍 언어/알고리즘 및 디자인패턴

플레이어 이동

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

Player

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;

                //여기에다가 0.1초마다 실행될 로직을 넣어준다.
                int randValue = _random.Next(0, 5);
                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)
                            PosY = PosY - 1;
                        break;
                    case 3://우
                        if (PosX + 1 <= _board.Size && _board.Tile[PosY, PosX + 1] == Board.TileType.Empty)
                            PosY = PosY + 1;
                        break;

                }
            }
        }
    }
}

Board

using System;


namespace Algorithm
{
    
    class Board
    {
        public TileType[,] Tile { get; private set; }
        public int Size { get; private set; }
        const char CIRCLE = '\u25cf';
        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;

            //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();
            GenerateBySideWinder();
            
        }
        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
                        Console.ForegroundColor = GetTileColor(Tile[y, x]);
                    Console.Write(CIRCLE);
                }
                Console.WriteLine();
            }
            Console.ForegroundColor = prevColor;
        }
        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()
        {
            //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;
                    }
                }
            }
        }
        ConsoleColor GetTileColor(TileType type)
        {
            switch (type)
            {
                case TileType.Empty:
                    return ConsoleColor.Green;
                case TileType.Wall:
                    return ConsoleColor.Red;
                default:
                    return ConsoleColor.Green;
            }
        }
    }
}

Program

using System;
using System.Collections;
using System.Reflection;

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.Size - 2, board.Size - 2, board);

            Console.CursorVisible = false;

            const int WAIT_TICK = 1000 / 30;


            int lastTick = 0;
            while (true)
            {
                //FPS 프레임 (60프레임 OK 30 프레임 이하는 X)
                #region 프레임 관리
                int currentTick = System.Environment.TickCount;
                //만약에 경과한 시간이 1/30초보다 작다면 
                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.14
SideWinder 미로 생성 알고리즘  (0) 2023.07.13
Binary Tree  (0) 2023.07.13
맵 만들기  (0) 2023.07.13