C#

2차원 배열

송현호 2023. 7. 25. 18:22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace learndotnet
{
    internal class App
    {
        int[,] playerMap = new int[3,3];
        public App()
        {
            Hero hero = new Hero("홍길동");
            hero.SetUpPosition(playerMap,new Vector2(2,1));
            hero.PrintMap();
            hero.MoveLeft();
            hero.MoveLeft();
            hero.MoveLeft();
            Console.WriteLine();
            hero.PrintMap();
            hero.MoveRight();
            hero.MoveRight();
            hero.MoveRight();
            hero.MoveRight();
            Console.WriteLine();
            hero.PrintMap();
            hero.MoveUp();
            hero.MoveUp();
            hero.MoveUp();
            hero.MoveUp();
            Console.WriteLine();
            hero.PrintMap();
            hero.MoveDown();
            hero.MoveDown();
            hero.MoveDown();
            hero.MoveDown();
            Console.WriteLine();
            hero.PrintMap();

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

namespace learndotnet
{
    internal class Hero
    {
        string name;
        int id = 100;
        Vector2 position;
        int[,] playerMap;
        public Hero(string name)
        {
            this.name = name;
        }

        public void SetUpPosition(int[,] playerMap,Vector2 indices)
        {
            this.position = Utils.Convert2Position(indices);
            this.playerMap = playerMap;
            this.playerMap[this.position.x, this.position.y] = this.id; 
        }

        public void PrintMap()
        {
            for (int i = 0; i < playerMap.GetLength(0); i++)
            {
                for (int j = 0; j < playerMap.GetLength(1); j++)
                {
                    Console.Write("({0},{1}): {2}     ", j, i, this.playerMap[j, i]);
                }
                Console.WriteLine();
            }
        }

        public void MoveLeft()
        {
            for(int i = 0; i < this.playerMap.GetLength(0); i++)
            {
                for(int j = 0; j < this.playerMap.GetLength(1); j++)
                {
                    if (this.playerMap[i,j] == this.id)
                    {
                        if (this.position.x > 0)
                        {
                            this.position.x -= 1;
                            this.playerMap[i-1, j] = this.id;
                            this.playerMap[i, j] = 0;
                            break;
                        }
                        else
                        {

                        }
                    }
                }
            }
        }

        public void MoveRight()
        {
            for (int i = 0; i < this.playerMap.GetLength(0); i++)
            {
                for (int j = 0; j < this.playerMap.GetLength(1); j++)
                {
                    if (this.playerMap[i, j] == this.id)
                    {
                        if (this.position.x < this.playerMap.GetLength(0)-1)
                        {
                            this.position.x += 1;
                            this.playerMap[i + 1, j] = this.id;
                            this.playerMap[i, j] = 0;
                            break;
                        }
                        else
                        {

                        }
                    }
                }
            }
        }

        public void MoveUp()
        {
            for (int i = 0; i < this.playerMap.GetLength(0); i++)
            {
                for (int j = 0; j < this.playerMap.GetLength(1); j++)
                {
                    if (this.playerMap[i, j] == this.id)
                    {
                        if (this.position.y > 0)
                        {
                            this.position.y -= 1;
                            this.playerMap[i, j - 1] = this.id;
                            this.playerMap[i, j] = 0;
                            break;
                        }
                        else
                        {

                        }
                    }
                }
            }
        }

        public void MoveDown()
        {
            for (int i = 0; i < this.playerMap.GetLength(0); i++)
            {
                for (int j = 0; j < this.playerMap.GetLength(1); j++)
                {
                    if (this.playerMap[i, j] == this.id)
                    {
                        if (this.position.y < this.playerMap.GetLength(1)-1)
                        {
                            this.position.y += 1;
                            this.playerMap[i, j + 1] = this.id;
                            this.playerMap[i, j] = 0;
                            break;
                        }
                        else
                        {

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

namespace learndotnet
{
    internal class Utils
    {
        public static Vector2 Convert2Indices(Vector2 position)
        {
            return new Vector2(position.y, position.x);
        }

        public static Vector2 Convert2Position(Vector2 indices)
        {
            return new Vector2(indices.y, indices.x);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace learndotnet
{
    //구조체 : 값형식 -> 스택
    //기본생성자 못씀
    //상속불가, 기본클래스로 못씀
    //인터페이스 사용가능
    internal struct Vector2
    {
        public int x;
        public int y;
        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
}

 

'C#' 카테고리의 다른 글

인벤토리2  (0) 2023.07.26
몬스터 아이템 드랍  (0) 2023.07.26
2048  (0) 2023.07.25
인벤토리1  (0) 2023.07.24
마린 메딕  (0) 2023.07.24