Unity/수업과제
과제 9 - delegate
ljw4104
2021. 3. 22. 17:28
1. Item 클래스에서 강화를 진행하고 App에 완료되었다고 알림.
using System;
namespace Study09
{
public class App
{
public App()
{
Item item = new Item();
//델리게이트 메소드 연결
item.reinforce = ShowPopup;
item.Reinforce();
}
public void ShowPopup()
{
Console.WriteLine("[APP] : 강화가 끝났습니다.");
}
}
}
using System;
using System.Threading;
namespace Study09
{
//델리게이트 정의
public delegate void DelReinforce();
public class Item
{
//델리게이트 변수 선언
public DelReinforce reinforce;
private const int prob = 45;
private const int destroyProb = 2;
private int damage = 50;
public Item()
{
}
public void Reinforce()
{
while(damage < 70)
{
Random r = new Random();
int tmp = r.Next(1, 101);
Console.Write("[ITEM] : ");
if(tmp <= 45)
{
damage += 5;
Console.WriteLine("강화 성공");
}
else if(tmp > 47)
{
damage -= 3;
Console.WriteLine("강화 실패");
}
else
{
damage = 0;
Console.WriteLine("강화 파괴");
break;
}
Thread.Sleep(200);
}
reinforce();
}
}
}
2. 출석 시스템 - 전원이 출석할 때, 출석완료라는 메시지를 출력함.
- 10명의 학생들은 각각 랜덤한 확률로 출석을 함.
- 대리자가 하나의 매개변수를 받음.
using System;
namespace Study09
{
public class App
{
public App()
{
RollBook rollBook = new RollBook();
rollBook.delCheck = CheckAttendance;
Console.WriteLine("==== 출석체크를 진행합니다. ====");
rollBook.Check();
}
private void CheckAttendance(bool isAllPresent)
{
if (isAllPresent)
{
Console.WriteLine("모두 출석하였습니다.");
}
else
{
Console.WriteLine("모두 출석하지 않았습니다.");
}
}
}
}
using System;
using System.Collections.Generic;
namespace Study09
{
public delegate void DelCheck(bool b);
public class RollBook
{
public DelCheck delCheck;
private List<Student> list;
public RollBook()
{
Random r = new Random();
list = new List<Student>();
list.Add(new Student("홍길동", r.Next(1, 101)));
list.Add(new Student("임꺽정", r.Next(1, 101)));
list.Add(new Student("장길산", r.Next(1, 101)));
}
public void Check()
{
int attand = 0;
foreach (var i in list)
{
if (i.AttendProb > 10)
{
attand++;
Console.WriteLine("{0} 출석완료.", i.Name);
}
else
{
Console.WriteLine("{0} 결석.", i.Name);
}
}
delCheck(attand >= list.Count ? true : false);
}
}
}
using System;
namespace Study09
{
public class Student
{
private string name;
private int attendProb;
public string Name
{
get { return name; }
set { name = value; }
}
public int AttendProb
{
get
{
return attendProb;
}
private set
{
attendProb = value;
}
}
public Student(string name, int prob)
{
AttendProb = prob;
this.name = name;
}
}
}
3. 저그 플레이어의 오버로드가 한마리 죽어서 저그의 전체 인구수가 감소
- 오버로드 한 마리당 인구수는 8
- 골리앗이 오버로드를 공격함
using System;
namespace Study09
{
public class App
{
public int people = 26;
public App()
{
Goliath goliath = new Goliath();
Overlord overlord = new Overlord();
overlord.action = DecreasePeople;
while(overlord.Hp > 0)
{
goliath.AttackOverlord(overlord);
}
overlord.Die();
}
public void DecreasePeople()
{
people -= 8;
Console.WriteLine("인구 수 : {0}/{1}", people, people + 8);
}
}
}
using System;
namespace Study09
{
public class Overlord
{
private int hp;
private int maxHp;
private const int offerPeople = 8;
public Action action;
public int Hp { get => hp; set => hp = value; }
public Overlord()
{
maxHp = Hp = 200;
}
public void Damaged(int damage)
{
Hp -= damage;
if (Hp <= 0) hp = 0;
Console.WriteLine("오버로드의 체력 : {0}/{1}", Hp, maxHp);
}
public void Die()
{
Console.WriteLine("오버로드 사망");
action();
}
}
}
using System;
namespace Study09
{
public class Goliath
{
private int damage;
public Goliath()
{
Random r = new Random();
damage = 24 * (int)(r.NextDouble() + r.Next(1,5));
}
public void AttackOverlord(Overlord overlord)
{
Console.WriteLine("골리앗이 오버로드를 공격.");
overlord.Damaged(damage);
}
}
}