Sign in to follow this  

RPG Battle System

This topic is 2829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

here is my battle system but it always says that my player does 0 damage etc.

	int x;


x = rand() %5;

if(x == 0)
{
c.str = c.dmg + 1;
m.health -= c.dmg;
cout << "Your attack deals\n" << c.dmg << "Damage\n";
}
if(x == 1)
{
c.str = c.dmg - 1;
m.health -= c.dmg;
cout << "Your attack deals\n" << c.dmg << "Damage\n";
}
if(x == 2)
{
c.str = c.dmg / 2;
m.health -= c.dmg;
cout << "Your attack deals\n" << c.dmg << "Damage\n";
}
if(x == 3)
{
c.str = c.dmg * 3;
m.health -= c.dmg;
cout << "Your attack deals\n" << c.dmg << "Damage\n";
}
if(x == 4)
{
c.dmg = 0;
cout << "Monster Blocked your attack!\n";
}
if(m.health <=0)
{
cout << "You killed the monster!!\n";
}
else
{
cout << "Leaving the monster with\n" << m.health << "Health Points\n" << m.monster_name << "Will now attack you!\n";

int y;
y = rand() % 5;
if (y == 0)
{
m.str = m.dmg;
}
else if (y == 1)
{
m.str = m.dmg - 1;
}
else if (y == 2)
{
m.str = m.dmg + 1;
}
else if (y == 3)
{
m.str = m.dmg*2;
}
else if (y == 4)
{
m.dmg = 0;
cout << "Miss\n" << endl;
}
c.health = c.health - m.dmg;
if(c.health <0)
{
cout << "Game Over\n";
}
}
monster_load.close();
load_account.close();


it also never lets the monster attack, it just skips over that part. Any ideas?

Share this post


Link to post
Share on other sites
Sorry fellas didn't work I did get the monster to do damage an it's giving me weird amount of damage an def not showing up so I am thinking I did something wrong with code somewhere along the line idk though

Share this post


Link to post
Share on other sites
Are you seeding the random number generator?


#include <time.h>
int x;

srand(time(NULL)); //Seed the random number generator, or you'll always get the same number.
x = rand() % 5;
//The rest of your code.



I think you only need to seed it once, so probably in your main() function and not in your battle function.

Share this post


Link to post
Share on other sites
for some reason my rand keeps on landing on the same rand, and never changes any ideas?

srand(time_t(NULL));
int hit_miss;
hit_miss = rand() %4;

if(hit_miss == 0)
{
c.dmg = c.str+1;
m.health -=c.str;
cout << "(rand 0) You do\n" << c.dmg << "Damage Leaving\n" << m.monster_name << "With\n" << m.health << "Health points\n";
}

else if(hit_miss == 1)
{
c.dmg = c.str+1;
m.health -=c.str;
cout << "(rand 1)You do\n" << c.dmg << "Damage Leaving\n" << m.monster_name << "With\n" << m.health << "Health points\n";
}

else if(hit_miss == 2)
{
c.dmg = c.str*2;
m.health -=c.str;
cout << "(rand 2)You do\n" << c.dmg << "Damage Leaving\n" << m.monster_name << "With\n" << m.health << "Health points\n";
}
else if(hit_miss == 3)
{
c.dmg = 0;
cout << "(rand 3)You miss\n";
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Tiblanc
You need to swap str and dmg in your damage calculations.
You have addressed this issue a little too thoroughly. You calculate c.dmg but then you now use c.str to alter the health variable.

In regards to the random variable, which number always gets used? Does it change if you restart the game?

Share this post


Link to post
Share on other sites
time_t is a variable type, not a function.

So what you are basically doing is calling srand(NULL) every time before you calculate damage. This resets the list of random numbers, and because it's the same seed, you will always get the first number (in this case 2). You only need to seed once, at the start of your program.

I would replace time_t with time, and move the srand call to your init code where it will only run once.

Share this post


Link to post
Share on other sites
that worked thanks is this something to worry about

1>c:\documents and settings\computer\desktop\game\untitled_text_game\untitled_text_game\attack.h(38) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

It's just a warning but is there a way to get it to go away? Besides using time_t? That's the reason I had the srand as time_t

Share this post


Link to post
Share on other sites
This happens because srand() expects an integer, and time() gives it a time_t variable. You can try typecasting the time_t as an unsigned integer. This would look something like
srand((unsigned int)time(NULL))

This stuff isn't my strong point so it may or may not work but it's worth a try.

Share this post


Link to post
Share on other sites

This topic is 2829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this