0

I'm making a candy crush kinda game. I need to draw different colors of gems on the board randomly.

  class Gem
{
public:
int x; int y;
    Gem(){x = 0 ; y = 0;}
    void make(int x , int y, int color)
        {
            DrawRoundRect(x, y, 50 , 50 , colors[color], 10);
        }
};

Gem gem;
for(int i = 195 ; i <= 795 ; i = i+100)
{
    for(int j = 70 ; j <= 5000 ; j = j+100)
    {
        int col = Randomise(0,4);
        gem.make(i,j,col);
    }
}

All the gems drawn are of the same color. Also, this function gives a different random number on every redraw. Due to which the color of all the gems keeps changing with the movement of the mouse (The mouse and keyboard accelerate redraw of the screen). I have to move the gems downwards and yet want the color of that specific gem to remain the same throughout the game. How can I resolve it? The definition of Randomise( ) is:

int Randomise(int min, int max)
{
    srand((unsigned) time(0));
    long random;
    for (long i = min; i < 5; i++) 
    {
      random = (rand() % max) + 1;
      return random;
    }
}
  • The two biggest issues are that you seed your random number generator on every call to `Randomize` (only seed once, at the start of `main` generally), and that you don't actually make a new `Gem` for each new `Gem`. Your `Gem` class is acting more like a `GemDrawer` class. It doesn't save the state. – JohnFilleau Jun 05 '20 at 15:15
  • This is a DESIGN issue not a code issue. My recommendation is to have your `Gem` constructor take `x`, `y`, and `color`. Your `Gem` constructor should SAVE those values. You should CONSTRUCT your 5k * 795 gems and store them in a suitable container (likely a `std::vector` although a `std::unordered_map, Gem>` also makes sense to lookup which `Gem` is at which position). Then have some code that iterates through this container, and for each Gem, draws it. You may give `Gem` a `draw()` function if that helps. – JohnFilleau Jun 05 '20 at 15:17
  • Your `Randomise` function is also strange in that it starts iteration of a loop, but returns on the first iteration. That tells me you may be confused about what the loop is providing. If you can explain what you think the loop is doing to that function we may be able to help steer you in the right direction on that. – JohnFilleau Jun 05 '20 at 15:25
  • @JohnFilleau I assumed loop will call a different random number for each gem and save that then draw it. But I am wrong and badly stuck. – Asawira Emaan Khan Jun 07 '20 at 02:09
  • `time` returns the current time in seconds. So if you call the function a million times in the same second, you'll get the same random number sequence. YOU WANT TO SEED THE RANDOM NUMBER GENERATOR (generally) ONLY ONCE AT THE START OF YOUR PROGRAM - NOT EVERYTIME YOU GENERATE A RANDOM NUMBER. – JohnFilleau Jun 07 '20 at 23:10
  • Does this answer your question? [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – Peter O. Jun 15 '20 at 16:12

0 Answers0