better termination

This commit is contained in:
2025-04-12 13:04:48 +02:00
parent de733fcea2
commit 39c289cc47
4 changed files with 18 additions and 6 deletions

View File

@@ -98,4 +98,7 @@ public class CommandExecutor {
this.game = game;
}
public void close() {
this.dispatcher.stopService();
}
}

View File

@@ -2,7 +2,7 @@ package chess.controller.event;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
@@ -12,7 +12,7 @@ import chess.model.Coordinate;
public class GameDispatcher implements GameListener{
private final List<GameListener> listeners;
private final Executor executor;
private final ExecutorService executor;
public GameDispatcher() {
this.listeners = new ArrayList<>();
@@ -71,5 +71,9 @@ public class GameDispatcher implements GameListener{
public void updateDisplay() {
asyncForEachCall((l) -> l.updateDisplay());
}
public void stopService() {
this.executor.shutdown();
}
}

View File

@@ -158,7 +158,7 @@ public class Console implements GameListener {
public void gameEnded(){
System.out.println("Thank you for playing!");
System.exit(0);
this.commandExecutor.close();
}
@Override

View File

@@ -205,7 +205,7 @@ public class Window extends JFrame implements GameListener {
public void winnerIs(chess.model.Color color) {
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(this, "Victory of " + color);
this.dispose();
onGameEnd();
});
}
@@ -229,7 +229,7 @@ public class Window extends JFrame implements GameListener {
public void patSituation() {
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(this, "Pat. It's a draw!");
this.dispose();
onGameEnd();
});
}
@@ -240,6 +240,11 @@ public class Window extends JFrame implements GameListener {
});
}
private void onGameEnd() {
this.dispose();
this.commandExecutor.close();
}
@Override
public void gameStarted() {
buildBoard();