1. App class
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009
{
class App
{
public App()
{
Console.WriteLine("2020-04-14");
Barracks barracks = new Barracks();
Academy academy = new Academy();
barracks.academy = academy;
Unit marine = barracks.CreateUnit(UnitType.Marine);
Unit firebat = barracks.CreateUnit(UnitType.FireBat);
Unit medic = barracks.CreateUnit(UnitType.Medic);
Unit ghost = barracks.CreateUnit(UnitType.Ghost);
barracks.LiffOff();
barracks.LiffOff();
Barracks barracksHpState = marine.Attack(barracks);
Barracks barracksDestroy = barracks.Destory();
{
barracks = null;
Console.WriteLine("barracks이 파괴되었습니다.");
Console.WriteLine("barracks의 상태: {0} ", barracks);
}
}
}
}
|
2. Barracks class
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009
{
enum eLiftOffState //떠있는 상태
{
LiftOff,
Land //기본은 land
}
class Barracks
{
public string name; //이름
public int hp; //체력
eLiftOffState liftOffState; //상태
public int id;
int positionX;
int positionY;
public Academy academy; //아카데미 인스턴스
public Barracks()
{
this.name = "Barracks";
Console.WriteLine("\n{0}가 생성되었습니다.",this.name);
}
//초기화 함수(생성자와 달리 개발자가 원하는 시점에 초기화)
public void Init(int id, int hp)
{
this.id = id;
this.hp = hp;
}
//유닛 생성
public Unit CreateUnit(UnitType unitType)
{
if(this.academy==null)//아카데미가 없으면
{
{
Console.WriteLine("\n{0}을 생성합니다.", unitType); //마린
Unit marine = new Unit(unitType, 6, 40);
return marine;
}
else
{
Console.WriteLine("{0}을 생성할 수 없습니다.",unitType);
return null;
}
}
else//아카데미가 만들어졌으면
{
{
Console.WriteLine("{0}을 생성할 수 없습니다.",unitType);
return null;
}
{
Console.WriteLine("{0}을 생성합니다.", unitType); //마린
Unit marine = new Unit(unitType, 6, 40);
return marine;
}
else if (unitType == UnitType.FireBat)
{
Console.WriteLine("{0}을 생성합니다.", unitType); //파이어뱃
Unit firebat = new Unit(unitType, 16, 50);
return firebat;
}
{
Console.WriteLine("{0}을 생성합니다.", unitType); //메딕
Unit medic = new Unit(unitType, 0, 60);
return medic;
}
}
return null;
//return new Unit(unitType, 6, 40);// 마린
}
//현재 LiftOff상태 값을 반환
public eLiftOffState GetLiftOffState()
{
return this.liftOffState;
}
//띄운다(상태변경)
public void LiffOff()
{
this.liftOffState = eLiftOffState.LiftOff;
Console.WriteLine("{0}를 띄웁니다. ",this.name);
}
//착지 시킨다(상태변경)
public void Land()
{
Console.WriteLine("{0}가 착지합니다.",this.name);
}
//이동한다
public void Move(int positionX, int positionY)
{
this.positionX = positionX;
this.positionY = positionY;
{
Console.WriteLine("이동할 수 없습니다.");
}
else if(this.liftOffState==eLiftOffState.LiftOff)
{
Console.WriteLine("({0}),({1})로 이동합니다.",this.positionX,this.positionY);
}
}
//정지할 수 있다.
public void Stop()
{
Console.WriteLine("{0}이 정지합니다.",this.name);
}
//유닛으로부터 피해를 받을 수 있다
public void Hit(int barracksHp)
{
this.hp = barracksHp;
Console.WriteLine("체력이 {0}이 남았습니다.",this.hp);
}
//파괴될 수 있다
public Barracks Destory()
{
return this;
}
}
}
|
3. Unit class
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009
{
public enum UnitType
{
Marine, Medic, FireBat, Ghost
}
class Unit
{
int damage;
int hp;
UnitType unitType;
public Unit(UnitType unitType,int damage, int hp)
{
this.unitType = unitType;
this.damage = damage;
this.hp = hp;
}
public Barracks Attack(Barracks target)
{
return target;
}
}
}
|
4. Academy class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009
{
class Academy
{
public string name;
public Academy()
{
this.name = "Academy";
Console.WriteLine("{0}가 생성되었습니다.",this.name);
}
}
}
|
5. 결과값
'C# > 수업내용' 카테고리의 다른 글
2020.04.16. 수업내용 - 장바구니 (장바구니 클래스, 과일 클래스) (0) | 2020.04.16 |
---|---|
2020.04.16. 수업내용 - 문자열 배열 출력, 배열값 거꾸로 출력하기(foreach) (0) | 2020.04.16 |
2020.04.13. 수업내용 - Barracks, Unit class(enum) (0) | 2020.04.13 |
2020.04.13. 수업내용 - SiegeTank 클래스(enum), Factory 클래스 (0) | 2020.04.13 |
2020.04.09. 수업내용 - 물약 먹기(enum, switch) (0) | 2020.04.09 |