How to write a program that can.....

Draw the total of a passed value, or value in general, without checking how many values there are individually.

Here's a pseudo-example:

1
2
3
4
5
int aliens = 10; // This should be changed as desired.
void drawaliens(int value)
{
aliens.draw(value); // Instead of having a test of how many to draw, draw the amount there is from one check
}


Don't get it? I'll explain even further if you don't:

I want to draw the amount of aliens passed or called to the alien draw function.

However, I don't want the function to check every possible value of aliens which could be passed before drawing, and just check the value once and draw that value.

IF x ALIENS, DRAW x ALIENS instead of...

IF 1 ALIEN, DRAW ALIEN(1);
IF 2 ALIEN, DRAW ALIEN(2);
IF 3 ALIEN, DRAW ALIEN(3);
.... and so on.

If there can be hundreds of aliens, it seems impractical to check every single possible value before drawing, and just check the value and draw that value.

However, I'm at a total blank as to how to do this in any conceivable, imaginable way(not a total noob to programming, but just can't get my head around this one problem). ANY EXAMPLE will be preferred, such as you understand what I'm getting at here.
is alien a class? if so you could put a counter variable.

1
2
3
4
5
6
7
8
9
10
class Alien
{
    public:
        Alien(){ ++aliens; }
      ~Alien(){ --aliens; }
    private:
        static int aliens;
};

int Alien::aliens = 0;


This can keep track of how many aliens there are.
Your question is a bit vague to me, do you have a bunch of alien objects contained in the aliens variable that need to be drawn? Or does the parameter value determine the number of aliens to be drawn?

You could have a collection of alien objects using the template specified by giblit (minus the static counter). Then when it comes time to drawing, you can use a simple for-loop and call the draw method of each alien object:

1
2
3
4
5
6
7
std::vector<Alien> collection;
...
// Time to draw
for (auto alien : collection)
{
     alien.draw(/*some parameter*/);
}
Topic archived. No new replies allowed.