   [C++] Code in if statement not getting executed (but it is?)
void Map::setBombs(){    int randomNumber = 0;    int bombsPlaced = 0;    while (bombsPlaced<numberOfBombs)    {   for(it = squares.begin();it!=squares.end();it++)   {  if (bombsPlaced<numberOfBombs)  { randomNumber = rand() % 4; if (randomNumber > 2) {(*it)->setAsBomb();std::cout << "Placed bomb." << std::endl;bombsPlaced++; }  }   }    }}Okay, in the last if statement in the nest, randomNumber is definitely more than 2 on occassions, as breakpoints have shown. bombsPlaced++ works (the program would infinitely loop otherwise, and breakpoints have also shown it increments). However, it doesn't print out "Placed bomb.", and the setAsBomb() function doesn't get executed. I have no idea why. Halp?   
  I know this is a bit of a bold statement, but is bombsPlace ever actually less than numberOfBombs? And perhaps some checks along the way like some print statments after each if, while and for statment respectively so that you can see where the program is kind of like this void Map::setBombs(){    int randomNumber = 0;    int bombsPlaced = 0;    while (bombsPlaced<numberOfBombs)    {    // print something to say you have passed the while loop   for(it = squares.begin();it!=squares.end();it++)   {   // print something to say you have passed the for loop  if (bombsPlaced<numberOfBombs)  { // print something to say you have passed the if statement. randomNumber = rand() % 4; if (randomNumber > 2) {(*it)->setAsBomb(); std::cout << "Placed bomb." << std::endl;bombsPlaced++; }  }   }    }};Quote:Original post by Googol PL3XI know this is a bit of a bold statement, but is bombsPlace ever actually less than numberOfBombs? And perhaps some checks along the way like some print statments after each if, while and for statment respectively so that you can see where the program is kind of like thisI know the variables are as they should be and all ifs are passing as true through breakpoints and local variable watches.   ;  Are you clearing the screen anywhere in the program after this function is called?   ;Quote:Original post by dwfaitfor(it = squares.begin();it!=squares.end();it++)Hm, I don't see a definition for the variable "it". You don't use a global variable for that which you recycle in setAsBomb(), do you?   ;  Your apparent intent is to distribute 'numberOfBombs' bombs among the 'squares', without bias (i.e. each square has an equal chance to receive a bomb). Unfortunately, it is easy to see that even if fixed, your algorithm will be biased towards placing bombs earlier in the grid. (A quick check: let numberOfBombs = 1 and let there be 3 squares. What's the chance of the bomb ending up in each square? Answer: 4/7, 2/7 and 1/7 respectively.) You can reduce this bias by reducing the chance of placing a bomb each time, but you only approach a fair distribution as that chance goes to zero (which would make it take literally forever to finish).A typical approach to this problem is to arbitrarily set up bombs in the first numberOfBombs locations, and then shuffle the container randomly. A standard library algorithm already exists to do both of those tasks:#include <algorithm> // in addition to anything elsevoid Map::setBombs(){    // Construct the range of squares that will be filled with bombs.    assert squares.size() >= numberOfBombs;   // This only works for random access iterators    // (e.g. if 'squares' is a vector). Otherwise, you will need to use    // std::advance to construct the iterator for the end of the range.    std::foreach(squares.begin(), squares.begin() + numberOfBombs, std::mem_fun(&Square::setAsBomb));    std::random_shuffle(squares.begin(), squares.end());}Yep, really that simple. Of course, this requires that the squares are free to be swapped around - i.e. that they don't remember information about "where they are". Which they oughtn't anyway. :)But I have to wonder: why not store the squares by value, instead of holding pointers to them as you do now?   