Jump to content

minsweeper random locations

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
https://linustechtips.com/topic/1177159-minsweeper-random-locations/
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

×