C#/수업 내용

[C#] 2021-09-01 2차원 배열 캐릭터 이동

JSH1 2021. 9. 1. 16:03
class App
{
    public int[,] array =
        {
            { 100,300,301,302 },
            { 100,100,100,100 },
            { 200,200,200,200 }
        };

    private Hero hero;

    public App()
    {
        hero = new Hero("홍길동");
        PrintMap();

        while (true)
        {
            ConsoleKeyInfo info = Console.ReadKey();
            
            switch(info.Key.ToString())
            {
                case "RightArrow":
                    MoveCheck(0, 1);
                    break;

                case "LeftArrow":
                    MoveCheck(0, -1);
                    break;

                case "DownArrow":
                    MoveCheck(1, 0);
                    break;

                case "UpArrow":
                    MoveCheck(-1, 0);
                    break;
            }
            PrintMap();
        }
    }

    void MoveCheck(int y, int x)
    {
        switch (y)
        {
            case 1: // DownArrow
                if (this.hero.position.y + y < array.GetLength(0))
                {
                    this.hero.MoveDown(array[hero.position.y + y, hero.position.x]);
                }
                else
                {
                    Console.WriteLine("아래쪽으로 이동할 수 없습니다.");
                }
                break;

            case -1: // UpArrow
                if (this.hero.position.y > 0)
                {
                    this.hero.MoveUp(array[hero.position.y + y, hero.position.x]);
                }
                else
                {
                    Console.WriteLine("위쪽으로 이동할 수 없습니다.");
                }
                break;
        }

        switch (x)
        {
            case 1: // RightArrow
                if (this.hero.position.x + x < array.GetLength(1))
                {
                    this.hero.MoveRight(array[hero.position.y, hero.position.x + x]);
                }
                else
                {
                    Console.WriteLine("오른쪽으로 이동할 수 없습니다.");
                }
                break;

            case -1: // LeftArrow
                if (this.hero.position.x > 0)
                {
                    this.hero.MoveLeft(array[hero.position.y, hero.position.x + x]);
                }
                else
                {
                    Console.WriteLine("왼쪽으로 이동할 수 없습니다.");
                }
                break;
        }
    }

    void PrintMap()
    {
        int totalRow = array.GetLength(0);
        int totalCol = array.GetLength(1);

        for (int row = 0; row < totalRow; row++)
        {
            for (int col = 0; col < totalCol; col++)
            {
                Console.Write("arr[{0},{1}] = {2}", row, col, array[row, col]);

                if (this.hero.position.y == row && this.hero.position.x == col)
                {
                    Console.Write("★\t");
                }

                else
                {
                    Console.Write("  \t");
                }

            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }
}
class Hero
{
    public Vector2 position;
    string name;

    public Hero(string name)
    {
        this.name = name;
        Console.WriteLine("{0}이 생성되었습니다.", this.name);
        this.PrintPosition();
    }

    public void MoveUp(int position)
    {
        if (position >= 300)
        {
            Console.WriteLine("위쪽으로 이동할 수 없습니다.");
        }

        else
        {
            this.position.y -= 1;
            this.PrintPosition();
            Console.WriteLine("위쪽으로 한 칸 이동합니다.");
        }
    }

    public void MoveDown(int position)
    {
        if (position >= 300)
        {
            Console.WriteLine("아래쪽으로 이동할 수 없습니다.");
        }

        else
        {
            this.position.y += 1;
            this.PrintPosition();
            Console.WriteLine("아래쪽으로 한 칸 이동합니다.");
        }
    }

    public void MoveLeft(int position)
    {
        if (position >= 300)
        {
            Console.WriteLine("왼쪽으로 이동할 수 없습니다.");
        }

        else
        {
            this.position.x -= 1;
            this.PrintPosition();
            Console.WriteLine("왼쪽으로 한 칸 이동합니다.");
        }
    }

    public void MoveRight(int position)
    {
        if (position >= 300)
        {
            Console.WriteLine("오른쪽으로 이동할 수 없습니다.");
        }

        else
        {
            this.position.x += 1;
            this.PrintPosition();
            Console.WriteLine("오른쪽으로 한 칸 이동합니다.");
        }
    }

    void PrintPosition()
    {
        Console.WriteLine("({0},{1})", this.position.y, this.position.x);
    }
}
struct Vector2
{
    public int x;
    public int y;

    public Vector2(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

Hello World.exe
0.01MB