Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

행복한 개구리

C# 21.03.12~14.과제 5 본문

C#/수업과제

C# 21.03.12~14.과제 5

HappyFrog 2021. 3. 12. 18:25

1 질럿과 마린의 싸움 

1.각각의 상태창을 띄운다.(Array)

2.둘의 거리를 측정하고

3.둘중 하나의 거리가 멀다면 상대쪽으로 이동시킨다.

4.사거리가 안에 들어왔다면 공격을 실행하고 둘중 하나가 죽는다면 멈춘다.

1-1 초안

==========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class App
    {
        public App()
        {
            Console.WriteLine("App 생성자 호출됨");

            //클래스 인스턴스 생성
            Position position = new Position();
            Unit unit = new Unit();
            Zealot zealot = new Zealot();
            Marine marine = new Marine();


            //인스턴스 정보 입력
            zealot.name = "질럿";
            zealot.hp = 100;
            zealot.shield = 60;
            zealot.dmg = 8;
            zealot.arm = 1;
            zealot.attackRange = 1;
            zealot.x = 0;
            zealot.y = 6;
            zealot.moveSpeed = 1;
            zealot.atkType = 2;            

            zealot.shieldRegen = 1;

            marine.name = "마린";
            marine.hp = 40;
            marine.shield = 0;
            marine.dmg = 6;
            marine.arm = 1;
            marine.attackRange = 6;
            marine.x = 0;
            marine.y = 0;
            marine.moveSpeed = 1;
            marine.atkType = 1;



            //클래스 기능 실행
            marine.Status(marine);
            zealot.Status(zealot);
                        
            bool b = zealot.OrderMove("Move", "Y");

            marine.Attack(zealot);
            zealot.MoveOn(zealot, marine, b);

            zealot.Attack(marine);

        }
    }
}

 

