CHECK spot for random monster

I have this code which I made:

http://pastie.org/private/vb0jhlc8zmaiong0qzyogw


I want to make it where when I step on a blank spot ' ', itll randomize a number and if that number is like <= 1 out of 10, then a random monster will appear.

I need help splitting my monster code which is on the void checkspot() into its separate code so I can call it whenever that tile is <= 1 out of 10. I tried splitting it up and I successfully worked it, but it kept bugging and looping.
I see you're calling srand() in multiple functions -not correct, just call it once in main()

I want to make it where when I step on a blank spot ' ', itll randomize a number and if that number is like <= 1 out of 10, then a random monster will appear.

try something like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    int num;
    srand(time(0));
    for(int i = 0; i < 10; ++i)
    {
        num = rand()%10;
        if(num <= 1) std::cout << "yay!" << std::endl;
        else std::cout << "nay..." << std::endl;
     }
}


I tried splitting it up and I successfully worked it, but it kept bugging and looping.

not sure what you're trying to say here
I split up the monster function from the checkspot() into its own function called monster(), then on the map i said that if they land on a blank spot, they have a 10% chance to get a monster. This all works but the problem is that the monster function called the map for it to show at all times. And it keeps running the function after and after becuase it checks the spot each time, so then it bugs.
Well there's small problem that there's no monster() anywhere in your code...

but then if I understand you correctly, in the checkspot() function, you want to do this:
1. check for locations
2. if it's a random location, check for possibility of monster appearing
2.1 if the condition is correct call the monster function, else perform other tasks
3. do other stuff

I think the lines 642-703 is a good candidate for putting into a separate function


How should I do #1 - 2 ad you specified. I was going a different approach and calling it at every movement on the map(); but I tried that and failed miserably.
Try to put the 'call monster function' after you've tested for all the special coordinates -i.e in checkspot():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void checkspot() 
{ 
       //first check for shops
       if( /*shop location*/ ) ...
        
       //else if it's not a shop
       else if( /*coordinate (15, 14)*/ ) ...
       else if( /*coordinate (0, 14)*/) ...

       //so, we're not at a special location
       else 
       {
             // get a random number
             if (random_number)
                 call_monster_function()
             else /*not the monster number*/
                 // move on
        }
}


and also try to make a simple constructor for each class you've created, e.g
1
2
3
4
5
6
7
8
9
10
11
12
class Monster
{
public:
      int strength, fight;
      
      Monster(): strength(0), fight(0) {}; //simple init
      Monster(int s, int f): strength(s), fight(f) {};
      // etc
};

       // this makes it easier to construct one on the fly e.g in monster_function()
       Monster some_random_monster(5, 4);


Topic archived. No new replies allowed.