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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
using namespace std;
 
// Text RPG
 
enum PlayerType
{
    PT_Knight = 1,
    PT_Archer = 2,
    PT_Mage = 3,
};
 
enum MonsterType
{
    MT_Slime = 1,
    MT_Orc = 2,
    MT_Skeleton = 3,
};
 
int playerType;
int hp;
int attack;
int defence;
 
int MonsterType;
int MonsterHp;
int MonsterAttack;
int MonsterDefence;
 
void EnterLobby();
void SelectPlayer();
void EnterField();
void CreateRandomMonster();
void EnterBattle();
 
int main()
{
    // 랜덤 시드 설정
    srand(time(0)); 
    EnterLobby();
 
    return 0;
}
 
void EnterLobby()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "로비에 입장했습니다!" << endl;
        cout << "--------------------" << endl;
 
        // 플레이어 직업 선택
        SelectPlayer();
 
        cout << "---------------------------" << endl;
        cout << "(1) 필드 입장 (2) 게임 종료" << endl;
        cout << "---------------------------" << endl;
 
        int input;
        cin >> input;
 
        if (input == 1)
        {
            EnterField();
        }
        else
        {
            return;
        }
    }
}
 
void SelectPlayer()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "직업을 골라주세요!" << endl;
        cout << "(1) 기사 (2) 궁수 (3) 법사" << endl;
        cout << "> " << endl;
 
        cin >> playerType;
        
        if (playerType == PT_Knight)
        {
            cout << "기사 생성중... !" << endl;
            hp = 150;
            attack = 10;
            defence = 5;
            break;
        }
        else if (playerType == PT_Archer)
        {
            cout << "궁수 생성중... !" << endl;
            hp = 100;
            attack = 15;
            defence = 3;
            break;
        }
        else if (playerType == PT_Mage)
        {
            cout << "마법사 생성중... !" << endl;
            hp = 80;
            attack = 25;
            defence = 0;
            break;
        }
    }
}
 
void EnterField()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "필드에 입장했습니다!" << endl;
        cout << "--------------------" << endl;
 
        cout << "[PLAYER] HP : " << hp << " / ATT : " << attack << "/ DEF : " << defence << endl;
 
        CreateRandomMonster();
 
        cout << "--------------------" << endl;
        cout << "(1) 전투 (2) 도주" << endl;
        cout << "> ";
 
        int input;
        cin >> input;
        
        if (input == 1)
        {
            EnterBattle();
            if (hp == 0)
            {
                return;
            }
        }
        else
        {
            return;
        }
 
    }
}
 
void CreateRandomMonster()
{
    MonsterType = 1 + (rand() % 3);  // 1, 2, 3
 
    switch (MonsterType)
    {
    case MT_Slime:
        cout << "슬라임 생성중... ! (ATT:15 / ATT:5 / DEF:0)" << endl;
        MonsterHp = 15;
        MonsterAttack = 5;
        MonsterDefence = 0;
        break;
    case MT_Orc:
        cout << "오크 생성중... ! (ATT:40 / ATT:10 / DEF:3)" << endl;
        MonsterHp = 40;
        MonsterAttack = 10;
        MonsterDefence = 3;
        break;
    case MT_Skeleton:
        cout << "스켈레톤 생성중... ! (ATT:80 / ATT:15 / DEF:5)" << endl;
        MonsterHp = 80;
        MonsterAttack = 15;
        MonsterDefence = 5;
        break;
    }
}
 
void EnterBattle()
{
    while (true)
    {
        int damage = attack - MonsterDefence;
        if (damage < 0)
            damage = 0;
 
        // 선빵
        MonsterHp -= damage;
        if (MonsterHp < 0)
            MonsterHp = 0;
        cout << "몬스터 남은 체력 : " << MonsterHp << endl;
 
        if (MonsterHp == 0)
        {
            cout << "몬스터를 처치했습니다!" << endl;
        }
        
        damage = MonsterAttack - defence;
        if (damage < 0)
            damage = 0;
 
        // 반격
        hp -= damage;
        if (hp < 0)
            hp = 0;
        cout << "플레이어 남은 체력 : " << hp << endl;
 
        if (hp == 0)
        {
            cout << "당신은 사망했습니다... GAME OVER" << endl;
            return;
        }
    }
}
cs

 

 

 

 

 

전방선언할 때 구조체로 묶어 ObjectInfo로 선언한다. player와 monster의 type, hp, attack, defence를 한 군데에 묶은 것이다. 이러면 변수 선언이 줄고 나중에 실수를 줄이기 좋다.   

몬스터의 체력이 0이 되어도 "몬스터를 처치했습니다!" 문구가 나오지 않는다. 이유는? 여기서 내가 한 실수는 무엇인가?

 

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
using namespace std;
 
// Text RPG
 
enum PlayerType
{
    PT_Knight = 1,
    PT_Archer = 2,
    PT_Mage = 3,
};
 
enum MonsterType
{
    MT_Slime = 1,
    MT_Orc = 2,
    MT_Skeleton = 3,
};
 
