본문 바로가기
C#/수업내용

[C# 5일차] Class 복습과 활용_Starcraft Marine과 Medic

by 왹져박사 2023. 1. 5.
728x90

Marine
------
이름 <---외부에서 설정
공격력 5
체력 60 
------
마린을 공격하다
이동하다
------

 

 

 

 

APP Class

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

namespace Study04
{
    class APP
    {
        //생성자

        public APP()
        {
            Console.WriteLine("APP 생성자");

            Marine marine0 = new Marine("marine0", 60, 5);

            marine0.Hit(35);
            Medic medic0 = new Medic("medic0", 5.86f, 50, 200, 5);    //치료량, 에너지(50/ 200)
            medic0.Heal(marine0);

            //marine0 40/60
            Console.WriteLine("{0} 체력 : {1}/{2}", marine0.name, marine0.hp, marine0.maxHp);

            //치료 할때 마다 에너지 5 감소
            //medic0 45/200
            medic0.Energy();
            Console.WriteLine("{0} 에너지: {1}/{2}", medic0.name, medic0.energy, medic0.maxEnergy);

        }
    }
}

 

Marine Class

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

namespace Study04
{
    class Marine
    {
        public string name;
        public int damage;
        public int maxHp;
        public int hp;

        //생성자
        public  Marine(string name, int maxHp, int damage)
        {
            this.name = name;
            this.maxHp = maxHp;
            this.hp = this.maxHp;
            this.damage = damage;
            Console.WriteLine("\"{0}\" 생성되었습니다. ({1}/{2})", this.name, this.hp, this.maxHp);
        }

        public void Move()
        {
            Console.WriteLine("{0} 이동했습니다. ", this.name);
        }
        public void AttackMarine(Marine target)
        {
            Console.WriteLine("{0}이 {1} 공격 했습니다. ", this.name, target.name);
            //target.hp = target.hp - this.damage;
            //Console.WriteLine("{0}가 {1}에게 피해(-{2})를 받았습니다. ({3}/{4})", target.name, this.name, this.damage, target.hp, target.maxHp);

            target.Hit(this.damage);
        }

       public void Hit(int damage)
        {
            this.hp = this.hp - damage;
            Console.WriteLine("{0} 체력 : {1}/{2}", this.name, this.hp, this.maxHp);
        }
    }
}

 

Medic Class

Medic

------
이름 <---외부에서 설정
공격력 5
체력 60 
------
마린을 치유함
치유하면 에너지를 사용함
------

<예외사항 처리>

1. 마린의 체력이 가득 찼을때 힐 하면 "더이상 치료할 수 없습니다. "

2. 메딕의 에너지가 5이하일 경우 "에너지가 부족합니다. "

3, 마린의 체력이 치료된 후 최대체력을 넘어가면 안됨

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

namespace Study04
{
    
    class Medic
    {
        public string name;
        float heal;
        public float energy;
        public float maxEnergy;
        float healEnergy;

        //생성자
        public Medic(string name, float heal, float energy, float maxEnergy, float healEnergy)
        {
            this.name = name;
            this.heal = heal;
            this.energy = energy;
            this.maxEnergy = maxEnergy;
            this.healEnergy = healEnergy;
        }

        public void Heal(Marine target)
        {
            if(target.hp == 0 || target.hp == target.maxHp)     //마린의 체력이 0이거나(죽음) 마린의 체력이 최대일 경우
            {
                Console.WriteLine("더이상 치료할 수 없습니다. ");

            }
            else if(this.energy < this.healEnergy)        //메딕의 에너지가 5이하일 경우
            {
                Console.WriteLine("에너지가 부족합니다.");

            }
            else
            {
                target.hp = target.hp + (int)this.heal;
                Console.WriteLine("{0}을 회복시켰습니다. ", target.name);

                if (target.hp > target.maxHp)   //체력이 치료된 후 최대체력을 넘어가면 안됨
                {
                    target.hp = target.maxHp;
                }

            }
        }




        public void Energy()
        {
            this.energy = this.energy - healEnergy;

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

namespace Study04
{
    class Program
    {
        static void Main(string[] args)
        {
            new APP();
        }
    }
}

 

실행결과

728x90