Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

행복한 개구리

C# 21.03.18.과제 본문

C#/수업과제

C# 21.03.18.과제

HappyFrog 2021. 3. 18. 23:58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    public class App
    {

        //생성자 
        public App()
        {
            Console.WriteLine("App");
            TileMapManager tmm = new TileMapManager();
            tmm.Init();
            tmm.PrintMap();

            Hero hero = new Hero(new Coord(0, 0));
            hero.Move(new Coord(2, 2));
            Coord coord = hero.Coord;
            Console.WriteLine("hero coord: ({0},{1})", coord.x, coord.y);

            MapData data = tmm.GetMapData(coord);
            Console.WriteLine("map data: {0}", data.data);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    public struct Coord
    {
        public int x;
        public int y;
        //생성자 
        public Coord(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

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

namespace Homework000
{
    public class Hero
    {
        public Coord Coord
        {
            get; private set;
        }
        //생성자 
        public Hero(Coord coord)
        {
            this.Coord = coord;
        }
        public void Move(Coord coord)
        {
            int x = this.Coord.x;
            int y = this.Coord.y;
            int cX = coord.x;
            int cY = coord.y;

            int dX = Math.Abs(cX - x);  //X축 거리의 절대값
            int dY = Math.Abs(cY - y);  //Y축 거리의 절대값

            if(dX > 0 && dY > 0)
            {
                for (int j = 0; j < dX; j++)
                {
                    x += 1;
                    Console.WriteLine("X축으로 이동합니다. ({0},{1}) -> ({2},{3})", x-1, y, x, y);	//x-1은 이전 좌표값. x는 현재 좌표값. y는 이동중이 아니라서 현재 y좌표를 입력
                }

                for (int i = 0; i <dY; i++)
                {
                    y +=1 ;
                    Console.WriteLine("Y축으로 이동합니다. ({0},{1}) -> ({2},{3})", x, y-1, x, y);	//x와 같다.
                }                
            }            
            this.Coord = coord;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    public class TileMapManager
    {
        private int row;
        private int col;
        public MapData[,] map;
        public int[,] arr = {
            { 101, 100, 100 },
            { 101, 101, 101 },
            { 100, 100, 100 },
        };

        //생성자 
        public TileMapManager()
        {

        }

        public void Init()
        {
            this.row = this.arr.GetLength(0);
            this.col = this.arr.GetLength(1);

            this.map = new MapData[this.row, this.col];

            for (int i = 0; i < this.row; i++)
            {
                for (int j = 0; j < this.col; j++)
                {
                    var data = new MapData(this.arr[i, j]);
                    this.map[i, j] = data;
                }
            }
        }

        public MapData[,] GetMap()
        {
            return this.map;
        }

        public void PrintMap()
        {
            for (int i = 0; i < this.row; i++)
            {
                for (int j = 0; j < this.col; j++)
                {
                    MapData data = this.map[i, j];
                    Console.Write("{0, 2} ", data.data);
                }
                Console.WriteLine();
            }
        }

        public MapData GetMapData(Coord coord)
        {
            return this.map[coord.y, coord.x];
        }

    }
}

 

'C# > 수업과제' 카테고리의 다른 글

C# 21.03.22.과제 11  (0) 2021.03.22
C# 21.03.19 ~ 21.과제10  (0) 2021.03.20
C# 21.03.17.과제 8  (0) 2021.03.17
C# 21.03.16.과제 7  (0) 2021.03.16
C#21.03.15.과제 6  (0) 2021.03.15