Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Runtime Bug.

Think you can spy out that little nasty bug? Try debugging some code in here

Runtime Bug.

Postby CSkinner on Thu Jul 23, 2009 1:35 pm

why cant player or enemy hit less than 5 does it have something to do with antiRTFM's random function that i'm using or is it somewhere embedded in my code


Code: Select all
#include "stdafx.h"

#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
#include <cmath>
#include <iostream> // for cin() and cout()
using namespace std;

int GetRand(int rangeMin, int rangeMax)
{
    return (int)floor(rand() / (RAND_MAX + 1.0) * ((rangeMax - rangeMin) + 1) + rangeMin);
}

class Player
{
private:
   //Declare Player Values
   int MaxHealth;
   int Health;
   int MinDamage;
   int MaxDamage;
   int DamageResistance;
   int Damage;
public:
      Player():
      MaxHealth(100),
      Health(100),
      MinDamage(0),
      MaxDamage(10),
      DamageResistance(2)
      {
      }
   int GetMaxHealth()         { return MaxHealth; }

   int GetHealth()            { return Health; }

   int GetDamageResistance()   { return DamageResistance; }

   int GetMaxDamage()         { return MaxDamage; }

   int GetDamage()
   {Damage = GetRand(MinDamage, MaxDamage);
   return Damage;
   }
};

class Enemy
{
private:
   //Declare Enemy Values
   int MaxHealth;
   int Health;
   int MinDamage;
   int MaxDamage;
   int DamageResistance;
   int Damage;
public:
      Enemy():
      MaxHealth(100),
      Health(100),
      MinDamage(0),
      MaxDamage(10),
      DamageResistance(2)
      {
      }
   int GetMaxHealth()         { return MaxHealth; }

   int GetHealth()            { return Health; }

   int GetDamageResistance()   { return DamageResistance; }

   int GetMaxDamage()         { return MaxDamage; }

   int GetDamage()
   {Damage = GetRand(MinDamage, MaxDamage);
   return Damage;
   }
};

int main()
{
   // set initial seed value to system clock
   srand((long)time(0));

   Player Player;
   Enemy Enemy;

   int Player_Health = Player.GetHealth();
   int Player_MaxHealth = Player.GetMaxHealth();
   int Player_DamageResistance = Player.GetDamageResistance();
   int Player_Damage = Player.GetDamage();
   int Player_MaxDamage = Player.GetMaxDamage();

   int Enemy_Health = Enemy.GetHealth();
   int Enemy_MaxHealth = Enemy.GetMaxHealth();
   int Enemy_DamageResistance = Enemy.GetDamageResistance();
   int Enemy_Damage = Enemy.GetDamage();
   int Enemy_MaxDamage = Enemy.GetMaxDamage();

   for(int CombatOver = 0;CombatOver = 1;)
   {
      Player_Damage = Player.GetDamage();
      Enemy_Damage = Enemy.GetDamage();

      if (Player_Damage > Player_MaxDamage)
      {
         cout << "Error, Players's Damage was more than its maximum value!";
         Player_Damage = Player_MaxDamage;
      }
      if (Enemy_Damage > Enemy_MaxDamage)
      {
         cout << "Error, Enemy's Damage was more than its maximum value!";
         Enemy_Damage = Enemy_MaxDamage;
      }
      if (Enemy_Health <= 1)
      {
         cout << "You killed the Enemy";
         break;
      }

      if (Player_Health <= 1)
      {
         cout << "You Died";
         break;

      }

      if (Player_Health > Player_MaxHealth)
      {
         Player_Health = Player_MaxHealth;
         cout << "Error, Players's health was more than its maximum value!";
      }
      if (Enemy_Health > Enemy_MaxHealth)
      {
         Enemy_Health = Enemy_MaxHealth;
         cout << "Error, Enemy's health was more than its maximum value!";
      }
      if (Player_Damage > Enemy_DamageResistance)
      {
         if ((Player_Damage + Enemy_DamageResistance) < (Player_MaxDamage + 1))
         {
            cout << "Player is attacking! \n";
            Enemy_Health = Enemy_Health - (Player_Damage + Enemy_DamageResistance);
            cout << "Player hits Enemy for ";
            cout << Player_Damage + Enemy_DamageResistance;
            cout << " Damage! \n";
            cin.get();
         }

      }
      else
      {
         cout << "Player Misses Enemy! \n\n";
         cin.get();
      }

      if (Enemy_Damage > Player_DamageResistance)
      {
         if ((Enemy_Damage + Player_DamageResistance) < (Enemy_MaxDamage + 1))
         {
            cout << "Enemy is attacking! \n";
            Player_Health = Player_Health - (Enemy_Damage + Player_DamageResistance);
            cout << "Enemy hits Player for ";
            cout << Enemy_Damage + Player_DamageResistance;
            cout << " Damage! \n";
            cin.get();
         }
      }
      else
      {
         cout << "Enemy Misses Player! \n\n";
         cin.get();
      }
      

   }

   cin.get();
}
CSkinner
 
Posts: 1
Joined: Thu Jul 23, 2009 12:11 pm

Re: Runtime Bug.

Postby tbdawg on Tue Oct 20, 2009 6:49 pm

In short yes the problem is with the PRNG that you are using. Although it works, when your clock ticks aren't making drastic changes the number won't change much. If you waited long enough you'd eventually get a number lower than 5. This PRNG is not very good for small ranges, but it does work.

This isn't the best PRNG either, but will work much better for what you are doing.

Code: Select all
#include <iostream> // for cout and endl

using namespace std;

int GetRand(int rangeMin, int rangeMax)
{
   return ((rand() % (rangeMax - rangeMin + 1)) + rangeMin);
}

int main()
{
    srand(static_cast<unsigned long>(time(0)));
    cout << GetRand( 0, 10) << endl;
    return 0;
}
Last edited by tbdawg on Thu Oct 22, 2009 12:27 am, edited 3 times in total.
tbdawg
 
Posts: 6
Joined: Wed Jul 15, 2009 5:40 am

Re: Runtime Bug.

Postby Vevix on Tue Oct 20, 2009 6:54 pm

Meh, Whenever I've needed a random type function I've always just used the boost libraries.

Use C++ casts, For example:

Code: Select all
srand(static_cast<unsigned int>(time(0)));
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup
User avatar
Vevix
 
Posts: 142
Joined: Sat Oct 10, 2009 9:24 pm
Location: kernel32.dll

Re: Runtime Bug.

Postby tbdawg on Tue Oct 20, 2009 7:04 pm

Ya I know, just been working in C for the last couple days so was stuck in C mode. However, the cast wasn't needed (neither was floor()), as the rand function In my post above will already return an integer from 0-10 inclusive, so I edited it. The cast for Time has also been changed to C++.
tbdawg
 
Posts: 6
Joined: Wed Jul 15, 2009 5:40 am

Re: Runtime Bug.

Postby noobgrammer on Wed Oct 21, 2009 4:16 pm

I keep seeing this pop up isn't this a C header

Code: Select all
#include <cstdlib> // for RAND_MAX


I commented this out and the program still ran fine
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Runtime Bug.

Postby antiRTFM on Wed Oct 21, 2009 5:44 pm

Yep its for compatibility with C, no need to include it
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Runtime Bug.

Postby tbdawg on Thu Oct 22, 2009 12:26 am

Ya, as I said I was stuck in C mode and forgot to remove the unnecessary C header files from the source. None of which were needed. Re-edited source.
tbdawg
 
Posts: 6
Joined: Wed Jul 15, 2009 5:40 am

Re: Runtime Bug.

Postby noobgrammer on Thu Oct 22, 2009 7:22 am

That was why I ask here because you had said that you where still in C mode so I was curious if that was one of your C modes left on accident.
People keep sticking it in there code here, was it something C++ updated and done away with so it is still in outdated material saying to use it?
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Runtime Bug.

Postby tbdawg on Thu Oct 22, 2009 6:33 pm

Actually, from what i understand it is compiler dependent, but most are this way. May just be the older C++ compilers that aren't including those functions/headers within the iostream header. So, really it is better for portability to go ahead and list those header files. Because (not sure), I don't think that being linked via the iostream header file is the ANSI standard. Then again maybe it now and didn't used to be IDK. The functions are still located in those headers AFAIK and not duplicated somewhere else.

As for the why I'm stuck in C mode. I have been trying to translate some C code to C++, and I have forgoten alot since I haven't been messing with any code for quite some time and just started to get back into it. If ya don't use it ya loose it, and I'm almost back to square 1. Which isn't really saying much anyhow. lol
tbdawg
 
Posts: 6
Joined: Wed Jul 15, 2009 5:40 am


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests