Reputation: 23
I am creating a Monopoly game, and I've come across a problem for ending the game. For one class, I have an accessor method to get the variable that determines whether or not the game is over, with the variable "gameover" becoming true when someone goes bankrupt. In the other class, I created an object that calls this method, with the button that rolls the dice becoming disabled when the variable is true. My problem is that for someone reason the gameover variable never changes to true in the class with the button even though I set it to true when someone loses.
Accessor method:
public boolean getGame()
{
return gameover;
}
Example of when someone loses in the paint method:
if(player1.money < 0)
{
System.out.println("Player 1 is bankrupt, Player 2 wins!");
gameover = true;
}
Method that listens for mouse clicks on the button:
public void actionPerformed(ActionEvent evt)
{
repaint();
boolean condition = x.getGame();
if(condition == true)
{
b1.setEnabled(false);
}
}
Upvotes: 2
Views: 76
Reputation: 15874
I will suggest you create a method like the following :
private void endGame() {
this.gameover = true;
}
and then call from your method
if(player1.money < 0) {
System.out.println("Player 1 is bankrupt, Player 2 wins!");
endGame();
}
Upvotes: 1