C#프로그래밍/복습

2차원 배열 복습

다모아 2023. 7. 26. 01:22

App 클래스

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

namespace Game2048
{
    internal class App
    {
        int[,] playerMap = new int[3, 3];

        //생성자
        public App()
        {
            Hero hero = new Hero(100, "홍길동");
            hero.init(playerMap, new Vector2(1, 2));
            hero.PrintPosition();
            hero.PrintPlayerMap();
        }
    }
}

 

Hero 클래스

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

namespace Game2048
{
    internal class Hero
    {
        private int[,] playerMap;
        private Vector2 position;
        private int id;
        private string name;

        //생성자
        public Hero(int id, string name)
        {
            this.id = id;
            this.name = name;
            Console.WriteLine("{0}", name);
        }

        //초기화
        public void init(int[,] playerMap, Vector2 position)
        {
            this.playerMap = playerMap;
            this.UpdatePosition(position);
            this.UpdatePlayerMap();
        }

        //위치출력
        public void PrintPosition()
        {
            Console.WriteLine("좌표: {0}", this.position.ToString());
        }

        //플레이어 맵 출력
        public void PrintPlayerMap()
        {
            for(int i = 0; i < this.playerMap.GetLength(0); i++)
            {
                for(int j = 0; j < this.playerMap.GetLength(1); j++)
                {
                    Console.Write("[{0},{1}] : {2}\t", i, j, playerMap[i,j]);
                }
                Console.WriteLine();
            }
        }

        //위치변경
        void UpdatePosition(Vector2 targetPosition)
        {
            this.position = targetPosition;
        }

        //위치변경시 playerMap 업데이트
        void UpdatePlayerMap()
        {
            Vector2 indices = Utils.ConvertPosition2Indices(this.position);
            this.playerMap[indices.x, indices.y] = this.id;
        }
    }
}

 

Utils 클래스

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

namespace Game2048
{
    public class Utils
    {
        public Utils()
        {

        }

        public static Vector2 ConvertPosition2Indices(Vector2 position)
        {
            //1, 2 -> 2, 1
            return new Vector2(position.y, position.x);
        }
        
        public static Vector2 ConvertIndices2Position(Vector2 Indices)
        {
            //2, 1 -> 1, 2
            return new Vector2(Indices.y, Indices.x);
        }
    }
}

 

Vector2 클래스

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

namespace Game2048
{
    public struct Vector2
    {
        public int x;
        public int y;

        //생성자
        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public override string ToString()
        {
            return $"({this.x},{this.y})";
        }
    }
}

 

 

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

JSON 복습2  (0) 2023.07.28
JSON 복습1  (0) 2023.07.28
2048 [완성]  (0) 2023.07.26
복습1  (0) 2023.07.23