배열 - 캐릭터의 무기 습득 및 장착
2020. 4. 20. 16:05ㆍC#/수업내용
<Character.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
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_012
{
class Character
{
public CharacterData data;
public Weapon[] weapon;
public int index;
public int maxArrayIndex;
public Weapon equipedWeapon;
public bool isEquip;
public Character(CharacterData characterData)
{
maxArrayIndex = 5;
this.weapon = new Weapon[maxArrayIndex];
this.data = characterData;
this.index = 0;
this.isEquip = false;
}
public void GetWeapon(Weapon weapon)
{
if (index < maxArrayIndex)
{
this.weapon[index] = weapon;
this.index++;
}
else
{
Weapon[] expansionWeapon = new Weapon[maxArrayIndex * 2];
this.weapon = expansionWeapon;
this.weapon[index] = weapon;
this.index++;
}
}
public void GetWeapon(Weapon[] weapon)
{
{
foreach (Weapon element in weapon)
{
this.weapon[index] = element;
this.index++;
}
}
else
{
Weapon[] expansionWeapon = new Weapon[maxArrayIndex * 2];
this.weapon = expansionWeapon;
foreach (Weapon element in weapon)
{
this.weapon[index] = element;
this.index++;
}
}
}
public void PrintWeaponNames()
{
foreach (Weapon element in this.weapon)
{
if (element != null)
}
}
public Weapon[] GetAllWeapons()
{
Weapon[] weapons = new Weapon[index];
weapons = weapon;
return weapons;
}
public Weapon SearchWeapons(Weapon[] weapons, string weaponName)
{
{
{
return weapon[i];
}
}
Console.WriteLine($"검색 결과가 없습니다.");
return null;
}
public void Equip(Weapon weapon)
{
if (this.isEquip == false)
{
this.equipedWeapon = weapon;
weapon.isEquiped = true;
this.isEquip = true;
}
else
{
this.equipedWeapon.isEquiped = false;
this.equipedWeapon = weapon;
weapon.isEquiped = true;
this.isEquip = true;
}
}
public void Equip(string weapon)
{
{
if(this.weapon[i] != null)
{
{
if (this.isEquip == false)
{
this.equipedWeapon = this.weapon[i];
this.weapon[i].isEquiped = true;
this.isEquip = true;
return;
}
else
{
this.equipedWeapon.isEquiped = false;
this.equipedWeapon = this.weapon[i];
this.weapon[i].isEquiped = true;
this.isEquip = true;
return;
}
}
}
}
Console.WriteLine($"{weapon} 을(를) 찾지 못했습니다.");
}
}
}
|
<CharacterData.cs>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_012
{
class CharacterData
{
public string name;
public int hp;
public int damage;
public CharacterData(string name, int hp, int damage)
{
this.name = name;
this.hp = hp;
this.damage = damage;
}
}
}
|
<Weapon.cs>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_012
{
class Weapon
{
public WeaponData data;
public bool isEquiped;
public Weapon(WeaponData data)
{
this.data = data;
this.isEquiped = false;
}
}
}
|
<WeaponData.cs>
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_012
{
class WeaponData
{
public string name;
public int damage;
public WeaponData(string name, int damage)
{
this.name = name;
this.damage = damage;
}
}
}
|
<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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_012
{
class App
{
public App()
{
CharacterData data = new CharacterData("홍길동", 100, 10);
Character hong = new Character(data);
WeaponData weaponData = new WeaponData("장검", 10);
Weapon weapon = new Weapon(weaponData);
hong.GetWeapon(weapon);
WeaponData weaponData1 = new WeaponData("단검", 8);
Weapon weapon1 = new Weapon(weaponData1);
hong.GetWeapon(weapon1);
Weapon[] hongWeapons = hong.GetAllWeapons();
hong.Equip("장검");
Weapon[] arrDropItems = new Weapon[5];
arrDropItems[0] = new Weapon(new WeaponData("창", 11));
arrDropItems[1] = new Weapon(new WeaponData("도끼", 15));
arrDropItems[2] = new Weapon(new WeaponData("활", 15));
arrDropItems[3] = new Weapon(new WeaponData("목검", 15));
arrDropItems[4] = new Weapon(new WeaponData("대검", 15));
hong.GetWeapon(arrDropItems);
hong.Equip("활");
hong.PrintWeaponNames();
}
}
}
|
'C# > 수업내용' 카테고리의 다른 글
업적 - Dictionary/File Read Write (0) | 2020.04.22 |
---|---|
Json을 이용하여 Excel 파일을 역직렬화 하기 (0) | 2020.04.21 |
배열의 복사(Deep/Shallow) (0) | 2020.04.16 |
스타크래프트 - 배럭 (0) | 2020.04.14 |
인벤토리에 아이템 넣기 (0) | 2020.04.14 |