10일차 클래스 커맨트센터, 유닛(공격,피격)

Console Programming/C# Console 2019. 10. 4. 13:53

커맨드센터

----------------

이름:

----------------

유닛을만든다

 

 

유닛

-----------------

이름:SCV

체력:100

공격력:5

-----------------

공격한다

공격받는다.

 

 
Unit.cs
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax26
{
    class Unit
    {
        //데이터
        //이름
        //체력
        //공격력
        public string name;
        int maxHp = 100;
        int hp;
        int damage = 5;
        public Unit()
        {
            hp = maxHp;
        }
        //메서드
        //이동한다
        //공격한다
        public void Attack(Unit target)
        {
            target.Hit(this.damage, this.name);
        }
        public void Hit(int damage, string attackerName)
        {
            this.hp = this.hp - damage;
            Console.WriteLine($"{this.name}가 {attackerName}에게 공격받았습니다.  ({this.hp})/({this.maxHp})");
        }
       
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

28번 라인의 Target.Hit의 매개변수로 this를 써서 이 클래스 자체를 매개변수로 받아야하는데 

this.~~~ 이라고 써서 이름과 데미지를 따로 받아야하게 만들어졌다.

 

 

 
CommandCenter.cs
 
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.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax26
{
    class CommandCenter
    {
        //데이터
        //체력
        int hp;
       
        public CommandCenter()
        {
 
        }
        //메서드
        //유닛을 만든다.
 
        public Unit MakeUnit(string name)
        {
            Unit unit = new Unit();
            unit.name = name;
            Console.WriteLine(unit.name + "이(가) 생성되었습니다.");
            return unit;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 

 

 
App.cs
 
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
using System;
using System.Collections.Generic;
 
namespace Syntax26
{
    class App
    {
        public App()
        {
            CommandCenter commandCenter1 = new CommandCenter();
            Console.WriteLine(commandCenter1 + "이(가) 생성되었습니다. ");
 
            
            Unit unit1 = commandCenter1.MakeUnit("SCV1");
           
            Unit unit2 = commandCenter1.MakeUnit("SCV2");
 
            unit1.Attack(unit2);
 
        }
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 

 

: