캐릭터간 대전

2020. 4. 10. 18:50C#/과제

<컴파일 실행 화면>

 

<Class App>

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_007
{
    class App
    {
        public App()
        {
            Character hong = new Character("홍길동"1001005);
            Character lim = new Character("임꺽정"1001005);   
 
            while(true)
            {
                Console.Write("조작할 캐릭터의 이름을 입력하세요. ");
                string player = Console.ReadLine();
                Console.Write("캐릭터가 취할 행동을 입력하세요. (제작, 획득, 착용, 해제, 공격) ");
                string action = Console.ReadLine();
                
                if(player == hong.name)         // 홍길동의 턴
                {
                    if(action == "제작")
                        hong.item = hong.CreateItem("단검"10);
                    else if(action == "획득")
                    {
                        hong.GetItem(hong.item);
                    }
                    else if(action == "착용")
                    {
                        hong.EquipItem(hong.item);
                    }
                    else if(action == "해제")
                    {
                        hong.ReleaseItem(hong.item);
                    }
                    else if(action == "공격")
                    {
                        hong.Attack(lim);
                    }
                }
                else if(player == lim.name)     // 임꺽정의 턴
                {
                    if(action == "제작")
                        lim.item = lim.CreateItem("장검"20);
                    else if (action == "획득")
                    {
                        lim.GetItem(lim.item);
                    }
                    else if (action == "착용")
                    {
                        lim.EquipItem(lim.item);
                    }
                    else if (action == "해제")
                    {
                        lim.ReleaseItem(lim.item);
                    }
                    else if (action == "공격")
                    {
                        lim.Attack(hong);
                    }
                }
            }   
        }
    }
}
 

<Class Character>

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_007
{
    class Character
    {
        public string name;
        public int hp;
        public int maxHp;
        public int damage;       
        public Item item;
        public bool isItem;
        public bool isEquipment;
 
        public Character(string name, int hp, int maxHp, int basicDamage)
        {
            this.name = name;
            this.hp = hp;
            this.maxHp = maxHp;
            this.damage = basicDamage;
            this.isItem = false;
            this.isEquipment = false;
 
            Console.WriteLine($"\"{this.name}\" 캐릭터를 생성했습니다.");
        }
                
        public Item CreateItem(string itemName, int itemDamage)
        {
            item = new Item();
            item.name = itemName;
            item.damage = itemDamage;
            Console.WriteLine($"\"{item.name}\"을 생성했습니다.");
            return item;
        }
 
        public void GetItem(Item itemName)
        {
            if (isItem == true)
                Console.WriteLine("인벤토리가 가득 찼습니다. ");
            else
            {               
                isItem = true;
                Console.WriteLine($"{itemName.name} 을 획득했습니다..");
            }
        }
 
        public void EquipItem(Item itemName)
        {
            if (this.isEquipment == true)
            {
                Console.WriteLine($"이미 장비를 착용하고 있습니다.");
            }
            else if(this.isItem == false)
            {
                Console.WriteLine("착용할 아이템이 없습니다.");
            }
            else if(isItem == true && isEquipment == false)
            {
                this.isEquipment = true;
                item = itemName;
 
                this.isItem = false;
                this.damage = this.damage + itemName.damage;
 
                Console.WriteLine($"{this.name}님이 \"{this.item.name}\"을 착용했습니다.");
            }            
        }
        public void ReleaseItem(Item itemName)
        {
            if(this.isEquipment == true)
            {
                this.isEquipment = false;
                this.isItem = true;
                this.damage -= this.item.damage;                
 
                Console.WriteLine($"{this.name}님이 \"{itemName.name}\"을 해제했습니다."); 
            }
            else
            {
                Console.WriteLine($"해제할 아이템이 없습니다.");
            }
        }
        public void Attack(Character target)
        {
            Console.WriteLine($"{this.name}이 {target.name}을 공격했습니다.");
            target.Hit(this);
        }
        public void Hit(Character target)
        {
            this.hp -= target.damage;       // 체력 감소
 
            if(this.hp <= 0)    // 사망
            {
                this.Death();
            }
            else
            {                
                Console.WriteLine($"{target.name} 으로부터 {this.name} 이(가) 공격 받았습니다. {this.hp}/{this.maxHp}");
            }
        }
        public void Death()
        {
            Console.WriteLine($"{this.name}이(가) 사망하였습니다.");
            Console.WriteLine("게임이 종료됩니다.");
        }
    }
}
 

 

<Class Item>

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_007
{
    class Item
    {
        public string name;
        public int damage;
        public Item()
        {
 
        }
        public Item(string itemName)
        {
            this.name = itemName;
            Console.WriteLine($"\"{this.name}\" 이 생성되었습니다.");
        }
        public Item(string itemName, int damage)        // Weapons의 경우
        {
            this.name = itemName;
            this.damage = damage;
            Console.WriteLine($"\"{this.name}\"이 생성되었습니다.");
            Console.WriteLine($"공격력은 \"{this.damage}\"입니다..");
        }    
    }
}
 
 

 

 

 

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

객체지향이란  (0) 2020.04.22
레시피 관리 및 요리하기  (0) 2020.04.20
인벤토리  (0) 2020.04.14
2020-04-03  (0) 2020.04.03
2020-04-02 과제  (0) 2020.04.02