본문 바로가기
C#/수업내용

[C# 7일차] 배열을 이용한 맵 이동

by 왹져박사 2023. 1. 9.
728x90

App Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study07
{
    class App
    {
        int[,] arrHero = new int[2, 3];

        int[,] arr =
{
                {1, 3, 1 },
                {1, 1, 2 }
            };

        int rowIndex;
        int colIndex;

        //생성자
        public App()
        {
            Console.WriteLine("App 생성자");
            Console.WriteLine();



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

            rowIndex = 1;
            colIndex = 2;
            arrHero[rowIndex, colIndex] = 100;

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



            Console.WriteLine();

            Console.WriteLine("종료는 Enter");
            Console.WriteLine();

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


                if (cki.Key == ConsoleKey.LeftArrow)
                {
                    MoveLeft();
                    PrintPlayer();
                }
                else if (cki.Key == ConsoleKey.RightArrow)
                {
                    MoveRight();
                    PrintPlayer();
                }
                else if (cki.Key == ConsoleKey.UpArrow)
                {
                    MoveUp();
                    PrintPlayer();
                }

                else if (cki.Key == ConsoleKey.DownArrow)
                {
                    MoveDown();
                    PrintPlayer();
                }
                else if (cki.Key == ConsoleKey.Enter)
                    break;
            }


        }

        void MoveLeft()
        {
            int to = colIndex-1;
            if (to < 0 || arr[rowIndex, to] == 3) 
            {
                Console.WriteLine("왼쪽으로 이동 불가");
                Console.WriteLine();
            }
            else
            {
                arrHero[rowIndex, to] = arrHero[rowIndex, colIndex];
                arrHero[rowIndex, colIndex] = 0;
                Console.WriteLine("왼쪽으로 이동");
                colIndex = to;
            }
        }
        void MoveRight()
        {
            int to = colIndex + 1;
            if (to > arrHero.GetLength(1)-1 || arr[rowIndex, to] == 3)
            {
                Console.WriteLine("오른쪽으로 이동 불가");
                Console.WriteLine();
            }
            else
            {
                arrHero[rowIndex, to] = arrHero[rowIndex, colIndex];
                arrHero[rowIndex, colIndex] = 0;
                Console.WriteLine("오른쪽으로 이동");
                colIndex = to;
            }

        }
        void MoveUp()
        {
            int to = rowIndex - 1;
            if (to < 0 || arr[to, colIndex] == 3)
            {
                Console.WriteLine("위쪽으로 이동 불가");
                Console.WriteLine();
            }
            else
            {
                arrHero[to, colIndex] = arrHero[rowIndex, colIndex];
                arrHero[rowIndex, colIndex] = 0;
                Console.WriteLine("위쪽으로 이동");
                rowIndex = to;
            }

        }
        void MoveDown()
        {
            int to = rowIndex + 1;
            if (to > arrHero.GetLength(0)-1  || arr[to, colIndex] == 3) 
            {
                Console.WriteLine("아래쪽으로 이동 불가");
                Console.WriteLine();
            }
            else
            {
                arrHero[to, colIndex] = arrHero[rowIndex, colIndex];
                arrHero[rowIndex, colIndex] = 0;
                Console.WriteLine("아래쪽으로 이동");
                rowIndex = to;
            }

        }

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

        }

    }

 

실행 결과

728x90