Okay, what I'm trying to do here is set up a some-what simple battle system for a text based game... I'm having some trouble getting it to work correctly though. I want the Player's attack and the Monster's defense to be random each time the program loops (which is also not working), but it keeps giving the same numbers.
I don't have much experience, so any help you can offer would be appreciated, thanks.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Variables
int charLevel, Floor;
int Hp, Atk, Def;
int monLevel, monHp, monAtk, monDef;
string action, charName, monName;
int damageDealt;
// Prototypes
void battlePhase();
int main()
{
// Defining the character
cout << "What is your name? ";
cin >> charName;
cout << "\nWhat level are you? ";
cin >> charLevel;
cout << "\nWhat floor are you on? ";
cin >> Floor;
cout << "\nWhat monster do you wish to fight? ";
cin >> monName;
// Player Stats
// charLevel defined earlier
srand((unsigned)time(0));
Hp = (15 + ( charLevel * 5 ));
Atk = (((rand() % 6) + 0) + (charLevel - 1));
Def = (((rand() % 4) + 0) + (charLevel - 1));
// Monster Stats
monLevel = (1/2) * Floor + (charLevel - 2);
if ( monLevel < 0 )
{
monLevel == 1;
}
monHp = (10 + (monLevel * 2));
monAtk = (((rand() % 4) + 1) + (monLevel - 1));
monDef = (((rand() % 3) + 1) + (monLevel - 1));
while (Hp > 0 || monHp > 0)
{
battlePhase();
}
return 0;
}
void battlePhase()
{
// Actual Battle
cout << endl << "\nWhat do you want to do? ";
cin >> action;
if ( action == "Attack" || action == "attack" )
{
cout << endl << charName << " attacked " << monName << " for " << Atk << endl;
cout << monName << " defended " << charName << " for " << monDef << endl;
damageDealt = Atk - monDef;
if ( damageDealt < 0 )
{
damageDealt = 0;
}
if ( damageDealt == 0 )
{
cout << "\nNo damage!";
}
if ( damageDealt > 0 )
{
cout << monName << " took " << damageDealt << " damage!";
monHp = monHp - damageDealt;
}
}
else
{
cout << "\nThat isn't an action! Try typing 'attack'.";
battlePhase();
}
}