본문 바로가기

C#/수업 과제

건물건설과 유닛생산

메인메서드

더보기
using System;

class Program
{
    public enum eBuildType
    {
        Barracks, SupplyDepot, Academy
    }

    public enum eUnitType
    {
        Marine, Firebat, Ghost, Medic
    }

    public static int amountMineral = 50;

    static int toX;
    static int toY;
    static int inputNum;
    static Barracks barracks1;
    static Academy academy1;
    static SupplyDepot supplyDepot1;

    static void Main(string[] args)
    {
        
        SCV scv = new SCV();
        Marine marine1 = new Marine(40, 6, "마린1");
                
        while (true)
        {
            Console.WriteLine();
            Console.WriteLine("현재 미네랄: {0}", amountMineral);
            Console.Write("숫자입력 (0:미네랄에디트, 1:건설, 2:유닛생산 //나머지:종료) : ");
            string input = Console.ReadLine();
            if (input == "0")
            {
                Console.Write("미네랄 량 입력 : ");               
                amountMineral = Convert.ToInt32(Console.ReadLine());
            }
            else if (input == "1")
            {
                Console.Write("건설 목표 x좌표 입력: ");
                toX = Convert.ToInt32(Console.ReadLine());
                Console.Write("건설 목표 y좌표 입력: ");
                toY = Convert.ToInt32(Console.ReadLine());
                Console.Write("선택 (0병영, 1보급고, 2아카데미): ");
                inputNum = Convert.ToInt32(Console.ReadLine());
                switch (inputNum)
                {
                    case 0:
                        barracks1 = (Barracks)scv.Build(toX, toY, eBuildType.Barracks);
                        break;

                    case 1:
                        supplyDepot1 = (SupplyDepot)scv.Build(toX, toY, eBuildType.SupplyDepot);
                        break;

                    case 2:
                        academy1 = (Academy)scv.Build(toX, toY, eBuildType.Academy);
                        break;
                }
                
            }
            else if (input == "2")
            {
                Console.Write("숫자입력 (0:마린, 1:파뱃, 2:고스트, 3:메딕) : ");
                string inputNum = Console.ReadLine();
                if (barracks1 == null)
                {
                    Console.WriteLine("먼저 병영을 건설해주세요");
                }
                else
                {
                    switch (inputNum)
                    {
                        case "0":
                            Marine marine2 = (Marine)barracks1.CreateUnit(eUnitType.Marine);
                            break;

                        case "1":
                            if (academy1 != null)
                            {
                                barracks1.CreateUnit(eUnitType.Firebat);
                            }
                            else
                            {
                                Console.WriteLine("아카데미를 먼저 건설해주세요.");
                            }
                            break;

                        case "2":
                            barracks1.CreateUnit(eUnitType.Ghost);
                            break;

                        case "3":
                            if (academy1 != null)
                            {
                                barracks1.CreateUnit(eUnitType.Medic);
                            }
                            else
                            {
                                Console.WriteLine("아카데미를 먼저 건설해주세요.");
                            }
                            break;
                    }
                }
            }
            else
            {
                break;
            }
        }
        

        Console.ReadLine();
    }
}

SCV클래스

더보기
using System;
using System.Threading;

class SCV
{
    //생성자
    public SCV()
    {

    }

    public Building Build(int x, int y, Program.eBuildType build)
    {
        if (build == Program.eBuildType.Barracks && Program.amountMineral >= 150)
        {
            Program.amountMineral -= 150;
            Console.WriteLine("({0},{1})에 {2}를 건설합니다.", x, y, build);
            return new Barracks(x, y);
        }
        else if (build == Program.eBuildType.Academy && Program.amountMineral >= 150)
        {
            Program.amountMineral -= 150;
            Console.WriteLine("({0},{1})에 {2}를 건설합니다.", x, y, build);
            return new Academy(x, y);
        }
        else if (build == Program.eBuildType.SupplyDepot && Program.amountMineral >= 100)
        {
            Program.amountMineral -= 100;
            Console.WriteLine("({0},{1})에 {2}를 건설합니다.", x, y, build);
            return new SupplyDepot(x, y);
        }
        else
        {
            Console.WriteLine("미네랄이 부족합니다.");
            return null;
        }
    }
}

병영 클래스

더보기
using System;

class Barracks : Building
{
    private int x;
    private int y;

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

