본문 바로가기
c#/과제

2020.04.10 과제 Character Class

by Luna_O 2020. 4. 12.

 

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_0410
{
    class App
    {
        public App()
        {
            Character hong = new Character("홍길동"1008);
 
            Character lim = new Character("임꺽정"10010);
 
            Item axe = hong.CreateItem("날도끼"5);
            hong.GetItem(axe);
            hong.EquipItem();
            hong.Attack(lim);
            lim.Attack(hong);
            hong.UnEquipItem();
           hong.EquipItem();
 
            while (true)
            {
                hong.Attack(lim);
 
                if (lim.hp <= 0)
                {
                    break;
                }
            }
 
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
 
namespace study_0410
{
    class Character
    {
        //데이터  멤버변수
        public string name; //이름
        public int hp;      //체력
        public int maxHp;   //full체력
        public int damage;  //공격력
        public Item item;   //소지 아이템
        bool equip = false//아이템 착용 여부
 
        //생성자 : 이름과 체력, 공격력을 가진 캐릭터를 만든다.
        public Character(string name, int maxHp, int damage)
        {  //전달받은 값을(매개변수) 멤버변수에 할당한다.
            this.name = name;
            this.maxHp = maxHp;
            this.hp = this.maxHp;
            this.damage = damage;
            Console.WriteLine("캐릭터가 생성되었습니다.");
            Console.WriteLine("Name : {0}, Hp : {1}, Damage : {2}"this.name, this.hp, this.damage);
            Console.WriteLine("");
        }
 
        //기능
        //메서드 매개변수
 
        //타겟을 공격한다. (타겟)
        public void Attack(Character target)
        {    //만약에 소지아이템이 기본값이 아닌 만들어져 있는 상태에 아이템을 착용하고 있으면 아이템으로 타겟을 공격을 한다. 
            if (this.item != null && equip == true)
            {
                Console.WriteLine("{0}님이 {1}님을 {2}(으)로 공격하였습니다."this.name, target.name, this.item.name);
                target.Hit(this); //타겟이 공격을 받으면 피해를 받는다
                Console.WriteLine("");
            }
            else
            {  //그 외에 기본으로 공격을 하면 맨손으로 공격한다.
                Console.WriteLine("{0}님이 {1}님을 맨손으로 공격하였습니다."this.name, target.name);
                target.Hit(this); //타겟이 공격을 받으면 피해를 받는다
                Console.WriteLine("");
            }
        }
        //타겟에게 피해를 받을 수 있다. (타겟)
        public void Hit(Character target)
        {   // 피해를 받는다면 타겟의 공격만큼 체력을 감소시킨다.
            this.hp -= target.damage;
            //만약에 체력이 0보다 같거나 작다면 체력을 0으로 만든다
            if (this.hp <= 0)
            {
                this.hp = 0;
            }
            Console.WriteLine("{0}님이 {1}님에게 공격(-{2})받았습니다.({3}/{4})"this.name, target.name, target.damage, this.hp, this.maxHp);
            //체력이 0 이라면 죽어야한다.
            if (this.hp == 0)
            {
                this.Die();
            }
        }
 
        // 죽을 수 있다.
        public void Die()
        {
            Console.WriteLine("");
            Console.WriteLine("{0}님이 죽었습니다."this.name);
        }
 
 
        //아이템을 제작한다.
        //Item class를 만들어 아이템의 이름과 공격력을 가진 새로운 아이템을 만든다.
        public Item CreateItem(string itemName, int itemDamage)
        {
            return new Item(this.name, itemName, itemDamage);   //만든 아이템을 메서드에 반환한다.
        }
        //아이템을 획득한다.
        public void GetItem(Item item1)
        {   //만든 아이템을 소지아이템 멤버변수에 할당한다.
            this.item = item1;
            Console.WriteLine("{0}님이 {1}를(을) 획득하였습니다."this.name, this.item.name);
            Console.WriteLine("");
        }
 
        //아이템을 착용한다.
        public void EquipItem()
        {   //만약에 소지아이템 멤버변수가 기본값이라면 착용할 아이템이 없다 출력
            if (this.item == null)
            {
                Console.WriteLine("착용할 아이템이 없습니다.");
                Console.WriteLine("");
            }
            //그렇지않고 만약에 아이템을 이미 착용한 상태라면 이미 착용하고 있다 출력
            else if (equip == true)
            {
                Console.WriteLine("{0}는(은) 이미 착용하고 있습니다."this.item.name);
                Console.WriteLine("");
            }
            else
            {   //그 외에는 기본적으로 착용을 한다 출력
                this.damage += this.item.damage; //아이템을 착용하는 것으로 기본공격력에 아이템 공격력을 더해준다.
                Console.WriteLine("{0}님이 {1}를(을) 착용하였습니다."this.name, this.item.name);
                equip = true;   //착용 여부를 판단하기 위해 착용변수를 true로 바꾼다.
                Console.WriteLine("");
            }
        }
 
        //아이템을 해제한다
        public void UnEquipItem()
        {
            //만약에 소지아이템 멤버변수가 기본값이라면 해제할 아이템이 없다 출력
            if (this.item == null || equip == false)
            {
                Console.WriteLine("해제할 아이템이 없습니다.");
                Console.WriteLine("");
            }
            
            else
            {
                this.damage -= this.item.damage;
                Console.WriteLine("{0}님이 {1}를(을) 착용 해제하였습니다."this.name, this.item.name);
                equip = false;
                Console.WriteLine("");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_0410
{
    public class Item
    {
        public string name;
        public int damage;
 
 
        public Item(string characterName ,string name, int damage)
        {
            this.name = name;
            this.damage = damage;
            Console.WriteLine("{0}님이 아이템을 제작하였습니다.", characterName);
            Console.WriteLine("Item Name : {0}, Damage : {1}."this.name, this.damage);
            Console.WriteLine("");
        }
    }
}
 
 

실행 결과

'c# > 과제' 카테고리의 다른 글

2020.05.01  (0) 2020.05.01
과제  (3) 2020.04.22
2020.04.08 과제 enum 캐릭터 생성  (0) 2020.04.08
2020.04.03 과제 * 모양 for문 이용 규칙성 출력  (0) 2020.04.05
2020.04.02 문자열 연결 연산자 "아이언맨" 과제  (0) 2020.04.02