Reputation: 3
I have this and its in an action listener. If i lose it will display 1. It then resets the board and 1 will stay there which is what I want.. Then if i lose again it doesn't change the 1 to a 2. Same problems goes for wins. I hope this is enough information.
if (game.getGameStatus() == GameStatus.Lost) {
displayBoard();
JOptionPane.showMessageDialog(null, "You Lose \nThe game will reset");
//exposeMines = false;
game.reset();
displayBoard();
int losses = 0;
losses = losses + 1;
String lost = Integer.toString(losses);
jtfLosses.setText(String.format(lost));
}
Upvotes: 0
Views: 94
Reputation: 25094
You are defining "int losses = 0" everytime you enter your if-statement, it is getting overwritten by 0, then you add 1 again, which results in 1 everytime.
so defining
int losses = 0;
before
if (game.getGameStatus() == GameStatus.Lost) {
...
would fix the problem, like this:
int losses = 0;
if (game.getGameStatus() == GameStatus.Lost) {
...
but be careful, this solution is just to show you, what the problem is, to actually make it correct, you should use a member-variable, and not pollute the global name-space
Upvotes: 1
Reputation: 1976
I suggest you to use properties file to store this information like here you use
PlayerLoose = 2
PlayerWin = 3
than you can use
int loose =(prop.getProperty("PlayerLoose"));
loose++;
prop.setProperty("PlayerLoose", loose+"");
String lost = Integer.toString(loose);
jtfLosses.setText(String.format(lost));
Upvotes: 0
Reputation: 3825
You overwrite the number of losses with 0 every time, then increment that by one. Initialize the variable losses
outside of this method and don't do it every time you want to increment it.
Upvotes: 1