10.01 커맨드센터에서 SCV1,2를 만들고 공격하기

C#/실습 2019. 10. 1. 16:49

예제:

코드:

Program.cs:
namespace _10._01_step2
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();//App타입 객체를 생성하고 App()이라는 메소드를 호출한다
        }
    }
}

 

App.cs:

namespace _10._01_step2
{
    class App
    {
        public App()
        {
            CommandCenter commandCenter1 = new CommandCenter();
            Console.WriteLine(commandCenter1 + "가 생성되었습니다.");
            Unit unit1 = commandCenter1.MakeUnit("SCV1");
            Unit unit2 = commandCenter1.MakeUnit("SCV2");
            unit1.Attack(unit2);
        }
    }
}

CommandCenter.cs:

namespace _10._01_step2
{
    class CommandCenter
    {
        int hp;

        public CommandCenter()
        {

        }
        public Unit MakeUnit(string name)
        {
            Unit unit = new Unit();
            unit.name = name;
            Console.WriteLine(unit.name + "이 생성되었습니다.");
            return unit;
        }
    }
}

Unit.cs:

using System;

namespace _10._01_step2
{
    class Unit
    {
        //데이터
        public string name;
        int hp =100;
        int maxHp = 100;
        int damage=5;
        public Unit()
        {

        }
        //메서드
        //이동한다
        public void Move()
        {

        }
        public void Attack(Unit unit)
        {
            Console.WriteLine($"{this.name}이{unit.name}에게 {damage}만큼의 피해를 입혔습니다.");
            unit.GetHit(this.damage);
        }
        public void GetHit(int damage)
        {
            this.hp = this.hp - damage;
            Console.WriteLine($"{this.name}이 {damage}만큼의 피해를 입었습니다.");
            Console.WriteLine(this.hp + "/" + this.maxHp);
        }
    }
}

'C# > 실습' 카테고리의 다른 글

10.02 버거세트  (0) 2019.10.02
10.02 커피상점  (0) 2019.10.02
10.01 여러 class를 이용해 햄버거 세트 만들기  (0) 2019.10.01
워크래프트 캐릭생성하기  (0) 2019.09.27
switch 문  (0) 2019.09.26
: