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();
		}
	
}

If you want the Atk, Def, monAtk, and monDef variables to randomize for each iteration of the loop, you will have to calculate them inside the loop. Currently, you calculate them before the loop starts and you have no way to get back to them to re-calculate them.

Edited 7 Years Ago by Fbody: n/a

Thanks for the feedback... Hm, I tried what you said and moved:

Atk = (((rand() % 6) + 0) + (charLevel - 1));
Def = (((rand() % 4) + 0) + (charLevel - 1));

monAtk = (((rand() % 4) + 1) + (monLevel - 1));
monDef = (((rand() % 3) + 1) + (monLevel - 1));

Into the if statement to make:

void battlePhase()
{	 
	// Actual Battle
	cout << endl << "\nWhat do you want to do? ";
	cin >> action;

	if ( action == "Attack" || action == "attack" )
		{
			// Randomizing the attack/defend values for player and monster
			Atk = (((rand() % 6) + 0) + (charLevel - 1));
			Def = (((rand() % 4) + 0) + (charLevel - 1));
			
			monAtk = (((rand() % 4) + 1) + (monLevel - 1));
			monDef = (((rand() % 3) + 1) + (monLevel - 1));

			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();
		}
	
}

It still doesn't seem to work however, and when I tried this the first result for the monDef was -1... Which shouldn't be able to happen.

I'm rather stumped. :-/

You need to use srand, with integrated time, so the random integer will change(seed).

Example:

srand ((unsigned)time(0));
				random3 = (rand() % 4) + 1;

Edit: Also include the header: #include<time.h> if you do not already have it.

Also, if you don't mind, please send me the source code once you are done so I can check it out. I love checking out others' RPGs that they make. :)

Edited 7 Years Ago by restrictment: n/a

I didn't see it before...

In your original post, Line 40, change that line (whatever line number it is now) from the equality comparison operator to the assignment operator. monLevel == 1; needs to be: monLevel = 1;

Edited 7 Years Ago by Fbody: n/a

Apparently using "goto x;" is some kind of ungodly error that i seem to have picked up. But I have been able to create some really successful programs with it.

You can create different type of modules within your program using goto.

For instance:

{
HPSTASIS:
hptot = Health - EnDamage;
Health = hptot;
if (Health <= 0) {cout<<"You have died.";system("PAUSE");goto endgame;}
else if (Health > 0){
goto CHECKPOINT;
}
}

In which after each attack against you, it makes sure your health is correct and checks to see if you are dead. In which the game would end, otherwise if you are not dead, it goes to the checkpoint module that continues the battle.

This article has been dead for over six months. Start a new discussion instead.