VioletaBabel

9일 : 다형성을 이용해 간단한 텍스트 RPG 만들기 본문

BCA/1. C,C++,C#
9일 : 다형성을 이용해 간단한 텍스트 RPG 만들기
Beabletoet 2018. 2. 20. 14:48
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
//Main.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"jeok.h"
#include"Unit.h"
#include"Boss.h"
#include"Magician.h"
#include"Monster.h"
#include"Warrior.h"
using namespace std;
void delay(clock_t n)
{
    clock_t s = clock();
    while (clock() - s < n);
}
int main()
{
    srand(time(NULL));
    int select = 0;
    Unit *player, *enemy;
    bool turn;
    cout << "게임을 시작합니다." << endl;
    while (select != 1 && select != 2)
    {
        cout << "\n직업을 선택해주세요.\n1.전사 2.마법사\n선택 -> ";
        cin >> select;
    }
 
    if (select == 1)
    {
        player = new Warrior;
        cout << "전사\n장점 : 높은 물리 공격력, 높은 물리 방어력, 상대적으로 높은 체력\n단점 : 마법 공격에 취약\n스킬 : 버서크.\n  체력 300을 소모하여 공격력을 120 ~ 150%로 올린다.\n" << endl;
    }
    else
    {
        player = new Magician;
        cout << "마법사\n장점 : 높은 마법 공격력, 높은 마법 방어력\n단점 : 낮은 물리 방어력\n스킬 : 마나 익스플로전.\n  자신과 상대방의 MP를 소모해 소모된 MP의 2 ~ 3.5배의 데미지를 입힌다.\n" << endl;
    }
    cout << "로딩 중." << endl;
    for (int i = 0; i < 50++i)
    {
        delay(100);
        cout << ".";
    }
    cout << endl;
    for (int i = 0; i < 3++i)
    {
        turn = rand() % 2;
        if (i < 2)
        {
            enemy = new Monster;
            cout << "야생의 몬스터가 나타났다!\n" << endl;
            bboolchungii();
            cout << endl;
        }
        else
        {
            enemy = new Boss;
            cout << "마왕이 나타났다!\n" << endl;
            jyujyu();
            cout << endl;
        }
        while (1)
        {
            if (turn)
                cout << "당신의 ";
            else
                cout << "적의 ";
            cout << "차례입니다.\n==========\n\nPlayer 체력 : " << player->sayHP() << "\nPlayer 마력 : " << player->sayMP() << "\n\nEnemy 체력 : " << enemy->sayHP() << "\nEnemy 마력 : " << enemy->sayMP() << "\n\n";
            delay(1000);
            if (turn)
            {
                player->SetMPECool();
                player->SetBskCool();
                while (1)
                {
                    select = 0;
                    cout << "당신의 행동은?\n1. 공격 ";
                    if (player->sayType() == 2)
                    {
                        cout << "2. 마법공격 3.마나 익스플로전";
                    }
                    else
                    {
                        cout << "2. 버서크";
                    }
                    cout << "\n-> ";
                    cin >> select;
                    if (select != 1 && select != 2 && select != 3)
                    {
                        cout << "잘못 누르셨습니다!" << endl;
                        continue;
                    }
                    else if (select == 1)
                    {
                        player->Atk(*enemy);
                    }
                    else if(select == 2)
                    {
                        if (player->sayType() == 2)
                        {
                            if (player->sayMP() > 0)
                            {
                                player->MagiAtk(*enemy);
                            }
                            else
                            {
                                cout << "MP가 없습니다!!!\n" << endl;
                                continue;
                            }
                        }
                        else
                        {
                            if (player->GetBskCool() != 0)
                            {
                                cout << "쿨타임이 " << player->GetBskCool() << "턴만큼 남았습니다.\n" << endl;
                                continue;
                            }
                            else if(player->sayHP() > 500)
                            {
                                player->Berserk();
                            }
                            else
                            {
                                cout << "HP가 없습니다!!!\n" << endl;
                                continue;
                            }
                        }
                    }
                    else
                    {
                        if (player->GetMPECool() != 0)
                        {
                            cout << "쿨타임이 " << player->GetMPECool() << "턴만큼 남았습니다.\n" << endl;
                            continue;
                        }
                        else if (player->sayMP() < 500)
                        {
                            cout << "MP가 없습니다!!!\n" << endl;
                            continue;
                        }
                        player->MPExplosion(*enemy);
                    }
                    break;
                }
            }
            else
            {
                select = (enemy->sayType() == 3) ? 1 : rand() % 2 + 1;
                if (select == 1)
                {
                    enemy->Atk(*player);
                }
                else
                    enemy->MagiAtk(*player);                
            }
            if (player->sayHP() < 1)
            {
                cout << "플레이어가 사망하였습니다.\n당신은 패배자입니다.\n" << endl;
                delete player;
                delete enemy;
                return 1;
            }
            else if (enemy->sayHP() < 1)
            {
                cout << "적을 죽였습니다.\n\n\n" << endl;
                delete enemy;
                break;
            }
            turn = !turn;
        }
    }
    delete player;
    cout << "\n승리!!!\n" << endl;
    return 0;
}
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
//Unit.h
#pragma once
class Unit
{
protected:
    int physPwr;
    int physDef;
    int hp;
    int mp;
    int magiPwr;
    int magiDef;
    int type;
public:
    Unit() {};
    void Atk(Unit &defer);
    void MagiAtk(Unit &defer);
    void getHit(int atkDam);
    void getMagiHit(int atkDam);
    int sayHP();
    int sayMP();
    int sayType();
    void setMP(int a);
 
