I created a GUI that will be 10 x 10 and have 100 buttons. I had to insert 20 treasures and have the other 80 be empty. This is the code I used. Sometimes I get 17 treasures, sometimes 18, sometimes 19. How do I fix this?
Thank you in advance.
Random random = new Random();
for (int index = 0; index < 20; index++)
{
int insertionIndex = random.nextInt(99)+1;
buttonGrid[insertionIndex] = new TreasureButton(treasureGame, this);
}
// Loop to add EmptyButton()'s into remaining null indexes within the buttonGrid
for (int index = 0; index < 100; index++)
{
// if the current index is null, add an EmptyButton() into it
if (buttonGrid[index] == null)
{
buttonGrid[index] = new EmptyButton(treasureGame, this);
}
}
// Loop to add all of the contents of the buttonGrid into the gamePanel
for (int index = 0; index < 100; index++)
{
gridPanel.add(buttonGrid[index]);
}