I know, I know. This has been asked a lot of times. I am not looking for an algorithm. I think my algorithm is not working properly.
Here is the algorithm I am using:
public void onFirstMove (int moveX, int moveY) {
setFirstMove (false);
Random r = new Random ();
for (int i = 0 ; i < 10 ; i++) {
int x;
int y;
do {
x = r.nextInt (9);
y = r.nextInt (9);
} while (tileMatrix[x][y].hasMine () &&
moveX == x && moveY == y);
tileMatrix[x][y].setMine ();
}
timer.startTimer ();
}
I put it in the onFirstMove method because I don't want the player to lose on the first move. As you can see, I made it keep trying to find x and y coords while it is the same as the position of the first move.
while (tileMatrix[x][y].hasMine () &&
moveX == x && moveY == y);
And now it has 2 known bugs:
It sometimes generates 9 mines instead of 10. I know this because when I lose, it displays where all the mines at.
It sometimes does generate a mine at the position of the first move.