From b3710a878fb2092d45dd3092a9997213ab440076 Mon Sep 17 00:00:00 2001 From: delvh Date: Tue, 23 Jul 2019 09:31:20 +0200 Subject: [PATCH] Implemented color swapping + swapColor method in Board + Button for swapping colors in MainWindow --- .classpath | 2 +- src/dev/kske/chess/game/Game.java | 11 +++++++++++ src/dev/kske/chess/ui/MainWindow.java | 6 ++++++ src/dev/kske/chess/ui/MenuBar.java | 5 +++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.classpath b/.classpath index d0a08f5..83f2c4a 100644 --- a/.classpath +++ b/.classpath @@ -7,7 +7,7 @@ - + diff --git a/src/dev/kske/chess/game/Game.java b/src/dev/kske/chess/game/Game.java index 18d3228..095f17b 100644 --- a/src/dev/kske/chess/game/Game.java +++ b/src/dev/kske/chess/game/Game.java @@ -108,6 +108,17 @@ public class Game { public void disconnect() { players.values().forEach(Player::disconnect); } + + public void swapColors() { + players.values().forEach(Player::cancelMove); + Player white = players.get(Color.WHITE); + Player black = players.get(Color.BLACK); + white.setColor(Color.BLACK); + black.setColor(Color.WHITE); + players.put(Color.WHITE, black); + players.put(Color.BLACK, white); + players.get(board.getActiveColor()).requestMove(); + } public Board getBoard() { return board; } } diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index b776425..351ef45 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -67,7 +67,13 @@ public class MainWindow { JButton btnRestart = new JButton("Restart"); btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); + + JButton btnSwapColors = new JButton("Swap Colors"); + btnSwapColors.addActionListener((evt) -> game.swapColors()); + toolPanel.add(btnRestart); + toolPanel.add(btnSwapColors); + mframe.pack(); mframe.setLocationRelativeTo(null); } diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index a502474..fe2b806 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -34,7 +34,7 @@ public class MenuBar extends JMenuBar { JMenuItem aiMenuItem = new JMenuItem("Game against artificial opponent"); JMenuItem aiVsAiMenuItem = new JMenuItem("Watch AI vs. AI"); JMenuItem uciMenuItem = new JMenuItem("UCI"); - + naturalMenuItem.addActionListener((evt) -> startGame(Game.createNatural(boardPane))); aiMenuItem.addActionListener((evt) -> { @@ -53,11 +53,12 @@ public class MenuBar extends JMenuBar { JOptionPane.QUESTION_MESSAGE); if (enginePath != null) startGame(Game.createUCI(boardPane, enginePath)); }); - + gameMenu.add(naturalMenuItem); gameMenu.add(aiMenuItem); gameMenu.add(aiVsAiMenuItem); gameMenu.add(uciMenuItem); + add(gameMenu);