App을 깔끔하게 사용할 순 없을까

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Marine : Unit
    {
        public Marine()
        {
            Console.WriteLine("Marine 생성자");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Zealot : Unit
    {
        public Zealot()
        {
            
            Console.WriteLine("Zealot 생성자 호출됨");
            
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Unit : Position
    {
        //멤버 정보
        public string name;
        public float hp;
        public float dmg;
        public float attackRange;
        public float arm;
        public float moveSpeed;
        public float shield;
        public float shieldRegen;
        public float atkType;

        //공격 타입
       
        //필요한 형식인지
        /* public enum eAttackType
        {
            Single,
            Double
        }*/

        //생성자
        public Unit()
        {
            Console.WriteLine("Unit 생성자 호출됨");
        }

        //죽음
        public void Die()
        {
            Console.WriteLine("{0}이 죽었습니다.", this.name);
        }

        //유닛 스탯        
        public void Status(Unit unitStat)
        {
            Console.WriteLine("=================================");
            float[] arr = new float[4];
            arr[0] = this.hp;
            arr[1] = this.shield;
            arr[2] = this.dmg;
            arr[3] = this.arm;

            Console.WriteLine("Unit Status");
            Console.WriteLine("NAME/HP/SHIELD/DMG/ARM");
            Console.Write("{0}", this.name);
            for (int i = 0; i < 4; i++)
            {
                Console.Write("/{0}", arr[i]);
            }
            Console.WriteLine("");
            Console.WriteLine("=================================");

        }

        //이동명령
        public bool OrderMove(string question, string answer)
        {
            Console.WriteLine("{0}을 이동시키겠습니까? {1} / {2}", this.name, question, answer);

            if (question == "Move" && answer == "Y")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //★★질문 필요한 메서드인지
        //공격타입
        /*public float AttackType(eAttackType type)
        {
            if (type == eAttackType.Single)
            {
                return 1;
            }
            else if (type == eAttackType.Double)
            {
                return 2;
            }
            else
            {
                return 0;
            }
        }*/      
                

        public void MoveOn(Unit unit, Unit target, bool decision)
        {
            float a = unit.attackRange;
            float distance = Distance(target);
            float nowDistance = distance;

            if (decision == true && nowDistance > unit.attackRange)
            {

                while (unit.attackRange < nowDistance)
                {
                    nowDistance -= unit.moveSpeed;
                    if (nowDistance <= unit.attackRange)
                    {
                        Console.WriteLine("대상에 근접했습니다.", nowDistance);
                        break;
                    }
                                        
                    Console.WriteLine("{0}이 {1}에게 이동합니다.", unit.name, target.name);
                    Console.WriteLine("목표까지 남은 거리 : {0}", nowDistance);
                }
            }
        }


        //★★계속 첫번째 if문부터 출력
        public void Attack(Unit target)
        {
            if (this.atkType == 1)
            {
                int i = 0;
                while (i < 1)
                {
                    if (target.hp > 0)
                    {
                        target.hp -= (this.dmg - target.arm) * 1;
                        Console.WriteLine("{0}이 {1}에게 {2}의 데미지를 가합니다.", this.name, target.name, (this.dmg - target.arm) * 1);
                        continue;
                    }
                    else
                    {
                        target.Die();
                        break;
                    }
                }

            }

            else if (this.atkType == 2)
            {
                int i = 0;
                while (i < 1)
                {
                    if (target.hp > 0)
                    {
                        target.hp -= (this.dmg - target.arm) * 2;
                        Console.WriteLine("{0}이 {1}에게 {2}의 데미지를 가합니다.", this.name, target.name, (this.dmg - target.arm) * 2);
                        continue;
                    }

                    else
                    {
                        target.Die();
                        break;
                    }
                }
            }

            else
            {
                Console.WriteLine("공격할 수 없는 타입입니다.");
            }
        }
    }
}

 

공격하는 타입에 따라 계수를 붙여 아머계산을 쉽게 하고싶었지만 실패했다

그 과정에서 공격타입 메서드와 공격계수 메서드를 지우고 App에서 직접 계수를 부여하여 기능을 실행했다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Position
    {
        public float x;
        public float y;
        public Position()
        {
            Console.WriteLine("Position 생성자 호출됨");
        }

        public float Distance(Unit target)
        {
            Double x = Math.Pow(target.x - this.x, 2);
            Double y = Math.Pow(target.y = this.y, 2);
            Double result = Math.Sqrt(x + y);
            float distance = (float)result;

            return distance;
        } 

        
    }
}

 

암담한 출력값

도착하기 전에 죽었지만 죽어서도 마린에게 원한을 갚는 질럿 

 

 

 

==========================================================================

1-2 최종

1.공격계수와 공격타입을 유닛에게 적용시키는 것을 모르겠어서 포기하고 계수 자체를 Attack에 대입시켜 해결

2.Attack과 Move가 반복문의 형식이었는데 그러면 위의 초안처럼 메서드가 끝난 뒤 다음메서드가 실행되어서

 단발성으로 바꾸고 Battle함수를 새로 반복문으로 짜서 번갈아가며 출력하도록 만듬.

==========================================================================

Position

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Position
    {
        public float x;
        public float y;
        public Position()
        {
            Console.WriteLine("Position 생성자 호출됨");
        }

        public float Distance(Unit target)
        {
            Double x = Math.Pow(target.x - this.x, 2);
            Double y = Math.Pow(target.y = this.y, 2);
            Double result = Math.Sqrt(x + y);
            float distance = (float)result;

            return distance;
        } 
               
    }
}

 

App

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class App
    {
        public App()
        {
            Console.WriteLine("App 생성자 호출됨");

            //클래스 인스턴스 생성
            Position position = new Position();
            Unit unit = new Unit();
            Zealot zealot = new Zealot();
            Marine marine = new Marine();


            //인스턴스 정보 입력
            zealot.name = "질럿";
            zealot.hp = 100;
            zealot.dmg = 8;
            zealot.arm = 1;
            zealot.attackRange = 1;
            zealot.x = 0;
            zealot.y = 6;
            zealot.moveSpeed = 1;
            zealot.atkType = 2;

            zealot.shieldRegen = 1;

            marine.name = "마린";
            marine.hp = 40;
            marine.dmg = 6;
            marine.arm = 1;
            marine.attackRange = 6;
            marine.x = 0;
            marine.y = 0;
            marine.moveSpeed = 1;
            marine.atkType = 1;


            //클래스 기능 실행
            marine.Status(marine);
            zealot.Status(zealot);

            //질럿 이동명령
            bool zY = zealot.OrderMove("Move", "Y");
            
            //싸워라
            zealot.Battle(marine);

        }
    }
}

 

Unit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Unit : Position
    {
        //멤버 정보
        public string name;
        public float hp;
        public float dmg;
        public float attackRange;
        public float arm;
        public float moveSpeed;
        public float shield;
        public float shieldRegen;
        public float atkType;
        
        
        //생성자
        public Unit()
        {
            Console.WriteLine("Unit 생성자 호출됨");
        }

        //죽음
        public void Die()
        {
            Console.WriteLine("{0}이 죽었습니다.", this.name);
        }

        //유닛 스탯        
        public void Status(Unit unitStat)
        {
            Console.WriteLine("=================================");
            float[] arr = new float[4];
            arr[0] = this.hp;
            arr[1] = this.shield;
            arr[2] = this.dmg;
            arr[3] = this.arm;

            Console.WriteLine("Unit Status");
            Console.WriteLine("NAME/HP/SHIELD/DMG/ARM");
            Console.Write("{0}", this.name);
            for (int i = 0; i < 4; i++)
            {
                Console.Write("/{0}", arr[i]);
            }
            Console.WriteLine("");
            Console.WriteLine("=================================");

        }

        //이동명령
        public bool OrderMove(string question, string answer)
        {
            Console.WriteLine("{0}을 이동시키겠습니까? {1} / {2}", this.name, question, answer);

            if (question == "Move" && answer == "Y")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public float Move(Unit target, float leftDistance, bool decision)
        {
            float distance = Distance(target);
            float nowDistance = distance;

            if (decision == true)
            {
                if (nowDistance <= this.attackRange)
                {
                    Console.WriteLine("대상에 근접했습니다.");
                    Console.WriteLine("목표까지 남은 거리 : {0}", nowDistance);
                }
                else
                {
                    nowDistance -= this.moveSpeed;
                    Console.WriteLine("{0}이 {1}에게 이동합니다.", this.name, target.name);
                    Console.WriteLine("목표까지 남은 거리 : {0}", nowDistance);
                }
            }
            return nowDistance;          
        }


        
        public void Attack(Unit target)
        {
            if (this.atkType == 1)
            {
                if (target.hp > 0)
                {
                    target.hp -= (this.dmg - target.arm) * this.atkType;
                    Console.WriteLine("{0}이 {1}에게 {2}의 데미지를 가합니다. {1}의 남은 체력 : {3}", this.name, target.name, (this.dmg - target.arm) * 1, target.hp);
                }
                else
                {
                    target.Die();
                }
            }
            else if (this.atkType == 2)
            {
                if (target.hp > 0)
                {
                    target.hp -= (this.dmg - target.arm) * this.atkType;
                    Console.WriteLine("{0}이 {1}에게 {2}의 데미지를 가합니다. {1}의 남은 체력 : {3}", this.name, target.name, (this.dmg - target.arm) * 2, target.hp);
                }
                else
                {
                    target.Die();
                }
            }


            else
            {
                Console.WriteLine("대상이 너무 멉니다.");
            }
            
        }

        public void Battle(Unit target)
        {
            float a = this.attackRange;
            float distance = Distance(target);
            float nowDistance = distance;

            if (nowDistance > this.attackRange)
            {

                while (this.Distance(target) > this.attackRange)
                {
                    target.Attack(this);
                    nowDistance -= this.moveSpeed;

                    if (nowDistance <= this.attackRange)
                    {                        
                        if (nowDistance == this.attackRange)
                        {
                            Console.WriteLine("대상에 근접했습니다.", nowDistance);
                        }
                        
                        else if (nowDistance > this.attackRange)
                        {                           
                            Console.WriteLine("목표까지 남은 거리 : {0}", nowDistance);
                        }
                        Console.WriteLine("");
                        if (this.hp > 0 && target.hp > 0)
                        {
                            target.Attack(this);
                            this.Attack(target);
                            continue;
                        }
                        else if (this.hp > 0 && target.hp <= 0)
                        {
                            target.Die();
                            break;
                        }
                        else
                        {
                            this.Die();
                            break;
                        }
                        
                    }
                    
                    Console.WriteLine("{0}이 {1}에게 이동합니다.", this.name, target.name);
                    Console.WriteLine("목표까지 남은 거리 : {0}", nowDistance);
                    Console.WriteLine("");


                }


            }
            



        }
    }
}

 

Zealot

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Zealot : Unit
    {
        public Zealot()
        {
            
            Console.WriteLine("Zealot 생성자 호출됨");
            
        }

    }
}

 

