Jump to content

minsweeper random locations

TheBean
Go to solution Solved by WereCatf,
15 minutes ago, Saksham said:

index = rand.nextInt(gridSize);

index = rand.nextInt(gridSize * gridSize);

I am trying to make minesweeper in Java. But, I am noticing a very strange issue. I am choosing the locations of the mines at random, but, they seem like they are trying to "hug" the edges. about 90% of the time, there are at least 2 mines at the edge (3 mines in a 6x6 grid just for testing purposes).

 

Here is my code that I am using to randomly enter the mines. it chooses a random number between 0 and the number of possible cells. then based on that index, it places a mine there as long as a mine isnt already there. if there is, it will try another index. it keeps going until it hits the number of mines I want to place. 

int index = rand.nextInt(gridSize * gridSize);
// place mines into grid ([row][column])
for (int i = 0; i < numMines; i++) {
	while (grid[index / gridSize][index % gridSize].isMine()) {
		index = rand.nextInt(gridSize);
	}
	grid[index / gridSize][index % gridSize].setMine(true);
}

 

Edited by Saksham
Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Saksham said:

index = rand.nextInt(gridSize);

index = rand.nextInt(gridSize * gridSize);

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, WereCatf said:

index = rand.nextInt(gridSize * gridSize);

wow... I can't believe I missed that obvious mistake

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Saksham said:

wow... I can't believe I missed that obvious mistake

I know the feeling 😃

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×