C#/수업 내용

2차원 배열을 활용하여 방향키로 플레이어 이동시키기

HSH12345 2023. 1. 9. 15:44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study07
{
    class App
    {
        int[,] arr0;
        int[,] arr1;
        int grass = 1;
        int desert = 2;
        int wall = 100;
        int playerId = 100;
        int colIndex;
        int rowIndex;
        ConsoleKeyInfo cki;

        public App()
        {
            arr0 = new int[2, 3];
            for(int i = 0; i < arr0.GetLength(0); i++)
            {
                for (int j = 0; j < arr0.GetLength(1); j++)
                {
                    arr0[i, j] = grass;                       
                }
            }
            arr0[1, 2] = desert;
            arr0[0, 1] = wall;

            for (int i = 0; i < arr0.GetLength(0); i++)
            {
                for (int j = 0; j < arr0.GetLength(1); j++)
                {
                    Console.Write(arr0[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();

            arr1 = new int[2, 3];            
            colIndex = 2;
            rowIndex = 1;
            arr1[rowIndex, colIndex] = playerId;

            InputKey();

        }

        void MoveLeft()
        {
            if (colIndex == 0)
            {
                Console.WriteLine("움직일 수 없습니다.");
            }
            else
            {
                colIndex--;
                arr1[rowIndex, colIndex] = arr1[rowIndex, colIndex + 1];
                arr1[rowIndex, colIndex + 1] = 0;
                Console.WriteLine("왼쪽으로 움직였습니다.");

                if (arr1[0, 1] == wall)
                {
                    colIndex++;
                    arr1[rowIndex, colIndex] = arr1[rowIndex, colIndex - 1];
                    arr1[rowIndex, colIndex - 1] = 0;
                    Console.WriteLine("벽입니다.");
                }
            }             
        }

        void MoveRight()
        {
            if (colIndex == arr1.GetLength(1) - 1)
            {
                Console.WriteLine("움직일 수 없습니다.");
            }
            else
            {
                colIndex++;
                arr1[rowIndex, colIndex] = arr1[rowIndex, colIndex - 1];
                arr1[rowIndex, colIndex - 1] = 0;
                Console.WriteLine("오른쪽으로 움직였습니다.");
                if (arr1[0, 1] == wall)
                {
                    colIndex--;
                    arr1[rowIndex, colIndex] = arr1[rowIndex, colIndex + 1];
                    arr1[rowIndex, colIndex + 1] = 0;
                    Console.WriteLine("벽입니다.");
                }
            }
        }

        void MoveUp()
        {
            if (rowIndex == 0)
            {
                Console.WriteLine("움직일 수 없습니다.");
            }
            else
            {
                rowIndex--;
                arr1[rowIndex, colIndex] = arr1[rowIndex + 1, colIndex];
                arr1[rowIndex + 1, colIndex] = 0;
                Console.WriteLine("위쪽으로 움직였습니다.");
                if (arr1[0, 1] == wall)
                {
                    rowIndex++;
                    arr1[rowIndex, colIndex] = arr1[rowIndex - 1, colIndex];
                    arr1[rowIndex - 1, colIndex] = 0;
                    Console.WriteLine("벽입니다.");
                }
            }
        }

        void MoveDown()
        {
            if (rowIndex == arr1.GetLength(0) - 1)
            {
                Console.WriteLine("움직일 수 없습니다.");
            }
            else
            {
                rowIndex++;
                arr1[rowIndex, colIndex] = arr1[rowIndex - 1, colIndex];
                arr1[rowIndex - 1, colIndex] = 0;
                Console.WriteLine("아래쪽으로 움직였습니다.");
                if (arr1[0, 1] == wall)
                {
                    rowIndex--;
                    arr1[rowIndex, colIndex] = arr1[rowIndex + 1, colIndex];
                    arr1[rowIndex + 1, colIndex] = 0;
                    Console.WriteLine("벽입니다.");
                }
            }
        }

        void InputKey()
        {
            while (true)
            {
                cki = Console.ReadKey(false);

                switch (cki.Key)
                {
                    case ConsoleKey.LeftArrow:
                        MoveLeft();
                        PrintArr1();
                        Console.WriteLine();
                        break;

                    case ConsoleKey.RightArrow:
                        MoveRight();
                        PrintArr1();
                        Console.WriteLine();
                        break;

                    case ConsoleKey.UpArrow:
                        MoveUp();
                        PrintArr1();
                        Console.WriteLine();
                        break;

                    case ConsoleKey.DownArrow:
                        MoveDown();
                        PrintArr1();
                        Console.WriteLine();
                        break;
                }
            }

        }


        void PrintArr1()
        {
            for (int i = 0; i < arr1.GetLength(0); i++)
            {
                for (int j = 0; j < arr1.GetLength(1); j++)
                {
                    Console.Write(arr1[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}