본문 바로가기
KDT/C# 프로그래밍

23/07/25 다차원 배열2

by 잰쟁 2023. 7. 25.
728x90
using Starcraft;
using Starcraft;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    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();

        }
        void MoveLeft()
        {
            int nextCol = this.colIdx - 1;

            Console.WriteLine("nextCol ===> {0}", nextCol);

            if (nextCol < 0)
            {
                Console.WriteLine("갈 수 없는 곳입니다.");
                return; //메서드를 즉시 종료
            }

            playerMap[this.rowIdx,this.colIdx - 1] = 100;
            playerMap[this.rowIdx,this.colIdx] = 0;
            this.colIdx -= 1;
            Console.WriteLine("왼쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
        }


        void MoveRight(int row, int col)
        {
            playerMap[row +1,col] = 100;
            playerMap[row, col] = 0;
        }

        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();
            }
        }
    }
}

수정필요