c# 강의/과제

20200414 - 테란~아카데미까지

쪼혀 2020. 4. 14. 13:11

 


> App.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414
{
    class App
    {
        bool isBarracks;
        bool isAcademy;


        public App()
        {


            Barracks barracks = new Barracks();
            barracks.Init(1001, 1000); // 초기화 - ID / HP
            this.isBarracks = true;

            LiftOffState state = barracks.GetLiftOffState();

            barracks.Move();
            barracks.Stop();
            barracks.ChangeLiftOffState();
            barracks.Move();
            barracks.Stop();

            barracks.ChangeLiftOffState();
            barracks.Move();
            barracks.Stop();

            Console.WriteLine();


            // 캐릭터 생성
            if (this.isBarracks)
            {
                Unit marine = barracks.CreateUnit(UnitType.Marine);

                marine.Attack(barracks);

                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("마린을 생성하려면 배럭이 필요합니다.");
            }

           

            // 아카데미 생성
            if (isBarracks)
            {
                Academy academy = new Academy();
                academy.Init(2001, 600);
                this.isAcademy = true;
            }
            else
            {
                Console.WriteLine("아카데미를 생성하려면 배럭스가 필요합니다.");
            }

            // 파이어뱃 생성
            if (isAcademy)
            {
                Unit medic = barracks.CreateUnit(UnitType.Medic);
                Unit firebat = barracks.CreateUnit(UnitType.Firebat);
            }
            else
            {
                Console.WriteLine("파이어베이스를 생성하려면 아카데미가 필요합니다.");
            }
        }
    }
}

 

> Unit.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414
{
    public enum UnitType
    {
        Marine, Medic, Firebat, Ghost
    }

    class Unit
    {
        UnitType unitType;
        int damage;
        int hp;

        public Unit(UnitType unitType, int damage, int hp)
        {
            this.unitType = unitType;
            this.damage = damage;
            this.hp = hp;

            Console.WriteLine($"{this.unitType} / {this.damage} / {this.hp} 생성");
        }

        public void Attack(Barracks target)
        {
            Console.WriteLine($"[{GetUnitType()}]이(가) [{target.name}]을 공격합니다.");
            target.Hit(this, this.damage);
        }

        public UnitType GetUnitType()
        {
            return this.unitType;
        }
    }
}

> Barracks.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414
{
    enum LiftOffState
    {
        LiftOff,
        Land
    }

    class Barracks
    {
        //멤버 변수
        public int id;
        public string name; //이름
        public int maxHp; //체력
        public int hp;
        public LiftOffState liftOffState; // 떠있는 상태


        //생성자
        public Barracks()
        {
            Console.WriteLine("배럭스가 생성되었습니다.");
            this.liftOffState = LiftOffState.Land;
            this.name = "배럭스";
        }

        // 초기화 ID / HP
        public void Init(int id, int maxHp)
        {
            this.id = id;
            this.maxHp = maxHp;
            this.hp = this.maxHp;
        }

        //상태를 변경할수 있다.
        public void ChangeLiftOffState()
        {
            if (GetLiftOffState() == LiftOffState.Land)
            {
                LiftOff();
            }
            else if (GetLiftOffState() == LiftOffState.LiftOff)
            {
                Land();
            }
        }

        //이동
        public void Move()
        {
            if (GetLiftOffState() == LiftOffState.Land)
            {
                Console.WriteLine("배럭을 띄운 후 에 이동 가능합니다.");
            }
            else
            {
                Console.WriteLine("배럭이 이동합니다.");
            }

        }

        //정지
        public void Stop()
        {
            if (GetLiftOffState() == LiftOffState.Land)
            {
                Console.WriteLine("배럭을 띄운 후에 명령이 가능합니다.");
            }
            else
            {
                Console.WriteLine("배럭을 멈춥니다.");
            }
        }

        //유닛생성한다 (유닛의 타입)
        public Unit CreateUnit(UnitType unitType)
        {
            int unitDmg = 0;
            int unitHp = 0;
            switch (unitType)
            {
                case UnitType.Marine:
                    unitDmg = 6;
                    unitHp = 100;
                    break;
                case UnitType.Medic:
                    unitDmg = 0;
                    unitHp = 80;
                    break;
                case UnitType.Firebat:
                    unitDmg = 7;
                    unitHp = 100;
                    break;
                case UnitType.Ghost:
                    unitDmg = 10;
                    unitHp = 120;
                    break;
                default:
                    break;
            }

            Unit unit = new Unit(unitType, unitDmg, unitHp);
            return unit;
        }

        //유닛으로부터 피해를 받을수 있다.
        public void Hit(Unit unit, int damage)
        {
            this.hp = maxHp - damage;
            if (this.hp <= 0)
            {
                this.hp = 0;
                Destruction(this, unit);
            }
            else
            {
                Console.WriteLine($"[{this.name}]이(가) [{unit.GetUnitType()}]에게 {damage}의 피해를 받았습니다.({this.hp}/{this.maxHp})");
            }
        }

        //파괴될수 있다.
        public void Destruction(Barracks barracks , Unit unit)
        {
            Console.WriteLine($"[{this.name}]이(가) [{unit.GetUnitType()}]에게 파괴되었습니다.");
            barracks = null;
        }

        // 배럭 내린다.
        public void Land()
        {
            this.liftOffState = LiftOffState.Land;
            Console.WriteLine("> 배럭을 내립니다.");
        }

        //배럭을 공중에 띄운다..
        public void LiftOff()
        {
            this.liftOffState = LiftOffState.LiftOff;
            Console.WriteLine("> 배럭을 띄웁니다.");
        }

        // 리프트 상태값 호출
        public LiftOffState GetLiftOffState()
        {
            return this.liftOffState;
        }

    }
}

> Academy.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414
{
    class Academy
    {
        public int id;
        public string name; //이름
        public int maxHp; //체력
        public int hp;

        public Academy()
        {
            Console.WriteLine("아카데미가 생성되었습니다.");
            this.name = "아카데미";
        }

        public void Init(int id, int maxHp)
        {
            this.id = id;
            this.maxHp = maxHp;
            this.hp = this.maxHp;
        }

        //유닛으로부터 피해를 받을수 있다.
        public void Hit(Unit unit, int damage)
        {
            this.hp = maxHp - damage;
            if (this.hp <= 0)
            {
                this.hp = 0;
                Destruction(this, unit);
            }
            else
            {
                Console.WriteLine($"[{this.name}]이(가) [{unit.GetUnitType()}]에게 {damage}의 피해를 받았습니다.({this.hp}/{this.maxHp})");
            }
        }

        //파괴될수 있다.
        public void Destruction(Academy academy, Unit unit)
        {
            Console.WriteLine($"[{this.name}]이(가) [{unit.GetUnitType()}]에게 파괴되었습니다.");
            academy = null;
        }
    }
}