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

23/07/25 배열 복습 및 연습(집에서)

by 잰쟁 2023. 7. 25.
728x90

※헷갈렸던 부분※

1. 배열 선언, 배열의 요소에 데이터 담기,배열 초기화

2. 2차원 배열 전체적인 과정!!

 

 

1. 배열 기초 지식 연습

 

// 정수 배열을 선언 후 배열 요소에 데이터 담기 (배열 용량: 3)

 

▶배열 선언

 int[ ] arr = new int[3];

 

▶배열 요소에 데이터 담기

('[ ]'안에 있는 것은 인덱스)

arr[0] = 1;

arr[1] = 2;

arr[2] = 3;

------------------------------------------

▶배열 초기화 (방법: 3개)

방법 1)

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

방법 2)

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

방법 3)

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

 

 

2. 2차원 배열 전체적인 과정 연습

 

(원하는 결과 1)

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

namespace Starcraft
{
    internal class App
    {
        //생성자 메서드
        public App()
        {
            // map 배열 초기화 (배열 생성 및 원소에 값 부여)
            int[,] map =
            {
                {1,1,1 },
                {1,1,2 }
            };

            // playerMap 배열 생성
            // playerMap 배열 원소에 기본값 이미 부여됨.(null)
            int[,] playerMap = new int[map.GetLength(0), map.GetLength(1)];

            //map 과 playerMap 배열 출력
            this.PrintMap(map);
            this.PrintMap(playerMap);
            
        }

        // 배열 출력 메서드
        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();
            }
        }
    }
}

 

(원하는 결과 2 : 캐릭터ID 생성 및 위치시키기)

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

namespace Starcraft
{
    internal class App
    {
        //생성자 메서드
        public App()
        {
            // map 배열 초기화 (배열 생성 및 원소에 값 부여)
            int[,] map =
            {
                {1,1,1 },
                {1,1,2 }
            };

            // playerMap 배열 생성
            // playerMap 배열 원소에 기본값 이미 부여됨.(null)
            int[,] playerMap = new int[map.GetLength(0), map.GetLength(1)];

            //map 과 playerMap 배열 출력
            this.PrintMap(map);
            this.PrintSpace();  //<== 줄 띄기(구분)
            this.PrintMap(playerMap);
            //초기 위치 설정========================================

            playerMap[1, 2] = 100; //<== 캐릭터ID 생성 및 위치시키기
            this.PrintSpace();
            this.PrintMap(playerMap); //<== 캐릭터 위치 후 출력
        }

        // 배열 출력 메서드
        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();
            }
        }

        // 줄 띄기 메서드
        void PrintSpace()
        {
            Console.WriteLine();
        }
    }
}

 

(원하는 결과3: 캐릭터 왼쪽으로 이동 추가)

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

namespace Starcraft
{
    internal class App
    {
        //맴버 변수(필드)
        //MoveLeft 메서드에서 활용하기 위해 스택 메모리에 저장
        int[,] playerMap;
        int rowIdx;
        int colIdx;

        //생성자 메서드
        public App()
        {
            // map 배열 초기화 (배열 생성 및 원소에 값 부여)
            int[,] map =
            {
                {1,1,1 },
                {1,1,2 }
            };

            // playerMap 배열 생성(기본값: 0)
            this.playerMap = new int[map.GetLength(0), map.GetLength(1)];

            //map 과 playerMap의 초기 위치 설정 출력
            this.PrintMap(map);
            this.PrintSpace();  //<== 줄 띄기(구분)
            this.PrintMap(playerMap);           
            //-------------------------------------------------
            //캐릭터 초기 위치 설정
            this.rowIdx = 1;
            this.colIdx = 2;
            this.playerMap[rowIdx, colIdx] = 100;
            //-------------------------------------------------
            this.PrintSpace();
            this.PrintMap(playerMap);
            this.MoveLeft();
            this.PrintSpace();
            this.PrintMap(playerMap);

        }

        // 배열 출력 메서드
        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();
            }
        }

        // 줄 띄기 메서드
        void PrintSpace()
        {
            Console.WriteLine();
        }

        //왼쪽으로 이동 메서드
        void MoveLeft()
        {
            playerMap[this.rowIdx, this.colIdx - 1] = 100;
            playerMap[this.rowIdx, this.colIdx] = 0;
            this.colIdx -= 1; //이동하였으므로 열값 -1
            Console.WriteLine("왼쪽으로 이동하였습니다.[{0},{1}] : {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx,this.colIdx]);
        }
    }
}

 

(원하는 결과4: 캐릭터가 왼쪽으로 범위를 벗어나 이동하려고 할때 정지시키기)

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

namespace Starcraft
{
    internal class App
    {
        //맴버 변수(필드)
        //MoveLeft 메서드에서 활용하기 위해 스택 메모리에 저장
        int[,] playerMap;
        int rowIdx;
        int colIdx;

        //생성자 메서드
        public App()
        {
            // map 배열 초기화 (배열 생성 및 원소에 값 부여)
            int[,] map =
            {
                {1,1,1 },
                {1,1,2 }
            };

            // playerMap 배열 생성(기본값: 0)
            this.playerMap = new int[map.GetLength(0), map.GetLength(1)];

            //map 과 playerMap의 초기 위치 설정 출력
            this.PrintMap(map);
            this.PrintSpace();  //<== 줄 띄기(구분)
            this.PrintMap(playerMap);           
            //-------------------------------------------------
            //캐릭터 초기 위치 설정
            this.rowIdx = 1;
            this.colIdx = 2;
            this.playerMap[rowIdx, colIdx] = 100;
            //-------------------------------------------------
            this.PrintSpace();
            this.PrintMap(playerMap);
            this.MoveLeft();
            this.PrintSpace();
            this.PrintMap(playerMap);

            this.MoveLeft();
            this.PrintSpace();
            this.PrintMap(playerMap);

            this.MoveLeft();
            this.PrintSpace();
            this.PrintMap(playerMap);

        }

        // 배열 출력 메서드
        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();
            }
        }

        // 줄 띄기 메서드
        void PrintSpace()
        {
            Console.WriteLine();
        }

        //왼쪽으로 이동 메서드 (+ 범위 이탈시 정지시키기)
        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; //이동하였으므로 열값 -1
            Console.WriteLine("왼쪽으로 이동하였습니다.[{0},{1}] : {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx,this.colIdx]);
        }
    }
}

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

23/07/26 가짜 인벤토리 만들기2  (0) 2023.07.26
23/07/26 가짜 인벤토리 만들기1  (0) 2023.07.26
23/07/25 다차원 배열 기초연습  (0) 2023.07.25
23/07/25 다차원 배열2  (0) 2023.07.25
23/07/25 다차원배열  (0) 2023.07.25