Java 如何将所有值设置回原始起始值

Java 如何将所有值设置回原始起始值,java,Java,我有一个类似于生活游戏的游戏。这个游戏涉及建造一座房子和重新安排邻居等等。我想重新启动游戏,只需将所有这些值设置回原始的开始值。如何使用代码来实现这一点。我懂它的英文,但似乎不能把它转换成代码 这是我的一些主程序(如果有人想让我发布整个主程序,我可以),但为了简单起见,我不想把你们搞糊涂 所以我想要的是:要重新启动游戏,只需将所有这些值设置回原始的开始值 一些主要节目: public class Ghetto extends JFrame implements ActionListener,

我有一个类似于生活游戏的游戏。这个游戏涉及建造一座房子和重新安排邻居等等。我想重新启动游戏,只需将所有这些值设置回原始的开始值。如何使用代码来实现这一点。我懂它的英文,但似乎不能把它转换成代码

这是我的一些主程序(如果有人想让我发布整个主程序,我可以),但为了简单起见,我不想把你们搞糊涂

所以我想要的是:要重新启动游戏,只需将所有这些值设置回原始的开始值

一些主要节目:

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();
     }
  });

 }

}

通常,“重置”最简单的方法是不重置。扔掉这个东西,做一个全新的!构造器将为您处理所有事情,您不必担心遗漏什么。如果确实需要,可以创建一个reset方法来执行所有必要的设置,并让构造函数调用它。您必须确保捕获所有内容,特别是不能使用任何类似于
Foo x=bar
的字段初始化,也不能使用任何初始化程序块

我建议的方法是:

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.
如果这些“值”存储在单独的类中,比如类“GameProperties”,那么您只需要通过创建GameProperties的新实例来调用构造函数。 构造函数应该注意指定默认值。 因此,假设您在贫民区类中有一个名为道具的游戏属性实例:

添加GameProperties类的新实例,并在Ghetto类中更改resetWithCurrent:

   GameProperties props = new GameProperties(); 

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

    }
在使用游戏属性时删除常量值。使用getter方法 以获取属性值

创建新类:

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;
}

}

希望有帮助。

您希望重置哪些字段?你所拥有的一切几乎都是一场决赛。似乎没有什么东西的状态能改变这么多。我们也看不到performStep()的实现。只要我进行重置,我就可以更改我的设置,而不是将其设置为最终设置。如果有帮助的话,我可以添加performStep()。@MarkW,
网格
在这里似乎不是最终的,而且可能很重要。然而,按照我的回答所描述的方法,这可能也是最终的。嗯,你能更具体一点,在代码中会是什么样子?谢谢。那将是在哪种方法下?当我点击重置按钮时,我的按钮如何知道并响应它?谢谢。@user3490502,在我的方法中,你把重置按钮从
贫民区
类中拿出来。重置按钮将由处理贫民区的任何类处理。@用户3490502,将
main
函数从
gheetto
类中取出。把它放在别的地方(另一类)。其他地方可能是重置按钮的合理位置。所有值都存储在我的主程序中。好的,那么道具是什么,我应该声明它是什么?还有为什么有一个数组列表,出于某种原因,它用红线下划线,什么是initializePlayerList();