1. App.cs

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

namespace Study05
{
    public class App
    {
        int[,] map;

        // 생성자
        public App()
        {
            this.CreateMap();
            Hero hero = new Hero(map);

            hero.MoveDown();
            this.DrawMap();
        }

        public void CreateMap()
        { 
            this.map = new int[,]
            {
                {0, 0, 0, 0},
                {0, 0, 0, 0},
                {-1, -1, 0, 0},
                {0, 0, 0, -1}
            };
        }


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

2. hero.cs

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

namespace Study05
{
    public class Hero
    {
        // id, 공격력
        public int heroId = 10;
        public int damage = 10;
        private int row = 0;
        private int col = 0;
        private int[,] map;

        // 생성자
        public Hero(int[,] map)
        {
            this.map = map;
            this.map[0, 0] = this.heroId;
        }

        public void Location(int row, int col)
        {
            this.row = row;
            this.col = col;
        }

        public void MoveRight()
        {
            int prevCol = this.col;
            int nextCol;

            if (prevCol >= 3)
            {
                Console.WriteLine("이동할 수 없습니다.");
            }
            else
            {
                prevCol = this.col;
                nextCol = this.col + 1;

                this.map[this.row, prevCol] = 0;
                this.map[this.row, nextCol] = this.heroId;

                this.col = nextCol;
            }
            
        }

        public void MoveLeft()
        {
            int prevCol = this.col;
            int nextCol;

            if (prevCol <= 0)
            {
                Console.WriteLine("이동할 수 없습니다.");
            }
            else
            {
                prevCol = this.col;
                nextCol = this.col - 1;

                this.map[this.row, prevCol] = 0;
                this.map[this.row, nextCol] = this.heroId;

                this.col = nextCol;
            }
        }

        public void MoveDown()
        {
            int prevRow = this.row;
            int nextRow;

            if (prevRow >= 3)
            {
                Console.WriteLine("이동할 수 없습니다.");
            }
            else
            {
                prevRow = this.row;
                nextRow = this.row + 1;

                this.map[prevRow, this.col] = 0;
                this.map[nextRow, this.col] = this.heroId;

                this.row = nextRow;
            }
        }

        public void MoveUp()
        {
            int prevRow = this.row;
            int nextRow;

            if (prevRow <= 0)
            {
                Console.WriteLine("이동할 수 없습니다.");
            }
            else
            {
                prevRow = this.row;
                nextRow = this.row - 1;

                this.map[prevRow, this.col] = 0;
                this.map[nextRow, this.col] = this.heroId;

                this.row = nextRow;
            }
        }

        // 실행 X
        public void Colide()
        {
            int row = this.row;
            int col = this.col;
            for (int i = 0; i < this.map.GetLength(0); i++)
            {
                for (int j = 0; j < this.map.GetLength(1); j++)
                {
                    if (this.map[i, j] == -1)
                    {
                        this.map[this.row, this.col] = this.heroId;
                        Console.WriteLine("이동할 수 없습니다.");
                    }
                }
            }
        }
    }
}

'C# > 수업 내용' 카테고리의 다른 글

Json 연습 - 같은 아이템의 개수(amount) 증가  (0) 2022.06.20
대리자 연습  (0) 2022.06.16
2048 왼쪽 이동  (0) 2022.06.15
인벤토리 예제  (0) 2022.06.14
22.06.14 - property, get-set메서드, interface  (0) 2022.06.14

+ Recent posts