Marine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Zealot : Unit
    {
        public Zealot()
        {
            
            Console.WriteLine("Zealot 생성자 호출됨");
            
        }

    }
}

EMP를 맞아버린 질럿은 마린에게 화풀이를 하러갑니다.

 

 

==========================================================================

2 SCV 미네랄 채취

1.각 유닛에게 좌표부여

2.채집할 수 있는 거리보다 멀다면 채집할 수 없다는 메시지를 출력한 뒤 이동시작

3.이동은 처음 거리값 - 유닛의 이동속도로 계산

4.SCV가 도착하면 GoMine메서드로 채광시작

 

==========================================================================

 

App

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    class App
    {


        //생성된 인스턴스에 종족을 부여하는 방법
        public App()
        {
            Console.WriteLine("App생성자 호출됨");

            CommandCenter commandCenter = new CommandCenter();
            Mineral mineral = new Mineral();
            SCV scv = new SCV();

            commandCenter.name = "커멘드 센터";
            commandCenter.x = 0;
            commandCenter.y = 0;
            commandCenter.moveSpeed = 1;

            mineral.name = "미네랄";
            mineral.x = 12;
            mineral.y = 10;

            scv.name = "SCV";
            scv.x = 0;
            scv.y = 1;
            scv.moveSpeed = 2;
            scv.range = 1;

            
            scv.Move(mineral);
            scv.GoMine(mineral);
            



        }
    }
}

 

