user3490502
user3490502

Reputation: 9

How to set all of the values back to the original start values

I have a game that is similar to the game of life. the game deals with creating a house and rearranging the neighbors and such. I WANT to restart the game, simply need to set all of these values back to the original start values. How do I do that with a code. I understand the English of it but cant seem to convert it to a code.

This is some of my main program (If anyone want me to post the whole main program I can) but to make it simple and I dont want to confuse you guys.

So what I WANT: to restart the game, simply I want to set all of these values back to the original start values.

Some Of Main Program:

public class Ghetto extends JFrame implements ActionListener,      MouseListener,MouseMotionListener 
{

protected Grids theGrid;
JButton resetButton;
javax.swing.Timer timer; // generates ticks that drive the animation

public final static int SIZE = 5;
public final static int BLUE = 10;
public final static int RED = 8;
public final static int DIVERSITY_PERCENTAGE = 70;

public static void main(String[] args) 
{
   new Ghetto();

}

public Ghetto() {
setDefaultCloseOperation(EXIT_ON_CLOSE);

addMouseListener(this);
addMouseMotionListener(this);
setLayout(new FlowLayout());

theGrid = new Grids(SIZE, BLUE, RED, DIVERSITY_PERCENTAGE);
add(theGrid);

resetButton = new JButton("Reset");
add(resetButton);
resetButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        resetWithCurrent();
    }
});


setSize(new Dimension(550, 600));
setVisible(true);
}



//public void resetWithCurrent()
//{

//}

@Override
public void actionPerformed(ActionEvent e) 
{
timer.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e) 
    {
        performStep();
     }
  });

 }

}

Upvotes: 0

Views: 66

Answers (2)

Jack Daniels
Jack Daniels

Reputation: 1

If these "values" are stored in a separate class, say class "GameProperties" then you just need to invoke the constructor by creating a new instance of the GameProperties. The constructor should take care of assigning default values. So, assuming you have an instance of GameProperties within Ghetto class named props:

Add new instance of GameProperties class and change resetWithCurrent in Ghetto class:

   GameProperties props = new GameProperties(); 

   public void resetWithCurrent(){
    //This will reset the values to their defaults as defined in the constructor
    props = new GameProperties();

    }

Remove the values constants as you are using your GameProperties. Use the getters methods to obtain the properties values.

Create new class:

public class GameProperties {
//assign initial default values 
private int size= 5;
private int blue= 10;
private int red= 8;
private int diversity_percentage= 70;

 //calling default constructor will set the properties default values
public GameProperties(){

}

public int getSize(){
     return size;
}

public int getBlueValue(){
     return size;
}

public int getRedValue(){
     return size;
}

public int getDiversityPercentage(){
     return diversity_percentage;
}

}

Hope it helps.

Upvotes: 0

dfeuer
dfeuer

Reputation: 48591

Typically, the eaiest way to "reset" is not to. Just throw away the object and make a brand new one! The constructor will take care of everything for you, and you won't have to worry about missing something. If you really need to, you can make a reset method that performs all the necessary setting, and have the constructor call it. You have to be sure to catch everything, so in particular you can't use any field initializations that look like Foo x = bar and you can't use any initializer blocks.

The approach I suggest:

Ghetto ghetto = new Ghetto();

//Do stuff with the ghetto.

ghetto = new Ghetto();

//BLAM! The old ghetto is *gone*, and we have a new one to play with.

Upvotes: 1

Related Questions