我想写一个遗传程序通过游戏玩,但我遇到了一点障碍。当我把这个代码:等待方法完成,并与System.println怪异的交互
public double playMap (GameBoard gb, Player p) {
gb.playerController = p;
Game g = new Game(gb);
int initHP = 0;
for (Unit u : gb.enemy.units) {
initHP += u.maxHP;
}
g.playGame(false);
int finalHP = 0;
for (Unit u : gb.enemy.units) {
finalHP += u.currHP;
}
System.out.println(" " + initHP);
System.out.println(" " + finalHP);
System.out.println(" " + (finalHP - initHP));
if (initHP == finalHP) {
return -10;
}
return initHP - finalHP;
}
的g.playGame()线没有足够的时间来完成,而我正在从功能上不正确的结果。我可以等待游戏结束了,游戏结束了,但是没有使用相同的while循环没有打印语句。我知道必须有更优雅的解决方案,而且我似乎无法实现我所见过的方法。另外如果有人知道为什么我需要在while循环中的打印语句来让它等待,那也会很棒。
在此先感谢。
ADDED玩笑:
public void playGame(boolean visual) {
Global.visual = visual;
if (Global.visual) {
JFrame application = new JFrame();
application.setBackground(Color.DARK_GRAY);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(this);
application.setSize(500, 400); // window is 500 pixels wide, 400 high
application.setVisible(true);
}
PlayerInput pi = new PlayerInput();
this.addKeyListener(pi);
final Timer timer = new Timer(10/60, null);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pi.addPressed();
if (update(pi)) {
// application.setVisible(false);
// application.dispose();
System.out.println(gb.toString());
isDone = true;
timer.stop();
}
pi.reset();
}
};
timer.addActionListener(listener);
timer.start();
while (!isDone) {
System.out.println(isDone);
}
}
你能告诉我们如何游戏类和它的方法playGame()是否被实现? –
我添加的方法和类是相当大的,我将只是链接的混帐回购协议。 [https://github.com/RyTaus/SURP/blob/master/DoH/Game.java](https://github.com/RyTaus/SURP/blob/master/DoH/Game.java) –