본문 바로가기

C#/수업 과제

마린생산-미네랄 감소

메인메서드

using System;

class Program
{
    public static int amountMineral = 50;

    static void Main(string[] args)
    {
        Barracks barracks = new Barracks(3, 5);

        while (true)
        {
            Console.WriteLine("현재 미네랄량: {0}", amountMineral);
            Console.Write("숫자입력 (1.유닛생산 2.미네랄량에디트 나머지: 종료) : ");
            string selected = Console.ReadLine();

            if (selected == "1")
            {
                Console.Write("숫자입력 (1.마린 2.파뱃 3.고스트 4.메딕) : ");
                int inputNum = Convert.ToInt32(Console.ReadLine());
                if (inputNum == 1)
                {
                    Marine marine = (Marine)barracks.CreateUnit(Barracks.eUnitType.Marine);
                }
            }

            else if (selected == "2")
            {
                Console.Write("미네랄량 입력 : ");
                amountMineral = Convert.ToInt32(Console.ReadLine());
            }

            else
            {
                break;
            }
        }
    }
}

병영 클래스

using System;

class Barracks : Building
{
    public enum eUnitType
    {
        Marine, Firebat, Ghost, Medic
    }
    
    private int x;
    private int y;

    //생성자
    public Barracks(int x, int y) : base(x, y)
    {
        this.x = x;
        this.y = y;
    }

    public Unit CreateUnit(eUnitType unit)
    {
        if (unit == eUnitType.Marine)
        {
            if (Program.amountMineral >= 50)
            {
                Program.amountMineral -= 50;
                return new Marine(40, 6);
            }
            else
            {
                Console.WriteLine("미네랄이 부족합니다.");
                return null;
            }
        }
        else
        {
            return null;
        }
    }
}

마린 클래스

using System;

class Marine : Unit
{
    private int hp;
    private int damage;

    //생성자
    public Marine(int hp, int damage)
    {
        this.hp = hp;
        this.damage = damage;
        Console.WriteLine("체력: {0}, 공격력: {1}", this.hp, this.damage);
    }

    public void Attack()
    {

    }
}

이어서 생성한 두번째 세번째 개체를 자동으로 새로운 변수에 넣는다든가 해서

마린끼리의 전투까지 하고싶은데 방법을 모르겠다. 가능하긴할까?

HelloWorld.exe
0.01MB