struct ObjectInfo
{
    int type;
    int hp;
    int attack;
    int defence;
};
 
ObjectInfo playerInfo;
 
ObjectInfo monsterInfo;
 
 
void EnterLobby();
void SelectPlayer();
void EnterField();
void CreateRandomMonster();
void EnterBattle();
 
int main()
{
    // 랜덤 시드 설정
    srand(time(0)); 
    EnterLobby();
 
    return 0;
}
 
void EnterLobby()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "로비에 입장했습니다!" << endl;
        cout << "--------------------" << endl;
 
        // 플레이어 직업 선택
        SelectPlayer();
 
        cout << "---------------------------" << endl;
        cout << "(1) 필드 입장 (2) 게임 종료" << endl;
        cout << "---------------------------" << endl;
 
        int input;
        cin >> input;
 
        if (input == 1)
        {
            EnterField();
        }
        else
        {
            return;
        }
    }
}
 
void SelectPlayer()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "직업을 골라주세요!" << endl;
        cout << "(1) 기사 (2) 궁수 (3) 법사" << endl;
        cout << "> " << endl;
 
        cin >> playerInfo.type;
        
        if (playerInfo.type == PT_Knight)
        {
            cout << "기사 생성중... !" << endl;
            playerInfo.hp = 150;
            playerInfo.attack = 10;
            playerInfo.defence = 5;
            break;
        }
        else if (playerInfo.type == PT_Archer)
        {
            cout << "궁수 생성중... !" << endl;
            playerInfo.hp = 100;
            playerInfo.attack = 15;
            playerInfo.defence = 3;
            break;
        }
        else if (playerInfo.type == PT_Mage)
        {
            cout << "마법사 생성중... !" << endl;
            playerInfo.hp = 80;
            playerInfo.attack = 25;
            playerInfo.defence = 0;
            break;
        }
    }
}
 
void EnterField()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "필드에 입장했습니다!" << endl;
        cout << "--------------------" << endl;
 
        cout << "[PLAYER] HP : " << playerInfo.hp << " / ATT : " << playerInfo.attack << "/ DEF : " << playerInfo.defence << endl;
 
        CreateRandomMonster();
 
        cout << "--------------------" << endl;
        cout << "(1) 전투 (2) 도주" << endl;
        cout << "> ";
 
        int input;
        cin >> input;
        
        if (input == 1)
        {
            EnterBattle();
            if (playerInfo.hp == 0)
            {
                return;
            }
        }
        else
        {
            return;
        }
 
    }
}
 
void CreateRandomMonster()
{
    monsterInfo.type = 1 + (rand() % 3);  // 1, 2, 3
 
    switch (monsterInfo.type)
    {
    case MT_Slime:
        cout << "슬라임 생성중... ! (ATT:15 / ATT:5 / DEF:0)" << endl;
        monsterInfo.hp = 15;
        monsterInfo.attack = 5;
        monsterInfo.defence = 0;
        break;
    case MT_Orc:
        cout << "오크 생성중... ! (ATT:40 / ATT:10 / DEF:3)" << endl;
        monsterInfo.hp = 40;
        monsterInfo.attack = 10;
        monsterInfo.defence = 3;
        break;
    case MT_Skeleton:
        cout << "스켈레톤 생성중... ! (ATT:80 / ATT:15 / DEF:5)" << endl;
        monsterInfo.hp = 80;
        monsterInfo.attack = 15;
        monsterInfo.defence = 5;
        break;
    }
}
 
void EnterBattle()
{
    while (true)
    {
        int damage = playerInfo.attack - monsterInfo.defence;
        if (damage < 0)
            damage = 0;
 
        // 선빵
        monsterInfo.hp -= damage;
        if (monsterInfo.hp < 0)
            monsterInfo.hp = 0;
        cout << "몬스터 남은 체력 : " << monsterInfo.hp << endl;
 
        if (monsterInfo.hp == 0)
        {
            cout << "몬스터를 처치했습니다!" << endl;
 
        }
        
        damage = monsterInfo.attack - playerInfo.defence;
        if (damage < 0)
            damage = 0;
 
        // 반격
        playerInfo.hp -= damage;
        if (playerInfo.hp < 0)
            playerInfo.hp = 0;
        cout << "플레이어 남은 체력 : " << playerInfo.hp << endl;
 
        if (playerInfo.hp == 0)
        {
            cout << "당신은 사망했습니다... GAME OVER" << endl;
            return;
        }
    }
}
cs

 

 

 

 

수정한 코드

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
using namespace std;
 
// Text RPG
 
enum PlayerType
{
    PT_Knight = 1,
    PT_Archer = 2,
    PT_Mage = 3,
};
 
enum MonsterType
{
    MT_Slime = 1,
    MT_Orc = 2,
    MT_Skeleton = 3,
};
 
struct ObjectInfo
{
    int type;
    int hp;
    int attack;
    int defence;
};
 
ObjectInfo playerInfo;
 
