-
Notifications
You must be signed in to change notification settings - Fork 0
/
includes.cpp
178 lines (148 loc) · 4.94 KB
/
includes.cpp
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
#include <iostream> // Reading 'help' file
#include <cstdlib> // rand(), srand()
#include <fstream> // ifstream
#include "objects/mainchar.h"
#include "objects/enemy.h"
#include "includes.h"
// ----- Defining Non-member functions ----- //
#ifdef _WIN32
#include <Windows.h>
#define SLEEP_FACTOR 1000
#else
#include <unistd.h>
#define SLEEP_FACTOR 1
#endif
void tba_sleep(int i)
{
#ifdef _WIN32
Sleep(i * SLEEP_FACTOR);
#else
sleep(i * SLEEP_FACTOR);
#endif
}
void tba_sleep(double d)
{
#ifdef _WIN32
Sleep((unsigned int)(d * SLEEP_FACTOR));
#else
sleep((unsigned int)(d * SLEEP_FACTOR));
#endif
}
// return a random integer between 0 and a-1
int random(int a)
{
return rand()%a;
}
// checks if an object of a given class has been slaughtered
template <class T>
int dead_check(T& thing)
{
if(thing.hp<=0)
{
tba_sleep(1);
std::cout << "The " << thing.name << " has been slaughtered." << std::endl << std::endl;
return 1;
}
else
return 0;
}
// special case for main character
template <>
int dead_check<>(MainChar& player)
{
if( player.hp <= 0 )
{
tba_sleep(1);
std::cout << "You have died!" << std::endl;
tba_sleep(1);
return 1;
}
else
return 0;
}
// probability of an enemy attempting to cast a spell
int spellProb(Enemy foe)
{
//probability of casting a spell is 1/prob
int prob=100; //probability of a non-magic foe attempting to cast
if( foe.name == "skeleton" )
prob = foe.hp;
else if( ( foe.name=="magic carpet" ) || ( foe.name == "mad warlock" ) )
prob = 1;
else if( foe.name == "haunted suit of armour" )
prob = 16 - foe.hp;
return prob;
}
// the battle sequence between an enemy and the main character
void battle(MainChar& player, Enemy& foe)
{
// Decides whether foe will attack or cast a spell
int foeChoice = 0;
// Loops until one battler is defeated
while( ( foe.hp > 0 ) && ( player.hp > 0 ) )
{
//--- Enemy turn ---//
tba_sleep(1);
foeChoice = random( spellProb(foe) );
if( (foeChoice == 0) ) //if the enemy will cast a spell
{
if( (foe.spell(player) == 1) ) //spell is cast in this function call if it can be cast
// return value of 1 indicates that spell hasn't been cast
foe.attack(player); //if spell can't be cast, player is attacked instead
}
// Default action for opponent
else
foe.attack(player);
//--- Player turn ----//
tba_sleep(1);
if(player.hp>0)
{
// UI text
std::cout << player.name << ": hit points " << player.hp << "/att. power " << player.att_pow << "/magic points " << player.magic << std::endl;
std::cout << "The " << foe.name << " has " << foe.hp << " hit points remaining." << std::endl << std::endl;
tba_sleep(1.5);
std::cout << "What will you do?" << std::endl;
tba_sleep(0.5);
std::cout << "Choose: 'attack', 'bolt' or 'aura'." << std::endl;
std::cout << "Type 'help' if you need the rules of combat explained." << std::endl;
std::string turn_choice = "";
// get the player's choice of action
while( (turn_choice != "attack") && (turn_choice != "bolt") && (turn_choice != "aura") )
{
std::cin >> turn_choice;
std::cout << std::endl;
if(turn_choice == "help")
{
// printing the help screen from a .txt file
std::string line;
std::ifstream combat_help("Text/combat_help.txt");
while( getline(combat_help, line) )
{
std::cout << line << std::endl;
}
std::cout << std::endl;
// Close file after use
combat_help.close();
}
// invalid command entered (or 'help')
if( ( turn_choice != "attack" ) && ( turn_choice != "bolt" ) && ( turn_choice != "aura" ) )
std::cout << "Please choose 'attack', 'bolt' or 'aura'." << std::endl;
}
// A valid choice was entered
if(turn_choice == "attack")
player.attack(foe);
if(turn_choice == "bolt")
player.bolt_spell(foe);
if(turn_choice == "aura")
player.aura_spell();
}
}
// ending the fight
if(dead_check(foe) == 1)
return;
// dead_check for player happens in main loop
// so we don't need to call it here
if(player.hp <= 0)
return;
}
// ----- End of non-member functions ----- //