본문 바로가기

C#/과제

2020-04-10 과제 완료. Source code

과제 Source code

--------------------------------------------------------------------------------------------------------------------

Item class 

--------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HomeWork
{
    class Item
    {
        //데이터
        public string itemName;
        public int itemDamage;
        public Item(string itemName, int itemDamage)
        {
            this.itemName = itemName;
            this.itemDamage = itemDamage;
 
        }
        //메서드
        //필요없음
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

Character class

-----------------------------------------------------------------------------------------------------------------------------------

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HomeWork
{
    class Character
    {
        //데이터
        public string userName; //캐릭터이름
        public int userHp;  // 캐릭터 체력
        public int userMaxHp; // 캐릭터 최대체력
        public int defaultDamage; // 기본데미지
 
        public bool isEquiped = false// 장비를 장착했는지 안했는지 확인.
 
        Item inventory; // 아이템이 있는지 없는지.
 
        public Character(string userName, int userHp, int defaultDamage) // 매개변수로 값을 한번에 받아서 출력.
        {
            this.userName = userName;               //매개변수 userName으로 값을 받아서 현재 클래스 userName으로 할당
            this.userHp = userHp;                   //매개변수 userHp으로 값을 받아서 현재 클래스 userHp로 할당
            this.userMaxHp = this.userHp;           //현재 클래스 userHp 값을 현재 클래스 userMaxHp로 할당
            this.defaultDamage = defaultDamage;     //매개변수 defaultDamage로 값을 받아서 현재 클래스 defaultDamage로 할당
 
            Console.WriteLine("캐릭터 명 : {0}"this.userName);
            Console.WriteLine("체력 : {0} / {1}"this.userHp, this.userMaxHp);
            Console.WriteLine("기본 데미지 : {0}"this.defaultDamage);
            Console.WriteLine("--------------------------------------");
 
        }
        //메서드
 
        public void Attack(Character target)
        {     
            Console.WriteLine("---------------------------------------------------------------------");
 
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("★★★★★★★" + "\"{0}\"" + " 님이 " + "\"{1}\""+ " 님을 공격했습니다.★★★★★★★"this.userName, target.userName);
            Console.ResetColor();
 
            Console.WriteLine("---------------------------------------------------------------------");
            
            target.Hit(this);
        }
 
        public void Hit(Character target)
        {
            if (this.isEquiped == true)     //공격 받으면 공격 받은 상대편의 무기를 강제로 해제 시킵니다.
            {
                this.ItemTakeOff(target);
            }
 
            target.Die(this);
 
            this.userHp -= target.defaultDamage; // 공격 받을때마다 데미지만큼 상대방 에너지 감소.
 
 
            if (this.userHp <= 0)   
            {
                this.userHp = 0;
 
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\"{0}\"" + " 님에게 " + "\"{1}\"" + " 님이 (-{2}) 만큼의 피해를 받았습니다. ", target.userName, this.userName, target.defaultDamage);
                Console.ResetColor();
 
                Console.WriteLine("---------------------------------------------------------------------");
 
 
 
                Console.WriteLine("\"{0}\"" + " 님의 현재 체력 {1} / {2} | {3}님의 현재 체력 {4} / {5} "
                    , this.userName, this.userHp, this.userMaxHp, target.userName, target.userHp, target.userMaxHp);
                Console.WriteLine("---------------------------------------------------------------------");
 
            }
            else if (this.userHp > 0)
            {
 
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\"{0}\"" + " 님에게 " + "\"{1}\"" + " 님이 (-{2}) 만큼의 피해를 받았습니다.", target.userName, this.userName, target.defaultDamage);
                Console.ResetColor();
 
                Console.WriteLine("---------------------------------------------------------------------");
                
 
 
                Console.WriteLine("---------------------------------------------------------------------");
                Console.WriteLine("\"{0}\"" + " 님의 현재 체력 {1} / {2} | {3}님의 현재 체력 {4} / {5} "
                    , this.userName, this.userHp, this.userMaxHp, target.userName, target.userHp, target.userMaxHp);
                Console.WriteLine("---------------------------------------------------------------------");
            } 
 
        }
 
        public void GetItem(Item item)  //생성된 아이템값을 매개변수로 받아 현재 클래스 inventory에 할당해줍니다. 
        {
            this.inventory = item;
            Console.WriteLine("{0}가 {1}를 획득했습니다.",this.userName, this.inventory.itemName);
        }
 
        public void ItemEquiped()  // 장착시 장착되었다고 출력하고, 아이템의 효과로 데미지합산값이 얼마인지 출력해줍니다.
        {
            if (isEquiped == false)
            {
                this.isEquiped = true;
                this.defaultDamage += this.inventory.itemDamage;
                Console.WriteLine("{0}님이 {1}를 장착합니다."this.userName, this.inventory.itemName);
 
                Console.Write("{0}님의 공격력이 {1}의 효과로 " ,this.userName,this.inventory.itemName);
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("(+{0})"this.inventory.itemDamage);
                Console.ResetColor();
 
                Console.WriteLine(" 만큼 올라갑니다.");
            }
            else
            {
                Console.WriteLine("아이템이 장착되어있습니다.");
            }
        }
 
        public void ItemTakeOff(Character target)   // 해제시 해제되었다고 출력하고, 아이템의 효과가 사라진 데미지값이 얼마인지 출력해줍니다.
        {
            if (isEquiped == true)
            { 
                this.defaultDamage -= this.inventory.itemDamage;
 
                Console.WriteLine("{0}님의 공격으로 인해 {1}님의 {2}이 해제됩니다.",target.userName, this.userName, this.inventory.itemName);
 
                Console.Write("{0}님의 공격력이 {1}의 효과가 사라져 ",this.userName, this.inventory.itemName);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("(-{0})"this.inventory.itemDamage);
                Console.ResetColor();
 
                Console.WriteLine(" 만큼 내려갑니다.");
 
                isEquiped = false;
            }
            else
            {
                Console.WriteLine("해제할 아이템이 없습니다.");
            }
        
        
        }
 
        public Item CreateNewItem() // 아이템을 생성합니다.
        {
            Item item = new Item("혈마검"20);
 
            Console.WriteLine("{0}님에 의해 {1}가 제작되었습니다." ,this.userName,item.itemName);
 
            return item;
        }
 
 
        public bool Die(Character winner)   // 체력이 0이되면 죽음과 동시에 승리자 이름을 출력합니다.
        {
            bool isDied = false;
            if (this.userHp == 0)
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("{0}님의 체력이 {1} / {2} 입니다. 사망하셨습니다.\n"this.userName, this.userHp, this.userMaxHp);
                Console.ResetColor();
 
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("☆☆☆☆☆☆{0}님이 승리하셨습니다.☆☆☆☆☆☆\n", winner.userName);
                Console.ResetColor();
 
                isDied = true;
            }
            return isDied;
 
        }
 
 
 
 
    }
 
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

