아카이브

2차원 배열 본문

C#프로그래밍

2차원 배열

timbercat 2023. 7. 25. 15:52
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()
        {
            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 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();
            }
        }
    }
}

'C#프로그래밍' 카테고리의 다른 글

Dictionary 연습  (0) 2023.07.26
2차원 배열 연습  (0) 2023.07.25
배열연습 인벤토리  (0) 2023.07.25
배열연습3  (0) 2023.07.25
배열 연습2  (0) 2023.07.24