    virtual void MPExplosion(Unit &defer) {}
    virtual void SetMPECool() {}
    virtual int GetMPECool() { return 0; }
 
    virtual void Berserk() {}
    virtual void SetBskCool() {}
    virtual int GetBskCool() { return 0; }
};
 
 
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Boss.h
#pragma once
#include"Unit.h"
class Boss : public Unit
{
public:
    Boss()
    {
        physDef = 130;
        physPwr = 250;
        hp = 5000;
        mp = 10000;
        magiPwr = 300;
        magiDef = 200;
        type = 4;
    }
};
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Magician.h
#pragma once
#include"Unit.h"
class Magician : public Unit
{
private:
    int MPECool;
public:
    Magician()
    {
        physDef = 75;
        physPwr = 150;
        hp = 3000;
        mp = 7000;
        magiPwr = 275;
        magiDef = 150;
        type = 2;
        MPECool = 0;
    }
    void MPExplosion(Unit &defer);
    void SetMPECool();
    int GetMPECool();
};
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Monster.h
#pragma once
#include"Unit.h"
class Monster : public Unit
{
public:
    Monster()
    {
        physDef = 0;
        physPwr = 200;
        hp = 1000;
        mp = 0;
        magiPwr = 0;
        magiDef = 0;
        type = 3;
    }
};
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Warrior.h
#pragma once
#include"Unit.h"
class Warrior : public Unit
{
private:
    int BskCool;
public:
    Warrior()
    {
        physDef = 120;
        physPwr = 250;
        hp = 3500;
        mp = 0;
        magiPwr = 0;
        magiDef = 0;
        type = 1;
        BskCool = 0;
    }
    void Berserk();
    void SetBskCool();
    int GetBskCool();
};
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
//jeok.h
#pragma once
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;
void Pr(WORD w, string s);
void w(int i) { while (i--) { Pr(255" "); } }
void g(int i) { while (i--) { Pr(136" "); } }
void b(int i) { while (i--) { Pr(0" "); } }
void r(int i) { while (i--) { Pr(201" "); } }
void by(int i) { while (i--) { Pr(238" "); } }
void y(int i) { while (i--) { Pr(102" "); } }
void s(int i) { while (i--) { Pr(51" "); } }
void bs(int i) { while (i--) { Pr(187" "); } }
void c() { cout << endl; }
void bboolchungii();
void jyujyu();
 