    public Unit CreateUnit(Program.eUnitType unit)
    {
        if (unit == Program.eUnitType.Marine)
        {
            if (Program.amountMineral >= 50)
            {
                Program.amountMineral -= 50;
                return new Marine(40, 6, "마린");
            }
            else
            {
                Console.WriteLine("미네랄이 부족합니다.");
                return null;
            }
        }
        else if (unit == Program.eUnitType.Firebat)
        {
            if (Program.amountMineral >= 50)
            {
                Program.amountMineral -= 50;
                return new Firebat(50, 8, "파이어뱃");
            }
            else
            {
                Console.WriteLine("미네랄이 부족합니다.");
                return null;
            }
        }
        else if (unit == Program.eUnitType.Ghost)
        {
            Console.WriteLine("연구실을 먼저 지어주세요.");
            return null;
        }
        else if (unit == Program.eUnitType.Medic)
        {
            if (Program.amountMineral >= 50)
            {
                Program.amountMineral -= 50;
                return new Medic(60, 200, "메딕");
            }
            else
            {
                Console.WriteLine("미네랄이 부족합니다.");
                return null;
            }
        }
        else
        {
            return null;
        }
    }
}

Building

더보기
using System;

class Building
{
    //멤버 변수
    private int x;
    private int y;

    //생성자
    public Building(int x, int y)
    {
        this.x = x;
        this.y = y;
        Console.WriteLine("{0}가 ({1},{2})에 생성되었습니다.", this, this.x, this.y);
    }
}

아카데미

더보기
using System;

class Academy : Building
{
    private int x;
    private int y;

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

메딕

더보기
using System;

class Medic : Unit
{
    private int hp;
    private int energy;
    private string name;
    private static int x, y;

    //생성자
    public Medic(int hp, int energy, string name) : base(x, y, name)
    {

    }
}

마린

더보기
using System;

class Marine : Unit
{
    //변수
    private int maxHp = 40;
    public int hp;
    private int damage;
    public string name;
    private static int x;
    private static int y;

    //생성자
    public Marine(int hp, int damage, string name) : base(x, y, name)
    {
        this.hp = hp;
        this.damage = damage;
        this.name = name;
        Console.WriteLine("체력: {0}, 공격력: {1}, 이름: {2}", this.hp, this.damage, this.name);
    }

    //메서드
    public void Attack(Marine target)
    {
        if (target.hp > 0)
        {
            Console.WriteLine("{0}이 {1}를 공격합니다.", this.name, target.name);
        }
        else
        {
            Console.WriteLine("잘못된 대상입니다.");
        }
        
    }

    public void Hit(Marine enermy)
    {
        if (this.hp > 0)
        {
            this.hp -= enermy.damage;
            if (this.hp > 0)
            {
                Console.WriteLine("{0}가 {1}로부터 공격을 받았습니다.", this.name, enermy.name);
                Console.WriteLine("hp: {0}/{1}", this.hp, this.maxHp);
            }            
            if (this.hp < 0)
            {
                this.hp = 0;
                Console.WriteLine("{0}가 {1}로부터 공격을 받았습니다.", this.name, enermy.name);
                Console.WriteLine("hp: {0}/{1}", this.hp, this.maxHp);
                Console.WriteLine("{0}가 죽었습니다.", this.name);
            }   
        }
    }
}

Unit

더보기
using System;

class Unit
{
    public int hp;
    private int damage;    
    public string name;
    //생성자
    public Unit(int x, int y, string name)
    {
        this.name = name;
        Console.WriteLine("{0}이 생산되었습니다.", this);
    }

    public virtual void Attack(Unit target)
    {

    }
}

파뱃

더보기
using System;

class Firebat : Unit
{
    private int maxHp = 50;
    public int hp;
    private int damage;
    public string name;
    private static int x;
    private static int y;

    //생성자
    public Firebat(int hp, int damage, string name) : base(x, y, name)
    {
        this.hp = hp;
        this.damage = damage;
        this.name = name;
        Console.WriteLine("체력: {0}, 공격력: {1}, 이름: {2}", this.hp, this.damage, this.name);
    }
}

보급고

더보기
using System;

class SupplyDepot : Building
{
    //멤버 변수
    private int x;
    private int y;

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

HelloWorld.exe
0.01MB

 

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

OX퀴즈  (0) 2021.12.24
탱크와 벌쳐 전투  (0) 2021.12.24
마린2를 공격하는 마린1  (0) 2021.12.24
마린생산-미네랄 감소  (0) 2021.12.24
SCV 미네랄캐고 건물짓기  (0) 2021.12.24