c#/복습

(재연습)2차원 배열

yeeendy 2023. 1. 22. 23:04
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study0121
{
    class App
    {
        private int[,] arr2;
        private int colIndex = 0; //1차원 인덱스
        private int rowIndex = 0;   //2차원 인덱스
        //생성자
        public App()
        {
            int[,] arr = new int[2, 3]; //1차원의 길이 3, 2차원의 길이 2
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    arr[i, j] = 1;
                }
            }

            arr[1, 2] = 2;

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

            this.arr2 = new int[2, 3];

            this.rowIndex = 1;
            this.colIndex = 2;
            this.arr2[this.rowIndex, this.colIndex] = 100;

            this.MoveDown();
            this.PrintArr2();

            this.MoveDown();
            this.PrintArr2();
        }
        void MoveUp()
        {
            int to = rowIndex - 1;  //2차원 

            Console.WriteLine("{0}, {1}", to, this.arr2.GetLength(0) - 1);

            if (to < 0)
            {
                Console.WriteLine("막혀있습니다.");
            }
            else
            {
                this.arr2[to, this.colIndex] = this.arr2[this.rowIndex, this.colIndex];
                this.arr2[this.rowIndex, this.colIndex] = 0;
                this.rowIndex = to;
            }
        }

        void MoveDown()
        {
            int to = rowIndex + 1;  //2차원 

            if (to > this.arr2.GetLength(0) - 1)
            {
                Console.WriteLine("막혀있습니다.");
            }
            else
            {
                this.arr2[to, this.colIndex] = this.arr2[this.rowIndex, this.colIndex];
                this.arr2[this.rowIndex, this.colIndex] = 0;
                this.rowIndex = to;
            }
        }

        void MoveLeft()
        {
            int to = this.colIndex - 1;

            if (to < 0)
            {
                Console.WriteLine("막혀있습니다.");
            }
            else
            {
                this.arr2[1, to] = 100;
                this.arr2[1, this.colIndex] = 0;
                this.colIndex = to;
            }
        }

        void MoveRight()
        {
            int to = this.colIndex + 1;

            if (to < 0)
            {
                Console.WriteLine("막혀있습니다.");
            }
            else
            {
                this.arr2[1, to] = 100;
                this.arr2[1, this.colIndex] = 0;
                this.colIndex = to;
            }
        }

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

'c# > 복습' 카테고리의 다른 글

(재연습) 컬렉션<>  (0) 2023.01.24
(재연습)컬렉션  (0) 2023.01.24
(재연습) 배열  (0) 2023.01.22
(재연습) switch, while  (0) 2023.01.21
(재연습) 시즈탱크(탱크모드/공성모드)  (0) 2023.01.21