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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// Example program
#include <iostream>
#include <cmath>
#include <random>
#include <string>
#include <iomanip>
using namespace std;
//header
enum AttackType
{
OFFENSIVE = 1,
DEFENSIVE = 2,
};
class Character
{
public:
void Setup(const string &name, int hp, int atk, int def);
void DisplayStats();
void SelectAction();
int GetAttack();
void GetHit(int atk);
int GetHP();
protected:
string m_name;
int m_hp;
int m_atk;
int m_def;
AttackType m_attackType;
};
class Player: public Character
{
public:
void SelectAction();
};
class Enemy : public Character
{
public:
void SelectAction();
};
// main
int GetUserInput(int min, int max)
{
int number;
cout << "Please enter a number" << endl;
cin >> number;
if (number > max)
{
cout << "Sorry that's an invalid option. Please try again." << endl;
GetUserInput(min, max);
}
if (number < min)
{
cout << "Sorry that's an invalid option. Please try again." << endl;
GetUserInput(min, max);
}
return number;
}
int GetRandom(int min, int max)
{
srand(time(NULL)); // Seed the time for random number every time
int finalNum = rand() % (max - min + 1) + min;
return finalNum;
}
void SetupCharacters(Player &player, Enemy &enemy);
int main()
{
bool ready = false;
Player player;
Enemy enemy;
while (ready == false)
{
int pick;
SetupCharacters(player, enemy);
cout << "Your stats:" << endl;
player.DisplayStats();
cout << "Enemy's stats:" << endl;
enemy.DisplayStats();
cout << "Are you ready?\n\n1. Yes\n2. No" << endl;
pick = GetUserInput(1, 2);
if (pick == 1)
ready = true;
if (pick == 2)
ready = false;
}
int i = 1;
while (player.GetHP() > 0 && enemy.GetHP() > 0)
{
int playerAtkNum;
int enemyAtkNum;
cout << "Round " << i << "\n\n" << "Your stats: ";
player.DisplayStats();
cout << "\n\n" << "Your opponents' stats: ";
enemy.DisplayStats();
player.SelectAction();
enemy.SelectAction();
playerAtkNum = player.GetAttack();
enemyAtkNum = enemy.GetAttack();
player.GetHit(enemyAtkNum);
enemy.GetHit(playerAtkNum);
i++;
}
system("pause");
return 0;
}
void SetupCharacters(Player &player, Enemy &enemy)
{
string name;
int stats;
cout << "Please, tell us your name." << endl;
cin >> name;
const string NAME{ name };
cout << endl;
cout << "Pick your stats!\n\n";
cout << "Option" << setw(15) << "Attack" << setw(15) << "Defense" << endl;
cout << "1" << setw(15) << "5" << setw(15) << "15" << endl;
cout << "2" << setw(15) << "15" << setw(15) << "5" << endl;
cout << "3" << setw(15) << "10" << setw(15) << "10" << endl;
cin >> stats;
cout << endl;
if (stats == 1)
{
player.Setup(NAME, 50, 5, 15);
}
else if (stats == 2)
{
player.Setup(NAME, 50, 15, 5);
}
else
{
player.Setup(NAME, 50, 10, 10);
}
int finalNum;
finalNum = GetRandom(1, 3);
if (finalNum == 1)
{
enemy.Setup("NPC", 50, 5, 15);
}
else if (finalNum == 2)
{
enemy.Setup("NPC", 50, 15, 5);
}
else if (finalNum == 3)
{
enemy.Setup("NPC", 50, 10, 10);
}
}
//implementation
void Character::Setup(const string &name, int hp, int atk, int def)
{
m_name = name;
m_hp = hp;
m_atk = atk;
m_def = def;
}
void Character::DisplayStats()
{
cout << m_name << " HP:" << m_hp << " ATK:" << m_atk << " DEF:" << m_def << endl;
}
void Character::SelectAction()
{
}
int Character::GetAttack()
{
int attackNum;
srand(time(NULL)); // Seed the time for random number every time
int finalNum = rand() % (3 - 1 + 1) + 1;
if (m_attackType == OFFENSIVE)
{
attackNum = m_atk + finalNum;
cout << m_name << " chose Offensive style." << endl;
return attackNum;
}
else{
cout << m_name << " chose Defensive style." << endl;
attackNum = m_atk;
return attackNum; }
}
void Character::GetHit(int attack)
{
int dmg;
if (m_attackType == OFFENSIVE)
{
dmg = attack - m_def;
if (dmg >= 0)
{
m_hp = m_hp - dmg;
}
if (dmg <= 0)
{
m_hp = m_hp;
}
}
else
{
srand(time(NULL)); // Seed the time for random number every time
int finalNum = rand() % (3 - 1 + 1) + 1;
dmg = attack - m_def - finalNum;
if (dmg >= 0)
{
m_hp = m_hp - dmg;
}
if (dmg <= 0)
{
m_hp = m_hp;
}
}
}
int Character::GetHP()
{
return m_hp;
}
void Player::SelectAction()
{
int type;
srand(time(NULL)); // Seed the time for random number every time
int finalNum = rand() % (3 - 1 + 1) + 1;
cout << "Which attack style would you like?\n1. Offensive\n2. Defensive" << endl;
cin >> type;
cout << endl;
if (type == 1)
{
m_attackType = OFFENSIVE;
}
else {
m_attackType = DEFENSIVE;
}
}
void Enemy::SelectAction()
{
srand(time(NULL)); // Seed the time for random number every time
int finalNum = rand() % (2 - 1 + 1) + 1;
if (finalNum == 1)
{
m_attackType = OFFENSIVE;
}
else
{
m_attackType = DEFENSIVE;
}
}
|