몬스터와의 교전

2020. 4. 7. 12:38C#/수업내용

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
using System;
using System.Collections.Generic;
 
namespace Study_004
{
    class App
    {
        public App()
        {
            Console.WriteLine("2020-04-07");
 
            Console.Write("캐릭터의 이름은 무엇입니까? ");
            var playerName = Console.ReadLine();
            Console.WriteLine("\r");
 
            Console.Write("캐릭터의 공격력을 설정해주세요(1~5) ");
 
 
            var playerDamage = Int32.Parse(Console.ReadLine());
            Console.WriteLine("\r");
 
            for (; ; )        // 캐릭터 공격력 범위 체크
            {
                if (playerDamage > 0 && playerDamage <= 5)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("잘못된 명령어입니다.");
 
                    Console.Write("캐릭터의 공격력을 설정해주세요(1~5) ");
                    playerDamage = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("\r");
                }
            }
 
            Console.Write("몬스터의의 체력을 설정해주세요(10~20) ");
 
            double currentMonsterHp = 0;
            var maxMonsterHp = double.Parse(Console.ReadLine());
            Console.WriteLine("\r");
 
            for (; ; )        // 캐릭터 공격력
            {
                if (maxMonsterHp >= 10 && maxMonsterHp <= 20)
                {
                    currentMonsterHp = maxMonsterHp;
                    break;
                }
                else
                {
                    Console.WriteLine("잘못된 명령어입니다.");
 
                    Console.Write("몬스터의의 체력을 설정해주세요(10~20) ");
                    maxMonsterHp = double.Parse(Console.ReadLine());
                    Console.WriteLine("\r");
                }
            }
 
            for (; ; )
            {
                if (currentMonsterHp == 0)     // 몬스터를 이미 잡았을 경우의 탈출루트
                {
                    Console.WriteLine("몬스터가 아이템(장검)을 떨어뜨렸습니다.");
                    Console.Write("아이템을 획득 하시려면 '장검 집어'를 입력하세요.");
                    Console.WriteLine("\r");
 
                    var pharming = Console.ReadLine();
                    var command = pharming.Split(' ');
                    var item = command[0];
                    var acting = command[1];
 
                    if(item == "장검")
                    {
                        if(acting == "집어")
                        {
                            Console.WriteLine("장검을 획득했습니다.");
                            Console.WriteLine("게임을 종료합니다.");
                            break;
                        }
                        else
                        {
                            Console.WriteLine($"'{command[1]}' 이라는 명령어는 없습니다.");
                            Console.WriteLine("\r");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"'{command[0]}' 이라는 아이템은 없습니다.");
                        Console.WriteLine("\r");
                    }
                }
                else   // 몬스터와의 교전의 경우
                {
                    Console.WriteLine($"{playerName}이 입장 하셨습니다. (공격력 : {playerDamage})");
                    Console.WriteLine($"몬스터의 체력은 {currentMonsterHp} 입니다.");
                    Console.WriteLine("\r");
 
                    for (; ; )    // 게임 진행
                    {                        
                        Console.Write("몬스터를 공격 하시려면 '공격'이라고 입력하세요. ");
                        var command = Console.ReadLine();
 
                        if (command == "공격")     // 제대로 공격이 된 경우
                        {
                            Random rand = new Random();
                            int num = rand.Next(1, 101);
                            if(num<=50)             // 일반 공격
                            {
                                Console.WriteLine("당신은 몬스터를 공격했습니다.");
                                currentMonsterHp = currentMonsterHp - playerDamage;
                            }
                            else                    // 크리티컬 공격
                            {
                                Console.WriteLine("당신은 몬스터를 '크리티컬 히트'로 공격했습니다.");
                                currentMonsterHp = currentMonsterHp - (1.1 * playerDamage);
                            }                            
 
                            if (currentMonsterHp <= 0)   // 몬스터가 쓰러진경우
                            {
                                currentMonsterHp = 0;
                                Console.WriteLine("당신은 몬스터를 쓰러뜨렸습니다.");
                                Console.WriteLine("\r");
                                break;
                            }
                            else       // 아직 몬스터의 hp가 남아있는 경우
                            {
                                string parseCurrentMonsterHp = currentMonsterHp.ToString("N2");     // 소수점 둘째짜리 형식으로 변환
                                currentMonsterHp = double.Parse(parseCurrentMonsterHp);
 
                                Console.WriteLine($"몬스터의 체력 : {currentMonsterHp}/{maxMonsterHp}");
                                Console.WriteLine("\r");
                            }
                        }
                        else  // 제대로 공격이 되지 않은 경우
                        {
                            Console.WriteLine("잘못된 명령어입니다.");
                            Console.WriteLine("\r");
                        }
 
                    }
                }
            }
        }
    }
}
 

 

'C# > 수업내용' 카테고리의 다른 글

인벤토리에 아이템 넣기  (0) 2020.04.14
아이템 선택 및 강화  (0) 2020.04.09
#Enum - 캐릭터 생성 시 직업 및 종족 선택  (0) 2020.04.08
가위바위보 게임의 변형 - 묵찌빠  (0) 2020.04.06
2020-04-03  (0) 2020.04.03