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
|
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
//prototypes
void helplist ();
void displaynumber (int yhealth, int ehealth);
void commandprompt ();
void monsterattack ();
//health/etc. variable declaration
int yhealth, ehealth, damage, heal, enemydamage, enemyheal;
//displays health
void displaynumbers (int yhealth, int ehealth)
{
cout << "\nYour Health " << yhealth << "\tEnemy Health " << ehealth <<"\n" << endl;
}
//prompts for and computes commands
void commandprompt ()
{
char x;
bool input = false;
while (!input)
{
damage = 0;
heal = 0;
cout << "Please enter a command: ";
cin >> x;
switch (x)
{
case 'h':
case 'H':
helplist();;
break;
case 'a':
case 'A':
damage = damage = rand() % 5 + 5;
cout << "\nYou punched the enemy for " << damage << " damage" << endl;
input = true;
break;
case 'b':
case 'B':
damage = damage = rand() % 13 + 2;
cout << "\nYou kicked the enemy for " << damage << " damage" << endl;
input = true;
break;
case 'c':
case 'C':
damage = damage = rand() % 30;
if (damage == 0)
{
cout << "\nYou missed" << endl;
}
else
{
cout << "\nYou slashed the enemy for " << damage << " damage" << endl;
}
input = true;
break;
case 'd':
case 'D':
damage = damage = rand() % 100 + -50;
if (damage > 0)
{
cout << "\nYou cast magic on the enemy for " << damage << " damage" << endl;
}
else if (damage == 0)
{
cout << "\nYou cast magic on the enemy but it did nothing" << endl;
}
else
{
cout << "\nYou cast magic but accidently healed the enemy for " << damage * -1 << " health" << endl;
}
input = true;
break;
case 'e':
case 'E':
heal = rand() % 100 + -50;
if (heal > 0)
{
cout << "\nYou healed yourself for " << heal << " health" << endl;
}
else if (heal == 0)
{
cout << "\nThe potion did nothing" << endl;
}
else
{
cout << "\nThe potion did not work quite right and lowered your health by " << heal * -1 << endl;
}
input = true;
break;
default:
cout << "\nYou did not enter a valid command. Please try again. If you need help, type 'h' for a list of commands.\n" << endl;
}
}
}
//displays help
void helplist ()
{
cout << "\nPlease type in the letter of the command you wish to use\n" << endl;
cout << "---List of commands---" << endl;
cout << "Punch (a) - Punch the enemy for damage" << endl;
cout << "Kick (b) - Kick the enemy with a sword for damage" << endl;
cout << "Slash (c) - Slash the enemy for damage" << endl;
cout << "Magic (d) - Cast magic on the enemy causing either damage or health" << endl;
cout << "Potion(e) - Use a potion to heal yourself - it might have negetive effects\n" << endl;
}
//enemy attack
void monsterattack ()
{
int x;
bool input = false;
while (!input)
{
x = rand() % 4 + 1;
enemydamage = 0;
enemyheal = 0;
switch (x)
{
case 1:
enemydamage = rand() % 5 + 5;
cout << "\nThe enemy slashed you for " << enemydamage << " damage" << endl;
input = true;
break;
case 2:
enemydamage = rand() % 13 + 2;
cout << "\nThe enemy bit you for " << enemydamage << " damage" << endl;
input = true;
break;
case 3:
enemydamage = rand() % 30;
if (enemydamage == 0)
{
cout << "\nThe enemy missed" << endl;
}
else
{
cout << "\nThe enemy punched you for " << enemydamage << " damage" << endl;
}
input = true;
break;
case 4:
enemydamage = rand() % 100 + -50;
if (enemydamage > 0)
{
cout << "\nThe enemy cast magic on you for " << enemydamage << " damage" << endl;
}
else if (enemydamage == 0)
{
cout << "\nThe enemy cast magic but nothing happened" << endl;
}
else
{
enemyheal = enemydamage;
enemydamage = 0;
cout << "\nThe enemy accidently hit itself for " << enemyheal * -1 << " health with its own magic" << endl;
}
input = true;
break;
case 5:
enemyheal = rand() % 50 + -50;
if (enemyheal > 0)
{
cout << "\nThe enemy healed itself " << enemyheal << " health" << endl;
}
else if (enemyheal == 0)
{
cout << "\nThe enemy's potion did nothing" << endl;
}
else
{
cout << "\nThe enemy's potion did not work quite right and lowered its health by " << heal * -1 << endl;
}
input = true;
break;
default:
break;
}
}
}
int main()
{
int rounds = 0;
srand((unsigned)time(0));
cout << "Welcome to the battle program! Type 'h' if you want a list of the possible commands. Remember to press the ENTER key after each command you type." << endl;
//creates health for each side
yhealth = 100;
ehealth = 100;
//starts loop
while (ehealth > 0 || yhealth > 0)
{
displaynumbers (yhealth, ehealth);
commandprompt ();
monsterattack();
ehealth = ehealth - damage;
ehealth = ehealth + enemyheal;
yhealth = yhealth - enemydamage;
yhealth = yhealth + heal;
rounds ++;
if (ehealth < 1 || yhealth < 1)
{
ehealth = 0;
break;
}
}
if ( (yhealth < 1) && (ehealth > 1) )
{
displaynumbers(yhealth, ehealth);
cout << "\nAfter a numberous " << rounds << " rounds, you lost.";
}
else if ( (yhealth < 1) && (ehealth < 1) )
{
yhealth = 0;
ehealth = 0;
displaynumbers (yhealth, ehealth);
cout << "\nOddly enough, after a numerous " << rounds << " rounds, there was a tie.\n";
}
else
{
displaynumbers(yhealth,ehealth);
cout << "\nAfter a numerous " << rounds << " rounds, you win!\n";
}
system("PAUSE");
return 0;
}
| |