Unit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{



    class Unit
    {
        public string name;
        public float x;
        public float y;
        public float moveSpeed;
        public float range;

        public enum eRace
        {
            Zerg,
            Protoss,
            Terran,
            Object
        }

        public Unit()
        {
            Console.WriteLine("Unit생성자 호출됨");
        }


        public float Distance(Unit target)
        {
            Double x1 = Math.Pow(target.x - this.x, 2);
            Double y1 = Math.Pow(target.y - this.y, 2);
            Double d1 = Math.Sqrt(x1 + y1);
            float distance = (float)d1;

            return distance;
        }

        public float Move(Unit target)
        {

            float distance = this.Distance(target);
            float leftDistance = distance;
            Console.WriteLine("{0}와 {1}사이의 거리는 {2}입니다.", this.name, target.name, distance);
            Console.WriteLine("");
            if (this.range < leftDistance)
            {
                Console.WriteLine("거리가 멀어서 채집할 수 없습니다. 유닛을 이동합니다.");
                Console.WriteLine("");
            }


            int i = 0;
            while (i < 1)
            {
                if (this.range < leftDistance)
                {
                    leftDistance -= this.moveSpeed;
                    Console.WriteLine("{0}가 이동합니다 남은거리는 {1}입니다.", this.name, leftDistance);
                    Console.WriteLine("");
                }
                else
                {
                    Console.WriteLine("{0}가 목적지에 도착했습니다.", this.name);
                    Console.WriteLine("");
                    i++;
                }
            }

            return leftDistance;
        }

        public void GoMine(Unit target)
        {
            Console.WriteLine("{0}가 채집을 시작합니다.", this.name);
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("...채집중");
            }
            Console.WriteLine("");
            Console.WriteLine("=======================");
            Console.WriteLine("채집완료 채집량 : 8");
            Console.WriteLine("=======================");



        }





    }
}

 

