배열 - 캐릭터의 무기 습득 및 장착

2020. 4. 20. 16:05C#/수업내용

<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;
using System.Threading.Tasks;
 
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;
                Console.WriteLine($"{this.weapon[index].data.name} 을(를) 획득했습니다.");
                this.index++;
            }
            else
            {
                Weapon[] expansionWeapon = new Weapon[maxArrayIndex * 2];
                Array.Copy(this.weapon, expansionWeapon, this.weapon.Length);
                this.weapon = expansionWeapon;
 
                this.weapon[index] = weapon;
                Console.WriteLine($"{this.weapon[index].data.name} 을(를) 획득했습니다.");
                this.index++;
            }
        }
        public void GetWeapon(Weapon[] weapon)
        {
            if (index + weapon.Length < maxArrayIndex)
            {
                foreach (Weapon element in weapon)
                {
                    this.weapon[index] = element;
                    Console.WriteLine($"{this.weapon[index].data.name} 을(를) 획득했습니다.");
                    this.index++;
                }
            }
            else
            {
                Weapon[] expansionWeapon = new Weapon[maxArrayIndex * 2];
                Array.Copy(this.weapon, expansionWeapon, this.weapon.Length);
                this.weapon = expansionWeapon;
 
                foreach (Weapon element in weapon)
                {
                    this.weapon[index] = element;
                    Console.WriteLine($"{this.weapon[index].data.name} 을(를) 획득했습니다.");
                    this.index++;
                }
            }
        }
        public void PrintWeaponNames()
        {
            Console.WriteLine($"<현재 \"{this.data.name}\" 이(가) 소지중인 무기 목록>");
            foreach (Weapon element in this.weapon)
            {
                if (element != null)
                    Console.WriteLine($"이름 : {element.data.name}, 착용여부 : {element.isEquiped}");
            }
        }
        public Weapon[] GetAllWeapons()
        {
            Weapon[] weapons = new Weapon[index];
            weapons = weapon;
            return weapons;
        }
        public Weapon SearchWeapons(Weapon[] weapons, string weaponName)
        {
            for (int i = 0; i < weapons.Length; i++)
            {
                if (weapons[i].data.name == weaponName)
                {
                    return weapon[i];
                }
            }
            Console.WriteLine($"검색 결과가 없습니다.");
            return null;
        }
        public void Equip(Weapon weapon)
        {            
            if (this.isEquip == false)
            {
                this.data.damage += weapon.data.damage;
                this.equipedWeapon = weapon;
                weapon.isEquiped = true;
                this.isEquip = true;
            }
            else
            {
                this.data.damage -= this.equipedWeapon.data.damage;
                this.equipedWeapon.isEquiped = false;
 
                this.data.damage += weapon.data.damage;
                this.equipedWeapon = weapon;
                weapon.isEquiped = true;
                this.isEquip = true;
            }
 
        }
        public void Equip(string weapon)
        {
            for (int i = 0; i < this.weapon.Length; i++)
            {
                if(this.weapon[i] != null)
                {
                    if (this.weapon[i].data.name == weapon)
                    {
                        if (this.isEquip == false)
                        {
                            this.data.damage += this.weapon[i].data.damage;
                            this.equipedWeapon = this.weapon[i];
                            this.weapon[i].isEquiped = true;
                            this.isEquip = true;
                            return;
                        }
                        else
                        {
                            this.data.damage -= this.equipedWeapon.data.damage;
                            this.equipedWeapon.isEquiped = false;
 
                            this.data.damage += this.weapon[i].data.damage;
                            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;
using System.Threading.Tasks;
 
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;
using System.Threading.Tasks;
 
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;
using System.Threading.Tasks;
 
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;
using System.Threading.Tasks;
 
namespace Study_012
{
    class App
    {
        public App()
        {
            CharacterData data = new CharacterData("홍길동"10010);
            Character hong = new Character(data);
 
            Console.WriteLine($"이름 : {hong.data.name}, 체력 : {hong.data.hp}, 공격력 : {hong.data.damage}");
 
            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(hong.SearchWeapons(hongWeapons, "단검"));
            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();
        }
    }
}