ObjectInfo monsterInfo;
 
 
void EnterLobby();
void SelectPlayer();
void EnterField();
void CreateRandomMonster();
void EnterBattle();
 
int main()
{
    // 랜덤 시드 설정
    srand(time(0)); 
    EnterLobby();
 
    return 0;
}
 
void EnterLobby()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "로비에 입장했습니다!" << endl;
        cout << "--------------------" << endl;
 
        // 플레이어 직업 선택
        SelectPlayer();
 
        cout << "---------------------------" << endl;
        cout << "(1) 필드 입장 (2) 게임 종료" << endl;
        cout << "---------------------------" << endl;
 
        int input;
        cin >> input;
 
        if (input == 1)
        {
            EnterField();
        }
        else
        {
            return;
        }
    }
}
 
void SelectPlayer()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "직업을 골라주세요!" << endl;
        cout << "(1) 기사 (2) 궁수 (3) 법사" << endl;
        cout << "> " << endl;
 
        cin >> playerInfo.type;
        
        if (playerInfo.type == PT_Knight)
        {
            cout << "기사 생성중... !" << endl;
            playerInfo.hp = 150;
            playerInfo.attack = 10;
            playerInfo.defence = 5;
            break;
        }
        else if (playerInfo.type == PT_Archer)
        {
            cout << "궁수 생성중... !" << endl;
            playerInfo.hp = 100;
            playerInfo.attack = 15;
            playerInfo.defence = 3;
            break;
        }
        else if (playerInfo.type == PT_Mage)
        {
            cout << "마법사 생성중... !" << endl;
            playerInfo.hp = 80;
            playerInfo.attack = 25;
            playerInfo.defence = 0;
            break;
        }
    }
}
 
void EnterField()
{
    while (true)
    {
        cout << "--------------------" << endl;
        cout << "필드에 입장했습니다!" << endl;
        cout << "--------------------" << endl;
 
        cout << "[PLAYER] HP : " << playerInfo.hp << " / ATT : " << playerInfo.attack << "/ DEF : " << playerInfo.defence << endl;
 
        CreateRandomMonster();
 
        cout << "--------------------" << endl;
        cout << "(1) 전투 (2) 도주" << endl;
        cout << "> ";
 
        int input;
        cin >> input;
        
        if (input == 1)
        {
            EnterBattle();
            if (playerInfo.hp == 0)
            {
                return;
            }
        }
        else
        {
            return;
        }
 
    }
}
 
void CreateRandomMonster()
{
    monsterInfo.type = 1 + (rand() % 3);  // 1, 2, 3
 
    switch (monsterInfo.type)
    {
    case MT_Slime:
        cout << "슬라임 생성중... ! (ATT:15 / ATT:5 / DEF:0)" << endl;
        monsterInfo.hp = 15;
        monsterInfo.attack = 5;
        monsterInfo.defence = 0;
        break;
    case MT_Orc:
        cout << "오크 생성중... ! (ATT:40 / ATT:10 / DEF:3)" << endl;
        monsterInfo.hp = 40;
        monsterInfo.attack = 10;
        monsterInfo.defence = 3;
        break;
    case MT_Skeleton:
        cout << "스켈레톤 생성중... ! (ATT:80 / ATT:15 / DEF:5)" << endl;
        monsterInfo.hp = 80;
        monsterInfo.attack = 15;
        monsterInfo.defence = 5;
        break;
    }
}
 
void EnterBattle()
{
    while (true)
    {
        int damage = playerInfo.attack - monsterInfo.defence;
        if (damage < 0)
            damage = 0;
 
        // 선빵
        monsterInfo.hp -= damage;
        if (monsterInfo.hp < 0)
            monsterInfo.hp = 0;
        cout << "몬스터 남은 체력 : " << monsterInfo.hp << endl;
 
        if (monsterInfo.hp == 0)
        {
            cout << "몬스터를 처치했습니다!" << endl;
            return;
        }
        
        damage = monsterInfo.attack - playerInfo.defence;
        if (damage < 0)
            damage = 0;
 
        // 반격
        playerInfo.hp -= damage;
        if (playerInfo.hp < 0)
            playerInfo.hp = 0;
        cout << "플레이어 남은 체력 : " << playerInfo.hp << endl;
 
        if (playerInfo.hp == 0)
        {
            cout << "당신은 사망했습니다... GAME OVER" << endl;
            return;
        }
    }
}
cs

 

 

 

 

실수로 return; 을 빠뜨렸었다.

 

 

 

 

 

 

 

 

 

ObjectInfo 내 함수형을 바꿔보자.

 

11 byte가 아닌 16 byte다.

 

 

 

 

 

'⭐ Programming > C++' 카테고리의 다른 글

[C++] 포인터 #2  (0) 2022.03.25
[C++] 포인터 #1  (0) 2022.03.24
[C++] 오버 로딩, 오버 로딩과 오버라이딩의 차이, 함수 마무리  (0) 2022.03.22
[C++] 호출 스택  (0) 2022.03.22
[C++] 호출 스택  (0) 2022.03.22