'delegate 활용'에 해당되는 글 1건

  1. 2019.11.26 11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate)

11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate)

C#/실습 2019. 11. 26. 17:40

코드:

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
namespace _11._26_study2
{
    class App
    {
        private delegate void Del(Monster monster, Action<Monster> action);
        Action<Monster> monsterHit;
        Action<Hero, Monster> attackAction;
        public App()
        {
            var hero = new Hero(10);
            var monster = new Monster(100);
            monster.onDie = () => { Console.WriteLine("몬스터가 죽었습니다. hp:{0}"monster.hp); };
            //monsterHit = (monster) =>
            //{
            //    Console.WriteLine("몬스터의 체력이 감소되었습니다. 몬스터의 현재체력: {0}", monster.hp);
            //    Thread.Sleep(300);
            //    if (monster.hp > 0)
            //    {
            //        hero.Attack(monster, monsterHit);
            //    }
            //    else
            //    {
            //        monster.onDie(); //얘죽음
            //    }
            //};
            //hero.Attack(monster, monsterHit);
            //**********************************************
            //모든 과정을 App을 통해 전달:
 
            //어떻게 맞는지 정의
            monster.GetDamageAc = (damage) =>
            {
               monster.GetDamage(damage);
                Console.WriteLine("몬스터가 피격당했습니다. 몬스터 현재체력: {0}"monster.hp); //얘 맞음
            };
 
            //어떻게 때리는지 정의
            hero.iDamaged = (damage) =>
            {
                Console.WriteLine("영웅이 때렸습니다. 데미지:{0}", damage);
                monster.GetDamageAc(damage); //너 맞으래
            };
            //얘네 서로 어떻게싸우는지 정의
            attackAction = (hero, monster) =>
            {
                if(monster.hp>0)
                {
                    Thread.Sleep(300);
                    hero.iDamaged(hero.damage); //얘가 때린다
                    AttackUntilDie(hero, monster, attackAction);
                }
                else
                {
                    monster.onDie();
                }
            };
            AttackUntilDie(hero, monster, attackAction);
            
        }
        public void AttackUntilDie(Hero hero, Monster monster, Action<Hero, Monster> attackAction)
        {
            attackAction(hero, monster); //얘네 서로 싸운다
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace _11._26_study2
{
    class Hero
    {
        public int damage;
        public Action<int> iDamaged;
        public Hero(int damage)
        {
            this.damage = damage;
        }
        //public void Attack(Monster monster, Action<Monster> attackComplete)
        //{
        //    //몬스터의 체력을 감소시킨다
        //    monster.hp -= 10;
        //    attackComplete(monster);
            
        //}
        public void BlindAttack()
        {
            iDamaged(this.damage);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
31
using System;
using System.Collections.Generic;
using System.Text;
 
namespace _11._26_study2
{
    class Monster
    {
        public int hp;
        public Action onDie;
        public Action<int> GetDamageAc;
        public Monster(int hp)
        {
            this.hp = hp;
        }
        public void MyHpIsZero()
        {
            if(this.hp <=0)
            {
                onDie(); //나 죽었소
            }
            
        }
        public void GetDamage(int damage)
        {
            this.hp -= damage;
            GetDamageAc(hp); // 나 맞았소
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

출력:

 

 

: