상세 컨텐츠

본문 제목

Study_009 - 1 유닛, 배럭, 아카데미

C#/수업내용

by McRobbin 2020. 4. 14. 13:23

본문

현재까지의 구현 목록 입니다.

 

실행 결과 입니다.

 

현재까지 배운 내용으로는 상속, 배열 등을 사용할 수 없어 각 인스턴스를 하나씩 생성했고 enum 타입으로 현재 유닛의

타입을 하나하나 예외처리 하며 코드를 짰습니다.

 

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
using System;
using System.Collections.Generic;
 
namespace Study_009
{
    public class App
    {
        public App()
        {
            Console.WriteLine("2020-04-14\n=====================");
 
 
            //테란 배럭스 유닛 만들기 클래스 예제.
            Barracks barracks = new Barracks();
            Academy academy = null;
            barracks.LiftOff();
            Unit marine = barracks.SpawnUnit("Marine");
 
            barracks.Land();
 
            marine = barracks.SpawnUnit("Marine");
            Unit fireBat = barracks.SpawnUnit(2);
            Unit ghost = barracks.SpawnUnit((Unit.eBarracksUnitType)3);
            Unit medic = barracks.SpawnUnit("Medic");
 
            academy = new Academy(barracks);
            marine = barracks.SpawnUnit("Marine");
            fireBat = barracks.SpawnUnit(2);
            ghost = barracks.SpawnUnit((Unit.eBarracksUnitType)3);
            medic = barracks.SpawnUnit("Medic");
 
 
            barracks.Land();
            barracks.Move();
            barracks.LiftOff();
            barracks.Move();
 
            marine.Attack(barracks);
            medic.Attack(barracks);
            fireBat.Attack(barracks);
 
            marine.UseSteamPack(academy);
            medic.UseSteamPack(academy);
 
            academy.UpgradeSteamPack();
            academy.UpgradeSteamPack();
            marine.UseSteamPack(academy);
            medic.UseSteamPack(academy);
 
            fireBat.UseSteamPack(academy);
            fireBat.UseSteamPack(academy);
 
            medic.Heal(fireBat);
        }
    }
}
 
 
 

public App() 생성자의 내용 입니다.

UseSteamPack 부분이 아주 걸리는데 스팀팩 업그레이드 유무를 Academy가 static 변수로 가지고

있었으면 하지만 아직 배우지 않아 사용하지 않았고 아카데미를 넘겨줘서 업그레이드 유무를 확인했습니다.

 

아카데미의 존재 유무 역시 아카데미 안에서 static으로 가지고 있게 하고 싶지만 쓰지 않았고

아카데미를 생성할 때 배럭에 자신을 넘겨줘서 null 사용했습니다.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    public class Academy
    {
        public string Name;
        public int RemainHp;
        public int MaxHp;
        public bool hasSteamPack;
        public Academy(Barracks barracks)
        {
            this.Name = "Academy";
            this.RemainHp = this.MaxHp = 800;
            this.hasSteamPack = false;
            barracks.Academy = this;
            Console.WriteLine("{0}이 생성되었습니다."this.Name);
        }
 
        public void PrintInfo()
        {
            Console.WriteLine("{0}의 정보====================>"this.Name);
            Console.WriteLine("이름 : {0}, Hp : ({1} / {2}), 스팀팩 : {3}\n"this.Name, this.RemainHp, this.MaxHp, this.hasSteamPack);
        }
 
        public void Hit(Unit unit)
        {
            if (unit.UnitType == Unit.eBarracksUnitType.Medic)
            {
                Console.WriteLine("{0}은 공격할 수 없습니다.", unit.UnitType);
                return;
            }
 
            this.RemainHp -= unit.Damage;
            if (this.RemainHp <= 0)
                this.Destroy();
        }
 
        public void Destroy()
        {
            Console.WriteLine("{0}이 파괴되었습니다."this.Name);
        }
 
        public void UpgradeSteamPack()
        {
            if (!this.hasSteamPack)
            {
                this.hasSteamPack = true;
                Console.WriteLine("스팀팩 업그레이드가 완료되었습니다.");
            }
 
            else
            {
                Console.WriteLine("이미 업그레이드가 되었습니다.");
            }
        }
    }
}
 
 
 

 

Academy 클래스 코드 입니다. 배럭과 다른 점은 hasSteamPack bool타입 변수를 가지고 있어 스팀팩 사용 가능을알려줍니다. UpgradeSteamPack은 업그레이드가 되지 않았다면 bool 변수를 true로 바꿔줍니다.

 

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    public class Barracks
    {
        public enum eBarrackState
        {
            Land, LiftingOff
        }
 
        public string Name;
        public int RemainHp;
        public int MaxHp;
        public Academy Academy;
        public eBarrackState BarracksState;
 
        public Barracks()
        {
            this.Name = "Barracks";
            this.RemainHp = 1000;
            this.MaxHp = 1000;
            this.BarracksState = eBarrackState.Land;
            this.PrintInfo();
        }
 
        public void PrintInfo()
        {
            Console.WriteLine("{0}의 정보====================>"this.Name);
            Console.WriteLine("이름 : {0}, Hp : ({1} / {2}), State : {3}\n"this.Name, this.RemainHp, this.MaxHp, this.BarracksState);
        }
 
        public void LiftOff()
        {
            if (this.BarracksState == eBarrackState.Land) 
            {
                Console.WriteLine("{0}이 공중에 떴습니다."this.Name);
                this.BarracksState = eBarrackState.LiftingOff;
            }
            else if(this.BarracksState == eBarrackState.LiftingOff)
                Console.WriteLine("{0}이 이미 공중에 떠있습니다. "this.Name);
 
            else
            {
                Console.WriteLine("{0}을 공중에 띄울 수 없습니다. "this.Name);
            }
        }
 
        public void Land()
        {
            if (this.BarracksState == eBarrackState.LiftingOff)
            {
                this.BarracksState = eBarrackState.Land;
                Console.WriteLine("{0}을 땅에 내렸습니다."this.Name);
            }
 
            else if (this.BarracksState == eBarrackState.Land)
                Console.WriteLine("{0}이 이미 땅에 있습니다."this.Name);
 
            else
            {
                Console.WriteLine("{0}을 땅에 내릴 수 없습니다."this.Name);
            }
        }
 
        public void Move()
        {
            if(this.BarracksState == eBarrackState.LiftingOff)
            {
                Console.WriteLine("{0}이 움직였습니다. "this.Name);
            }
 
            else
            {
                Console.WriteLine("{0}을 움직일 수 없습니다."this.Name);
            }
        }
 
        public void Stop()
        {
            if (this.BarracksState == eBarrackState.LiftingOff)
            {
                Console.WriteLine("{0}이 정지했습니다. "this.Name);
            }
 
            else
            {
                Console.WriteLine("{0}을 정지할 수 없습니다."this.Name);
            }
        }
 
        public Unit SpawnUnit(int barracksUnitType)
        {
            if(this.BarracksState == (eBarrackState.LiftingOff))
            {
                Console.WriteLine("{0} 상태에서 유닛을 생성할 수 없습니다."this.BarracksState);
                return null;
            }
 
            switch ((Unit.eBarracksUnitType) barracksUnitType)
            {
                case Unit.eBarracksUnitType.Marine:
                    return new Unit((Unit.eBarracksUnitType)barracksUnitType);
                case Unit.eBarracksUnitType.FireBat:
                case Unit.eBarracksUnitType.Medic:
                    if (this.Academy == null)
                    {
                        Console.WriteLine("{0}을 만드려면 아카데미를 지어야 합니다.", (Unit.eBarracksUnitType)barracksUnitType);
                        return null;
                    }
                    else
                        return new Unit((Unit.eBarracksUnitType)barracksUnitType);
                    break;
                case Unit.eBarracksUnitType.Ghost:
                    Console.WriteLine("{0}를 생성할 수 없습니다.", (Unit.eBarracksUnitType)barracksUnitType);
                    return null;
                default:
                    break;
            }
            return null;
        }
 
        public Unit SpawnUnit(Unit.eBarracksUnitType unitType)
        {
            return this.SpawnUnit((int)unitType);
        }
 
        public Unit SpawnUnit(string unitName)
        {
            return this.SpawnUnit((Unit.eBarracksUnitType)Enum.Parse(typeof(Unit.eBarracksUnitType), unitName));
        }
 
        public void Hit(Unit byUnit)
        {
            if (byUnit.UnitType == (Unit.eBarracksUnitType)1)
            {
                Console.WriteLine("{0}은 공격할 수 없습니다.", byUnit.UnitType);
                return;
            }
 
            this.RemainHp -= byUnit.Damage;
            Console.WriteLine("{0}이 {1}에게 공격 받았습니다."this.Name, byUnit.UnitType);
            this.PrintInfo();
            if (this.isDestroy())
                this.Destroy();
        }
 
        public bool isDestroy()
        {
            return this.RemainHp <= 0;
        }
 
        public void Destroy()
        {
            Console.WriteLine("{0}이 파괴되었습니다. ");
        }
 
    }
}
 
 
 

