Best approach? Don't...that's a lot of buttons...
But if you must, I'd start by looking at the actual value of
totalButtons
using the debugger, given that you update it in two places:
totalButtons += totalProb;
...
totalButtons += nProb;
And since it's your loop termination condition...you need to know what is happening to it.
So use the debugger, and put a breakpoint in the
while
loop. Step over the method calls and see what has happened to the value. Because if that doesn't go up at any time in the loop, you will get a stack overflow - just as you are.
"Thank you for your help! Actually it's a hidden object game of sorts, where the program will ask you to look for something in the grid. :) Like for example, look for 5 apples, etc.
I started over and so far it's working, but I still have to work on how there should only be an exact or a not too big number of the item being asked for in the grid, and I can't seem to do that without the endless recursion"
OK, so dump the randomness! :laugh:
Instead, set up a list of locations, and "shuffle" them randomly:
List<Item> ordered = new List<Item>();
for (int i = 0; i < 63; i++)
{
Item item = new Item(i);
...
ordered.Add(item);
}
List<Item> shuffled = new List<Item>();
while (ordered.Count > 0)
{
int remove = rand.Next(ordered.Count);
Item item = ordered[remove];
ordered.Remove(item);
shuffled.Add(item);
}
You then get a list of Items or locations or whatever in a random order and no need to recurse.
See what I mean?