/    Sign up×
Community /Pin to ProfileBookmark

Minesweeper Applet help

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[/upl-file]

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@EbolaauthorNov 25.2005 — *Smacks head*

I'd been stuck on this for about two days, I just realized each time the init() was called if's as if I was clicking twice, then three times etc.

Good to have figured this out at least >_<
×

Success!

Help @Ebola spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...