CommandCenter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    class CommandCenter : Unit
    {
        public CommandCenter()
        {
            Console.WriteLine("CommandCenter생성자 호출됨");
        }
    }
}

SCV

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    class SCV : Unit
    {
        public SCV()
        {
            Console.WriteLine("SCV생성자 호출됨");
            
        }
    }
}

 

Mineral

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    class Mineral : Unit
    {
        public Mineral()
        {
            Console.WriteLine("Mineral생성자 호출됨");
        }
    }
}

 

출력값

 

==========================================================================

3 유닛 스탯 params 함수로 불러오기

1. Unit클래스에 params를 사용하여 정보를 입력하는 메서드 생성

2.SCV는 params만 이용하여 출력하고

3.Drone은 메서드의 매개변수를 params로 정한 뒤 출력은 Array를 이용하여 출력

 

==========================================================================

 

App

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class App
    {
        public string name;
        public App()
        {
            Console.WriteLine("App생성자 호출됨");

            Probe probe1 = new Probe();
            Probe probe2 = new Probe();
            probe1.name = "프로브";
            probe2.name = "프로비우스";

            Drone drone1 = new Drone();
            Drone drone2 = new Drone();
            drone1.name = "드론";
            drone2.name = "어린 드론";


            Console.WriteLine("HP/DMG/ARM/MOVESPEED/SIGHT");
            probe1.StatProbe(40, 5, 0, 2, 8);
            probe2.StatProbe(100, 8, 3, 2, 9);

            drone1.StatDrone(40, 5, 2, 2, 8);
            drone2.StatDrone(25, 4, 0, 3, 6);
        }


    }
}

 

Unit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Unit
    {
        public string name;
        public Unit()
        {
            Console.WriteLine("Unit생성자 호출됨");
        }

        //SCV 기본 값 설정
        public void StatProbe(int hp, int dmg, int arm, int moveSpeed, int sight)
        {
            Console.WriteLine("{0}==========================", this.name);            
            Console.Write("/ {0} /", hp);
            Console.Write("/ {0} /", dmg);
            Console.Write("/ {0} /", arm);
            Console.Write("/ {0} /", moveSpeed);
            Console.WriteLine("/ {0} /", sight);

        }

        public void StatDrone(params int[] arr)
        {
            Console.WriteLine("{0}==========================", this.name);

            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("/ {0} /", arr[i]);
            }
            Console.WriteLine("");
            
        }
    }
}

 

 

Probe(상속만 시킴)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Probe : Unit
    {
        public Probe()
        {
            Console.WriteLine("Probe 생성자 호출됨");
        }
    }
}

 

Drone(상속만 시킴)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework000
{
    class Drone : Unit
    {
        public Drone()
        {
            Console.WriteLine("Drone생성자 호출됨");
        }
    }
}

 

 

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

C# 21.03.16.과제 7  (0) 2021.03.16
C#21.03.15.과제 6  (0) 2021.03.15
C# 21.03.11.과제 4  (0) 2021.03.12
C# 21.03.10.과제 3  (0) 2021.03.10
C# 21.03.09.과제 2  (0) 2021.03.09