Main Source

-----------------------------------------------------------------------------------------------------------------------------------

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HomeWork
{
    class App
    {
        //데이터
 
        public App()
        {
            Random rand = new Random();
 
            Item itemTable = null;
 
            Character hong = new Character("홍길동"10010);
            Character lim = new Character("임꺽정"1009);
 
            while (true)
            {
                int dice = rand.Next(17);
                
                if (dice == 1 || dice == 3 || dice == 5)
                {   
 
                    Console.WriteLine("------------------------------------------------");
                    Console.WriteLine("현재 {0}님의 데미지 {1}", hong.userName, hong.defaultDamage);
                    Console.WriteLine("------------------------------------------------");
 
 
                    if (hong.isEquiped == true)
                    {
                        hong.Attack(lim);
                    }
                    else
                    {
                        itemTable = hong.CreateNewItem();
                        hong.GetItem(itemTable);
                        hong.ItemEquiped();
                        hong.Attack(lim);
                    }
 
                    if (lim.Die(hong) == true)
                    {
                        break;
                    }
                    else
                    {
                        continue;
                    }
 
                }
 
 
                if (dice == 2 || dice == 4 || dice == 6)
                {
 
                    Console.WriteLine("------------------------------------------------");
                    Console.WriteLine("현재 {0}님의 데미지 {1}", lim.userName, lim.defaultDamage);
                    Console.WriteLine("------------------------------------------------");
 
                    if (lim.isEquiped == true)
                    {
                        lim.Attack(hong);
                    }
                    else
                    {
                        itemTable = lim.CreateNewItem();
                        lim.GetItem(itemTable);
                        lim.ItemEquiped();
                        lim.Attack(hong);
 
                    }
                   
                    if (hong.Die(lim) == true)
                    {
                        break;
                    }
                    else
                    {
                        continue;  
                    }
 
                }
            }
 
        }
 
        //메서드
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

과제명 : 서로 아이템을 빨리 만들어서 때리는 게임 

-----------------------------------------------------------------------------------------------------------------------------------
일단 아래  변수 또는 메서드 이름 정하기
-----------------------------------------------------------------------------------------------------------------------------------
Character
캐릭터 

두명 A , B

이름, 체력, 최대체력, 기본 공격력, 아이템
userName, userHp, userMaxHp, defaultDamage, inventory

타겟을 공격 할 수 있다. Attack

타겟에게 피해를 받을 수 있다. Hit
피해를 받는다면 타겟의 공격만큼 체력을
감소 시킨다.
만약에 체력이 0보다 같거나 작다면 죽어야한다.


아이템 획득 할 수 있다.- GetItem

아이템 제작 할 수 있다.- CreateNewItem

아이템 착용 할 수 있다.- ItemEquiped

아이템 해제 할 수 있다.- ItemTakeOff

죽을 수 있다. -Die

-----------------------------------------------------------------------------------------------------------------------------------
Item
아이템

이름 - itemName
공격력 - itemDamage
-----------------------------------------------------------------------------------------------------------------------------------

 

@@@@@@추가요소

게임의 개요

-두 캐릭터 다 무기를 만들 수 있고,

주사위를 굴려 1,3,5 가 나오면 첫번째 플레이어가 무기를 만들어 두번째 플레이어를 공격합니다. 

주사위를 굴려 2,4,6 이 나오면 두번째 플레이어가 무기를 만들어 첫번째 플레이어를 공격합니다.

 

공격시 타격받은 상대편이 만약 무기를 가지고 있다면, 강제로 해제시킵니다.

무기가 강제로 해제된 상대편은 주사위를 굴려 다시 아이템을 만들어야합니다.

 

+색상을 넣어 가독성 더좋게

+무기 착용 또는 해제시, 데미지가 얼마나 올랐는지 이벤트마다 출력해줍니다.

+확률 밸런스를 위해 주사위방식으로 바꿈.

+만약내가 무기를 장착하고 있다면, 타격 받을시 해제되는 기능을 넣음

+둘중 한캐릭터가 사망시, 승리자를 출력해줌.

 

-----------------------------------------------------------------------------------------------------------------------------------

출력값

-----------------------------------------------------------------------------------------------------------------------------------

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

2020-04-17 과제  (0) 2020.04.19
2020-04-14 과제  (0) 2020.04.15
2020-04-10 과제 문제 및 진행단계 기록  (0) 2020.04.10
2020-04-03 과제  (0) 2020.04.03
2020.04.02 과제  (0) 2020.04.02