본문 바로가기

C#/수업 과제

SCV 미네랄캐고 건물짓기

더보기
using System;

class Program
{
    public enum eBuildType
    {
        Barracks, SupplyDepot
    }

    public static int toX;
    public static int toY;

    static void Main(string[] args)
    {

        Building commandcenter = new CommandCenter(0, 0);
        CommandCenter commandcenter1 = (CommandCenter)commandcenter;
        Minerals minerals = new Minerals(-5, -4, 1500);
        SCV scv = new SCV(-1, -1);
        
        bool havePiece;
        while (true)
        {
            Console.WriteLine();
            Console.Write("##입력 (1: 이동, 2: 건설, 3:미네랄에디트 나머지: 종료##) : ");
            string selected = Console.ReadLine();
            if (selected == "3")
            {
                Console.Write("입력 : 미네랄 량 수치 ");
                int editMineral = Convert.ToInt32(Console.ReadLine());
                commandcenter1.amountMineral = editMineral;
                Console.WriteLine("현재 미네랄량 : {0}", commandcenter1.amountMineral);
            }
            else if (selected == "1")
            {
                if (scv.piece != null)
                {
                    havePiece = true;
                }
                else
                {
                    havePiece = false;
                }
                //Console.WriteLine("scv가 가지고 있는 미네랄 조각: {0}", scv.piece);
                Console.WriteLine("scv가 미네랄 조각을 가지고 있는가?: {0}", havePiece);
                Console.WriteLine();
                Console.Write("x 좌표입력: ");
                toX = Convert.ToInt32(Console.ReadLine());
                Console.Write("y 좌표입력: ");
                toY = Convert.ToInt32(Console.ReadLine());
                scv.Move(toX, toY);
                if (toX == -5 && toY == -4)
                {
                    scv.piece = scv.GatherMineral(minerals);
                }
                else if (toX == 0 && toY == 0 && havePiece == true)
                {
                    scv.ReturnResource(commandcenter1);
                }
            }
            else if (selected == "2")
            {
                Console.WriteLine("현재 미네랄량 : {0}", commandcenter1.amountMineral);
                Console.Write("건설목표좌표 x입력: ");
                toX = Convert.ToInt32(Console.ReadLine());
                Console.Write("건설목표좌표 y입력: ");
                toY = Convert.ToInt32(Console.ReadLine());
                Console.Write("건설 선택 (0병영, 1보급고): ");
                int buildNum = Convert.ToInt32(Console.ReadLine());

                if (toX == -5 && toY == -4 || toX == 0 && toY == 00)
                {
                    Console.WriteLine("건물을 지을 공간이 없습니다.");
                }
                else if (buildNum == (int)eBuildType.Barracks && commandcenter1.amountMineral >= 150)
                {
                    commandcenter1.amountMineral -= 150;
                    scv.Move(toX, toY);
                    scv.Build(toX, toY, buildNum);
                }
                else if (buildNum == (int)eBuildType.SupplyDepot && commandcenter1.amountMineral >= 100)
                {
                    commandcenter1.amountMineral -= 100;
                    scv.Move(toX, toY);
                    scv.Build(toX, toY, buildNum);
                }
                else
                {
                    Console.WriteLine("미네랄이 부족합니다");
                }
            }
            else
            {
                break;
            }
        }      
    }
}

 

더보기
using System;
using System.Threading;

class SCV
{
    //변수
    private int x;
    private int y;

    public Minerals piece;     //미네랄 조각


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

    //메서드

    public void Move(int x, int y)
    {
        if (x != this.x || y != this.y)
        {
            Console.WriteLine("{0},{1}로 이동합니다.", x, y);

            while (x < this.x)
            {
                Thread.Sleep(100);
                Console.WriteLine("({0},{1})", --this.x, this.y);
            }
            while (x > this.x)
            {
                Thread.Sleep(100);
                Console.WriteLine("({0},{1})", ++this.x, this.y);
            }
            while (y < this.y)
            {
                Thread.Sleep(100);
                Console.WriteLine("({0},{1})", this.x, --this.y);
            }
            while (y > this.y)
            {
                Thread.Sleep(100);
                Console.WriteLine("({0},{1})", this.x, ++this.y);
            }

            Console.WriteLine("이동을 완료합니다.");


            if (this.x == -5 && this.y == -4)
            {
                Console.WriteLine("미네랄이 있습니다.");
            }

            else if (this.x == 0 && this.y == 0)
            {
                Console.WriteLine("커맨드센터가 있습니다.");
            }
        }
        
    }

    public Minerals GatherMineral(Minerals target)
    {
        Console.WriteLine("미네랄을 채취합니다.");
        target.deposits -= 8; //큰 미네랄에서 8을 뺀다
        return this.piece = new Minerals(this.x, this.y, 8);
    }

    public Minerals ReturnResource(CommandCenter commandCenter)
    {
        if (this.piece != null)
        {
            commandCenter.amountMineral += 8;
            Console.WriteLine("미네랄을 커맨드센터에 저장합니다.");
            Console.WriteLine(commandCenter.amountMineral);
            return this.piece = null;
        }
        else
        {
            return this.piece;
        }
        
    }

    public Building Build(int x, int y, int buildNum)
    {
        if (buildNum == 0)
        {
            Console.WriteLine("({0},{1})에 {2}를 건설합니다.", x, y, buildNum);
            return new Barracks(x, y);
        }
        else if (buildNum == 1)
        {
            Console.WriteLine("({0},{1})에 {2}를 건설합니다.", x, y, buildNum);
            return new SupplyDepot(x, y);
        }
        else
        {
            Console.WriteLine("null : {0}", null);
            return null;
        }
    }
}

 

더보기
using System;

class Minerals
{
    public int deposits;     //매장량
    private const int mineralX = -5;
    private const int mineralY = -4;

    //생성자
    public Minerals(int x, int y, int deposits)
    {
        this.deposits = deposits;
        Console.WriteLine("{0}이 ({1},{2})에 생성되었습니다. 매장량:{3}", this, x, y, this.deposits);
    }

    public void BeGather()
    {

    }
}

 

더보기
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 CommandCenter : Building
{
    //변수
    private int x;
    private int y;
    public int amountMineral = 50;

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

    //메서드
}

 

더보기
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;
    }
}

 

더보기
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;
    }
}

 

HelloWorld.exe
0.01MB