void Pr(WORD w, string s)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), w);
    cout << s;
}
void jyujyu()
{
    w(27); c();
    w(16); b(3); w(8); c();
    w(15); b(1); w(3); b(1); w(7); c();
    w(14); b(1); s(2); w(3); b(1); w(6); c();
    w(8); b(1); w(5); b(1); w(2); s(2); w(2); b(1); w(5); c();
    w(7); b(1); w(1); b(1); w(3); b(1); w(5); s(1); w(1); b(1); w(5); c();
    w(6); b(1); s(1); w(1); b(1); w(3); b(1); s(4); w(2); s(1); w(1); b(1); w(4); c();
    w(4); b(2); w(1); s(1); w(2); b(2); w(2); b(1); s(5); w(3); b(1); w(3); c();
    w(3); b(1); s(1); w(6); s(1); b(1); w(1); b(1); s(6); w(2); b(1); w(3); c();
    w(3); b(1); w(8); b(1); w(2); b(4); s(1); b(1); w(3); b(1); w(2); c();
    w(2); b(1); w(4); s(1); b(1); s(1); w(3); b(1); w(5); b(1); w(4); b(1); w(2); c();
    w(2); b(1); w(4); y(1); w(1); b(1); w(3); b(1); w(5); b(1); w(5); b(1); w(1); c();
    w(1); b(1); w(1); b(2); w(2); y(1); b(1); w(4); b(1); w(3); b(2); w(6); b(1); w(1); c();
    w(1); b(1); w(12); b(3); w(8); b(1); w(1); c();
    w(2); b(1); w(1); b(2); w(3); b(1); w(15); b(1); w(1); c();
    w(3); b(1); s(2); w(3); s(1); w(15); b(1); w(1); c();
    w(2); b(1); s(1); b(1); s(2); w(1); s(3); w(5); b(2); w(6); b(1); w(2); c();
    w(1); b(1); s(3); b(1); s(6); w(1); s(2); w(3); b(3); w(3); b(1); w(2); c();
    w(1); b(1); s(4); b(1); s(5); b(2); w(1); s(2); w(4); b(1); w(1); b(1); w(3); c();
    w(2); b(1); s(2); b(3); s(6); b(1); w(2); s(2); w(2); b(1); s(1); b(1); w(3); c();
    w(3); b(2); w(2); b(1); s(7); b(2); w(3); b(1); s(1); b(1); w(4); c();
    w(8); b(1); s(8); b(1); w(2); b(2); w(5); c();
    w(9); b(2); s(7); b(2); w(7); c();
    w(11); b(7); w(9); c();
    w(27); c();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void bboolchungii()
{
    w(17);    c();
    w(4);    b(2);    w(11);    c();
    w(4);    b(1);    w(1);    b(4);    w(7);    c();
    w(4);    b(1);    w(1);    g(1);    b(1);    y(2);    b(1);    w(6);    c();
    w(3);    b(1);    g(1);    w(1);    g(1);    b(1);    by(1);    y(2);    b(1);    w(5);    c();
    w(2);    b(1);    y(1);    by(6);    y(1);    b(2);    w(4);    c();
    w(2);    b(2);    by(6);    y(2);    b(1);    w(4);    c();
    w(1);    b(1);    r(2);    b(1);    by(4);    y(3);    b(1);    w(4);    c();
    w(1);    b(1);    r(3);    b(1);    y(1);    b(1);    y(4);    b(1);    w(4);    c();
    w(2);    b(1);    r(2);    b(1);    y(5);    b(1);    w(5);    c();
    w(3);    b(2);    y(4);    b(3);    w(2);    b(1);    w(2);    c();
    w(5);    b(4);    y(2);    b(1);    w(1);    b(1);    g(1);    b(1);    w(1);    c();
    w(5);    b(1);    r(1);    by(2);    r(1);    y(1);    b(3);    g(1);    b(1);    w(1);    c();
    w(6);    b(1);    y(3);    b(1);    y(1);    b(1);    y(1);    b(1);    w(2);    c();
    w(7);    b(3);    y(1);    r(1);    y(1);    b(2);    w(2);    c();
    w(7);    b(2);    y(3);    b(2);    w(3);    c();
    w(9);    b(4);    w(4);    c();
    w(17);    c();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
 
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
//Magician.cpp
#include"Magician.h"
#include<iostream>
using namespace std;
void Magician::MPExplosion(Unit &defer)
{//MP요구량 500, 쿨타임 5턴
    cout << "MP Explosion!" << endl;
    double r1 = (rand() % 151 + 200), r2 = (rand() % 151 + 200);
    int m1 = (rand() % 251 + 250), m2 = (rand() % 251 + 250), dam1, dam2, dam;
    dam1 = this->magiPwr*r1 / 100, dam2 = this->magiPwr*r2 / 100;
    if (dam1 > this->mp)
        dam1 = this->mp;
    if (dam2 > defer.sayMP())
        dam2 = defer.sayMP();
    if (dam1 > defer.sayHP())
        dam1 = defer.sayHP();
    this->mp -= 500;
    defer.setMP(defer.sayMP() - dam2);
    dam = dam1 + dam2;
    if (this->mp < 1)
        this->mp = 0;
    MPECool = 5;
    defer.getMagiHit(dam);
}
 
void Magician::SetMPECool()
{
    if (MPECool > 0)
        --MPECool;
}
 
int Magician::GetMPECool()
{
    return MPECool;
}
 
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
//Unit.cpp
#include"Unit.h"
#include<iostream>
using namespace std;
void Unit::Atk(Unit &defer)
{
    cout << "공격!" << endl;
    double r = (rand() % 41 + 80);
    int dam = this->physPwr * r / 100;
    defer.getHit(dam);
}
void Unit::MagiAtk(Unit &defer)
{
    cout << "마법공격!" << endl;
    double r = (rand() % 41 + 80);
    int dam = this->magiPwr * r / 100;
    if (dam > this->mp)
        dam = this->mp;
    if (dam > defer.sayHP())
        dam = defer.sayHP();
    this->mp -= 200;
    if (this->mp < 1)
        this->mp = 0;
    defer.getMagiHit(dam);
}
void Unit::getHit(int atkDam)
{
    int dam = (atkDam > this->physDef) ? (atkDam - this->physDef) : 0;
    cout << "\n" << dam << "의 데미지";
    if (this->type < 3)
        cout << "를 입었다!\n" << endl;
    else
        cout << "를 주었다!\n" << endl;
    hp -= dam;
}
void Unit::getMagiHit(int atkDam)
{
    int dam = (atkDam > this->magiDef) ? (atkDam - this->magiDef) : 0;
    cout << "\n당신은 " << dam << "의 데미지";
    if (this->type < 3)
        cout << "를 입었다!\n" << endl;
    else
        cout << "를 주었다!\n" << endl;
    hp -= dam;
}
int Unit::sayHP() { return this->hp; }
int Unit::sayMP() { return this->mp; }
int Unit::sayType() { return this->type; }
 
void Unit::setMP(int a) 
    if (a > this->mp)
        this->mp = 0;
    else
        this->mp = a;
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Warrior.cpp
#include"Warrior.h"
#include<cstdlib>
#include<ctime>
void Warrior::Berserk()
{//체력을 300 깎고 공격력을 120~150% 올린다. 쿨타임 5턴
    hp -= 300;
    int r = (rand() % 31 + 120);
    physPwr *= ((double)r/100);
    BskCool = 5;
}
 
void Warrior::SetBskCool()
{
    if (BskCool > 0)
        --BskCool;
}
 
int Warrior::GetBskCool()
{
    return BskCool;
}
 
cs



Comments