상세 컨텐츠

본문 제목

홍길동과 임꺽정의 전투 클래스 사용.

C#/과제

by McRobbin 2020. 4. 10. 18:46

본문

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Homework_001
{
    public class App
    {
        public App() 
        {
            Console.WriteLine("클래스 예제 과제\n=========================================================\n");
 
            Character hong = new Character();
            hong.name = "홍길동";
            
            hong.GetItem(new Item("혈마검"10));
            Item CreateItem = hong.CreateItem("혈마검(+4)"15);
            hong.EquipItem(CreateItem);
 
            hong.TakeOffItem();
            hong.EquipItem(new Item("목검"3));
 
 
            Character lim = new Character("임꺽정"120);
            Item limCreate = lim.CreateItem("부러진 혈마검"6.5f);
            lim.EquipItem(limCreate);
 
            lim.AttackTarget(hong);
            hong.AttackTarget(lim);
 
            lim.HitByTarget(hong);
            hong.HitByTarget(lim);
 
            Item limGetItem = new Item("누군가 버린 혈마검(+4)"15);
            lim.GetItem(limGetItem);
            lim.EquipItem(limGetItem);
            lim.AttackTarget(hong);
 
            hong.HitByTarget(lim);
 
 
            limCreate = lim.CreateItem("누군가 버린 혈마검(+10)"40);
            lim.EquipItem(limCreate);
            lim.AttackTarget(hong);
            hong.HitByTarget(lim);
        }
    }
}
 
 
 

class App 안의 생성자 public 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Homework_001
{
    public class Character
    {
        public string name;
        public float hp;
        public float maxHp;
        public float damage;
        public Item EquipedItem;
        public Character()
        {
            //기본 공격력 : 1 후에 무기의 데미지와 합쳐 최종 데미지 계산.
            this.hp = this.maxHp = 100;
            this.damage = 1.0f;
            this.EquipedItem = new Item();
            this.PrintInfo();
        }
 
        public Character(string name, float maxHp)
        {
            this.name = name;
            this.hp = this.maxHp = maxHp;
            this.damage = 1.0f;
            this.EquipedItem = new Item();
            this.PrintInfo();
        }
 
        public void GetItem(Item item)
        {
            Console.WriteLine("{0}이 아이템을 획득했습니다."this.name);
            item.PrintInfo();
        }
 
        public void EquipItem(Item item)
        {
            this.EquipedItem = item;
            Console.WriteLine("{0}이 아이템을 착용했습니다."this.name);
            this.PrintInfo();
 
        }
 
        public Item CreateItem(string name, float damage)
        {
            Item created = new Item(name, damage);
            Console.WriteLine("{0}이 아이템을 만들었습니다."this.name);
            created.PrintInfo();
 
            return created;
        }
 
        public void TakeOffItem()
        {
            Console.WriteLine("{0}이 {1}아이템을 벗었습니다."this.name, this.EquipedItem.name);
            this.EquipedItem = new Item();
            this.PrintInfo();
        }
 
        public float TotalDamage()
        {
            return this.EquipedItem.damage + this.damage;
        }
 
        public void PrintInfo()
        {
            Console.WriteLine("캐릭터 이름 : {0}, 캐릭터 총 데미지 : {1}, 체력 : ({2} / {3})",
            this.name, this.TotalDamage(), this.hp, this.maxHp);
            Console.Write("착용 무기 == ");
            EquipedItem.PrintInfo();
            Console.WriteLine("");
        }
 
        public void AttackTarget(Character target)
        {
            target.hp -= this.TotalDamage();
            Console.WriteLine("{0}가 {1}을 공격했습니다."this.name, target.name);
            this.PrintInfo();
            target.PrintInfo();
            if (target.hp <= 0)
                target.Die();
        }
 
        public void Die() => Console.WriteLine("{0}이 죽었습니다. "this.name);
 
        public void HitByTarget(Character target)
        {
            target.AttackTarget(this); 
        }
 
    }
}
 
 
 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Homework_001
{
    public class Item
    {
        public string name;
        public float damage;
 
        public Item()
        {
            //맨주먹 구현.
            this.name = "맨주먹";
            this.damage = 0.0f;
        }
 
        public Item(string name, float damage)
        {
            this.name = name;
            this.damage = damage;
        }
 
        public void PrintInfo() => Console.WriteLine("이름 : {0}, 공격력 : {1}\n"this.name, this.damage);
    }
}
 
 
 

Item코드 입니다.

 

과제 마치겠습니다. 배끼지 마세요!!

긁어다가 쓰지 마세요!!!!!

직접 타이핑 하면 인정.

실행 결과 올렸습니다.

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

2048 콘솔 게임.  (0) 2020.05.01
과제 - 롤 챔프, 스킨 구매 시뮬레이션..?  (0) 2020.04.30
레시피 과제.  (0) 2020.04.17
과제 04-03  (0) 2020.04.03
과제 04-02  (0) 2020.04.02

관련글 더보기