K-digital traning/C#프로그래밍

2차원 배열 연습

내꺼블로그 2023. 7. 25. 15:47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class App
    {
        int[,] playerMap;
        int rowIdx;
        int colIdx;
        public App() 
        {
            int[,] map = { 
                { 1, 1, 1 },
                { 1, 1, 2 } 
            };

            this.playerMap  = new int[map.GetLength(0), map.GetLength(1)];

            this.PrintMap(map);
            this.PrintSpace();
            this.PrintMap(playerMap);
            this.rowIdx = 1;
            this.colIdx = 2;
            playerMap[this.rowIdx, this.colIdx] = 100;
            this.PrintSpace();
            this.PrintMap(playerMap);
            this.MoveLeft();
            PrintSpace();
            this.PrintMap(playerMap);

        }

        void MoveLeft()
        {
            int nextCol = this.colIdx - 1;
            if (nextCol < 0)
                Console.WriteLine("더이상 이동할 수 없습니다.");
            else
            {
                playerMap[this.rowIdx, nextCol] = 100;
                playerMap[this.rowIdx, this.colIdx] = 0;
                this.colIdx = nextCol;
                Console.WriteLine("왼쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
            }
        }

        void MoveRight()
        {
            int nextCol = this.colIdx + 1;
            if (nextCol == playerMap.GetLength(1))
                Console.WriteLine("더이상 이동할 수 없습니다.");
            else
            {
                playerMap[this.rowIdx, nextCol] = 100;
                playerMap[this.rowIdx, this.colIdx] = 0;
                this.colIdx = nextCol;
                Console.WriteLine("오른쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
            }
        }

        void MoveUp()
        {
            int nextRow = this.rowIdx - 1;
            if (nextRow < 0)
                Console.WriteLine("더이상 이동할 수 없습니다.");
            else
            {
                playerMap[nextRow, this.colIdx] = 100;
                playerMap[this.rowIdx, this.colIdx] = 0;
                this.rowIdx= nextRow;
                Console.WriteLine("위쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
            }
        }

        void MoveDown()
        {
            int nextRow=this.rowIdx + 1;
            if (nextRow == playerMap.GetLength(0))
                Console.WriteLine("더이상 이동할 수 없습니다.");
            else
            {
                playerMap[nextRow, this.colIdx] = 100;
                playerMap[this.rowIdx, this.colIdx] = 0;
                this.rowIdx= nextRow;
                Console.WriteLine("아래쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
            }
        }
        void PrintSpace()
        {
            Console.WriteLine();
        }
        void PrintMap(int[,] arr)
        {
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    int element = arr[i, j];
                    Console.Write("[{0},{1}] : {2}\t", i, j, element);
                }
                Console.WriteLine();
            }
        }
      
    }
}