스토리지

[3.18] 2차원 배열 연습 5 - 2차원 배열을 이용한 좌표 이동 본문

Unity/수업내용(C#)

[3.18] 2차원 배열 연습 5 - 2차원 배열을 이용한 좌표 이동

ljw4104 2021. 3. 18. 17:48
using System;

namespace Study07
{
    public class App
    { 
        public App()
        {
            TilemapManager map = new TilemapManager();
            Hero hero1 = new Hero();
            Hero hero2 = new Hero();

            Console.WriteLine("Hero1이(가) (0.0)에서 (2.4)로 이동합니다.\n");
            hero1.Move(2, 4, map);
            Console.WriteLine();
            map.PrintMap();

            Console.WriteLine("\nHero2이(가) (0.0)에서 (3.3)로 이동합니다.\n");
            hero2.Move(3, 3, map);
            Console.WriteLine();
            map.PrintMap();
        }
    }
}
using System;

namespace Study07
{
    public class TilemapManager
    {
        private static int[,] map = new int[6, 6];
        public TilemapManager()
        {
            //100 : Default Map Tile
            //200 : Hero
            map = new int[,]
            {
                {100, 100, 100, 100, 100, 100 },
                {100, 100, 100, 100, 100, 100 },
                {100, 100, 100, 100, 100, 100 },
                {100, 100, 100, 100, 100, 100 },
                {100, 100, 100, 100, 100, 100 },
                {100, 100, 100, 100, 100, 100 }
            };
        }

        public bool isClear(int y, int x)
        {
            bool clear = default;
            if(map[x,y] == 100)
            {
                clear = true;
            }
            return clear;
        }

        public static void DisposeHero(int x, int y)
        {
            map[x, y] = 200;
        }

        public void PrintMap()
        {
            for(int i = 0; i < map.GetLength(0); i++)
            {
                for(int j = 0; j < map.GetLength(1); j++)
                {
                    Console.Write("{0} ", map[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
}
using System;

namespace Study07
{
    public class Hero
    {
        private int xPos;
        private int yPos;
        public int XPos
        {
            private set
            {
                this.xPos = value;
            }
            get
            {
                return this.xPos;
            }
        }

        public int YPos
        {
            private set
            {
                this.yPos = value;
            }
            get
            {
                return this.yPos;
            }
        }

        public Hero()
        {
            Console.WriteLine("Hero가 태어났다.");
            xPos = 0;
            yPos = 0;
        }

        public void Move(int x, int y, TilemapManager map)
        {
            int originalXPos = this.xPos;
            int originalYPos = this.yPos;
            //이동하려는 칸이 비어있으면
            if (map.isClear(x, y))
            {

                while (this.xPos != y)
                {
                    if (this.xPos > y)
                    {
                        this.xPos--;
                        //만약 map 밖으로 벗어나면
                        if (this.xPos <= 0)
                        {
                            this.xPos = 0;
                            break;
                        }
                    }
                    else
                    {
                        this.xPos++;
                        //만약 map 밖으로 벗어나면
                        if (this.xPos > 6)
                        {
                            this.xPos = 6;
                            break;
                        }
                    }
                    if (originalXPos < this.xPos)
                    {
                        Console.WriteLine("({0}.{1}) -> ({2}.{3})으로 이동합니다.", this.yPos, this.xPos - 1, this.yPos, this.xPos);
                    }
                    else
                    {
                        Console.WriteLine("({0}.{1}) -> ({2}.{3})으로 이동합니다.", this.yPos, this.xPos  + 1, this.yPos, this.xPos);
                    }
                }

                while (this.yPos != x)
                {
                    if (this.yPos > x)
                    {
                        this.yPos--;
                        if (this.yPos <= 0)
                        {
                            this.yPos = 0;
                            break;
                        }
                    }
                    else
                    {
                        this.yPos++;
                        if (this.yPos > 6)
                        {
                            this.yPos = 6;
                            break;
                        }
                    }
                    if (originalYPos < this.yPos)
                    {
                        Console.WriteLine("({0}.{1}) -> ({2}.{3})으로 이동합니다.", this.yPos - 1, this.xPos, this.yPos, this.xPos);
                    }
                    else
                    {
                        Console.WriteLine("({0}.{1}) -> ({2}.{3})으로 이동합니다.", this.yPos + 1, this.xPos, this.yPos, this.xPos);
                    }
                }


                TilemapManager.DisposeHero(originalXPos, originalYPos, this.xPos, this.yPos);
            }
            else
            {
                Console.WriteLine("그 좌표엔 이미 다른 것이 있어서 이동할 수 없음");
            }
        }
    }
}

hero1이 0.0 => 2.4 => 1.5로 이동하는 과정, hero2가 0.0 => 1.5로 이동하려고 시도

void DisposeHero(int ox, int oy, int x, int y)는 어차피 isClear(int x, int y) 함수에 의해 검증이 완료되었기 때문에 따로 객체를 통해서 호출되어지지 않아도 되는 static형식으로 선언했다.

대충 이런 알고리즘으로 돌아간다.

Comments