본문 바로가기

C#/수업 과제

클래스 생성과 인스턴스 생성 및 메서드 연습 과제

목표

 

메인메서드

using System;

class Program
{
    static void Main(string[] args)
    {
        Marine marine1 = new Marine("마린1");
        Medic medic1 = new Medic();
        medic1.name = "메딕1";
        medic1.hp = 60;

        Console.WriteLine("{0}이 생성되었습니다. (체력:{1}/{2}, 에너지:{3}/{4})", medic1.name, medic1.hp, medic1.maxHp, medic1.energy, medic1.maxEnergy);

        Marine marine2 = new Marine("마린2");
        Console.WriteLine();

        marine1.Attack(marine2);
        marine2.Hit(marine1.damage);

        for (int i = 0; i < 6; i++)
        {
            Console.WriteLine();
            medic1.Heal(marine2);
        }

        Console.WriteLine();
        Console.WriteLine("{0} (체력:{1}/{2}, 공격력:{3})", marine2.name, marine2.hp, marine2.maxHp, marine2.damage);
        Console.WriteLine("{0} (체력:{1}/{2}, 에너지:{3}/{4})", medic1.name, medic1.hp, medic1.maxHp, medic1.energy, medic1.maxEnergy);
        Console.ReadLine();
    }
}

마린 클래스

using System;

class Marine
{
    //멤버 변수
    public string name;
    public int damage = 5;
    public int maxHp = 40;
    public int hp = 40;

    //생성자
    public Marine(string name)
    {
        this.name = name;
        Console.WriteLine("{0}이 생성되었습니다. (공격력:{1}, 체력:{2}/{3})", this.name, this.damage, this.hp, this.maxHp);
    }

    //멤버 메서드
    public void Attack(Marine target)
    {
        Console.WriteLine("{0}이 {1}을 공격했습니다.", this.name, target.name);
    }

    public void Hit(int hitDamage)
    {
        this.hp -= hitDamage;
        Console.WriteLine("{0}가 피해를 받았습니다. ({1}/{2})", this.name, this.hp, this.maxHp);
    }
}

메딕 클래스

using System;

class Medic
{
    //멤버변수
    public string name;
    public int maxHp = 60;
    public int hp = 60;
    public int maxEnergy = 200;
    public int energy = 200;
    public int amountHeal = 2;

    //생성자
    public Medic()
    {
        
    }

    //멤버 메서드
    public void Heal(Marine target)
    {
        if (target.hp < target.maxHp && energy > 0)
        {
            energy--;
            target.hp += this.amountHeal;

            if (target.hp > target.maxHp && energy > 0)
            {
                this.amountHeal -= (target.hp - target.maxHp);
                target.hp = target.hp - (target.hp - target.maxHp);
                Console.WriteLine("{0}이 {1}의 체력을 회복시킵니다.", this.name, target.name);
                Console.WriteLine("{0}가 체력이 {1}회복되었습니다. ({2}/{3})", target.name, this.amountHeal, target.hp, target.maxHp);
            }
            else if (target.hp < target.maxHp && energy > 0)
            {
                Console.WriteLine("{0}이 {1}의 체력을 회복시킵니다.", this.name, target.name);
                Console.WriteLine("{0}가 체력이 {1}회복되었습니다. ({2}/{3})", target.name, this.amountHeal, target.hp, target.maxHp);
            }
            else if (energy < 0)
            {
                Console.WriteLine("에너지가 부족합니다.");
            }
        }
        else
        {
            Console.WriteLine("더이상 회복할수 없습니다.");
        }
        
    }
}

HelloWorld.exe
0.01MB