Barracks 클래스 코드 입니다. 

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    public class Unit
    {
        public enum eBarracksUnitType
        {
            Marine, Medic, FireBat, Ghost
        }
 
        public int RemainHp;
        public int MaxHp;
        public int Armor;
        public int Damage;
        public int AttackSpeed;
        public bool canUseSteamPack;
 
        public eBarracksUnitType UnitType;
 
        public Unit(eBarracksUnitType unitType)
        {
            switch (unitType)
            {
                case eBarracksUnitType.Marine:
                    this.RemainHp = this.MaxHp = 40;
                    this.Armor = 0;
                    this.Damage = 5;
                    this.canUseSteamPack = true;
                    break;
 
                case eBarracksUnitType.Medic:
                    this.RemainHp = this.MaxHp = 60;
                    this.Armor = 0;
                    this.Damage = 0;
                    this.canUseSteamPack = false;
                    break;
 
                case eBarracksUnitType.FireBat:
                    this.RemainHp = this.MaxHp = 80;
                    this.Armor = 1;
                    this.Damage = 8;
                    this.canUseSteamPack = true;
                    break;
 
                case eBarracksUnitType.Ghost:
                    this.RemainHp = this.MaxHp = 50;
                    this.Armor = 1;
                    this.Damage = 6;
                    this.canUseSteamPack = false;
                    break;
 
                default:
                    break;
            }
 
            this.AttackSpeed = 1;
            
            this.UnitType = unitType;
            Console.WriteLine("{0}이 생성되었습니다."this.UnitType);
            this.PrintInfo();
        }
 
        public void PrintInfo()
        {
            Console.WriteLine("{0}의 정보=====================>"this.UnitType);
            Console.WriteLine("Hp : ({0} / {1}), 데미지 : {2}, 방어력 : {3}, 공격 속도 : {4}\n"this.RemainHp, this.MaxHp, this.Damage, this.Armor, this.AttackSpeed);
        }
 
        public void Attack(Barracks target)
        {
            target.Hit(this);
        }
 
        public void Attack(Unit target)
        {
            target.Hit(this);
        }
 
        public void Hit(Unit unit)
        {
            if (unit.UnitType == (eBarracksUnitType)1)
            {
                Console.WriteLine("{0}은 공격할 수 없습니다.", unit.UnitType);
                return;
            }
 
            Console.WriteLine("{0}이 {1}을 공격했습니다.", unit.UnitType, this.UnitType);
            this.RemainHp -= (unit.Damage - this.Armor);
            this.PrintInfo();
            if (isDie())
                this.Die();
        }
 
        public void Move()
        {
            Console.WriteLine("{0}이 이동했습니다."this.UnitType);
        }
 
        public void Stop()
        {
            Console.WriteLine("{0}이 정지했습니다. "this.UnitType);
        }
 
        public bool isDie()
        {
            return this.RemainHp <= 0;
        }
 
        public void Die()
        {
            Console.WriteLine("{0}이 죽었습니다. "this.UnitType);
        }
 
        public void UseSteamPack(Academy academy)
        {
            if (academy == null)
                Console.WriteLine("스팀팩을 사용할 수 없습니다.");
            else if (academy.hasSteamPack)
            {
                switch (this.UnitType)
                {
                    case eBarracksUnitType.Marine:
                    case eBarracksUnitType.FireBat:
                        Console.WriteLine("{0}이 스팀팩을 사용했습니다."this.UnitType);
                        if (this.RemainHp <= 10)
                            this.RemainHp -= 1;
 
                        else
                            this.RemainHp -= 10;
 
                        this.AttackSpeed += 1;
                        this.PrintInfo();
                        break;
                    default:
                        Console.WriteLine("{0}은 스팀팩을 사용할 수 없는 유닛입니다."this.UnitType);
                        break;
                }
            }
        }
 
        public void Heal(Unit target)
        {
            if (this.UnitType == eBarracksUnitType.Medic)
            {
                target.RemainHp += 10;
                if (target.RemainHp > target.MaxHp)
                    target.RemainHp = target.MaxHp;
                Console.WriteLine("{0}이 {1}을 회복시켰습니다."this.UnitType, target.UnitType);
                target.PrintInfo();
            }
 
            else
                Console.WriteLine("{0}은 힐을 사용할 수 없습니다.", target.UnitType);
        }
    }
}
 
 
 

Unit 클래스 코드 입니다.

 

Attack, Heal, UseSteamPack 등의 기능은 사용 가능한 유닛 종류가 따로 있습니다. 지금은 상속 등의 기능을 따로 사용할

수 없어서 if, switch문으로 일일히 확인하며 실행 했습니다.

'C# > 수업내용' 카테고리의 다른 글

Study_010 - 과일, 과일바구니 클래스 배열  (0) 2020.04.16
Study_009 -2 아이템 인벤토리 배열  (0) 2020.04.14
Study_008 클래스 예제  (0) 2020.04.13
Study_006-1  (0) 2020.04.09
Study_005  (0) 2020.04.08

관련글 더보기