From 7a4266d4a7850240f9339203b7e533eef9a27037 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 16 Jul 2019 18:24:48 +0200 Subject: [PATCH] Fixed game state related bugs --- src/dev/kske/chess/game/Game.java | 10 ++++------ src/dev/kske/chess/ui/MainWindow.java | 2 +- src/dev/kske/chess/ui/MenuBar.java | 4 +++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/dev/kske/chess/game/Game.java b/src/dev/kske/chess/game/Game.java index e4e0a5f..6a30f6a 100644 --- a/src/dev/kske/chess/game/Game.java +++ b/src/dev/kske/chess/game/Game.java @@ -24,10 +24,10 @@ public class Game { private BoardComponent boardComponent; public Game(Map players, BoardPane boardPane) { - this.players = players; + this.players = players; this.overlayComponent = boardPane.getOverlayComponent(); this.boardComponent = boardPane.getBoardComponent(); - this.board = boardComponent.getBoard(); + this.board = boardComponent.getBoard(); // Initialize the game variable in each player players.values().forEach(player -> player.setGame(this)); @@ -46,10 +46,9 @@ public class Game { System.out.printf("%s in check!%n", player.color.opposite()); default: boardComponent.repaint(); - overlayComponent.displayArrow(move); players.get(player.color.opposite()).requestMove(); - } + overlayComponent.displayArrow(move); } else player.requestMove(); } @@ -57,12 +56,11 @@ public class Game { players.get(Color.WHITE).requestMove(); } - public void restart() { + public void reset() { players.forEach((k, v) -> v.cancelMove()); board.initializeDefaultPositions(); boardComponent.repaint(); overlayComponent.clearDots(); overlayComponent.clearArrow(); - start(); } } diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index c423013..54be5ef 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -65,7 +65,7 @@ public class MainWindow { mframe.getContentPane().add(toolPanel, BorderLayout.NORTH); JButton btnRestart = new JButton("Restart"); - btnRestart.addActionListener((evt) -> { if (game != null) game.restart(); }); + btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); toolPanel.add(btnRestart); mframe.pack(); mframe.setLocationRelativeTo(null); diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index f6ba11a..25386bb 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -80,9 +80,11 @@ public class MenuBar extends JMenuBar { } private void startGame() { - // TODO: Re-init board and overlay component Game game = new Game(players, boardPane); mainWindow.setGame(game); + + // Update board and board component + game.reset(); game.start(); } }