I’m trying to re-make minesweeper in an applet for a school project, and everything works fine, except for when the game ends. The first time through the code works fine, you either win or lose, and a face shows up in the middle (Happy/sad depending on if you won/lost). After the second time though, if you hit a mine on the same spot where the face appears, it is as if you clicked and ended the game, then clicked again to hit the face, and restarted the game. I have an if statement in mousePressed to check the game state, if if it either w(win) or l(lost) init() is run again, if it is in n state, it’s not determines and runs the code normally. It seems as if after the first run through the game both events occur on the same click, and I’m lost as to why. Any help would be appreciated.
public void mousePressed (MouseEvent event) {
x=event.getX();//Get x coordinate
y=event.getY();//Get y coordinate
colSpace = x/17;//Determines which column is clicked on (If 0-17px, returns 0, if 18-34, returns 2), has a flooring effect
rowSpace = y/16;//Determines which row is clicked on (If 0-16px, returns 0, if 17-32, returns 2)
if(won==’l’ || won==’y’) {
if(x>= 20 && x <= 48 && y>= 18 && y<=46){//These are the coordinates for the happy/sad face
init();
}
}else if(won==’n’) {
if (colSpace <= 3 && rowSpace <= 3){
Position = (rowSpace * 4) + colSpace;//Determines position in array using row/column previously determined (row 2, col 3 would be 8+2, or position 10)
if(result[Position] == 0) {//If the tile has a normal tile graphic, make it empty
checkMines(colSpace, rowSpace);
remainTiles -= 1;
}else if(result[Position] == 1){//If there’s a mine on that tile, game over
gameOver();
}
Position = 0;
if(remainTiles == 0) win();//Checks to see if the user has won
repaint();
}
}
}
This is the code for mousePressed, and I am attaching the entire project as well.When you run the game the text below the minesweeper grid is gamestate, row, column, and a flag I have in the code that I was using to track which code was being run.
[upl-file uuid=708ee0e8-df61-48b0-ae87-1f2bb8e1f619 size=28kB]minesweeper.zip