Implemented color swapping

+ swapColor method in Board
+ Button for swapping colors in MainWindow
This commit is contained in:
Kai S. K. Engelbart 2019-07-23 09:31:20 +02:00
parent a6fcaee70e
commit 2b00116837
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
4 changed files with 21 additions and 3 deletions

View File

@ -7,7 +7,7 @@
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_212"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
</attributes> </attributes>

View File

@ -108,6 +108,17 @@ public class Game {
public void disconnect() { public void disconnect() {
players.values().forEach(Player::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; } public Board getBoard() { return board; }
} }

View File

@ -67,7 +67,13 @@ public class MainWindow {
JButton btnRestart = new JButton("Restart"); JButton btnRestart = new JButton("Restart");
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); 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(btnRestart);
toolPanel.add(btnSwapColors);
mframe.pack(); mframe.pack();
mframe.setLocationRelativeTo(null); mframe.setLocationRelativeTo(null);
} }

View File

@ -34,7 +34,7 @@ public class MenuBar extends JMenuBar {
JMenuItem aiMenuItem = new JMenuItem("Game against artificial opponent"); JMenuItem aiMenuItem = new JMenuItem("Game against artificial opponent");
JMenuItem aiVsAiMenuItem = new JMenuItem("Watch AI vs. AI"); JMenuItem aiVsAiMenuItem = new JMenuItem("Watch AI vs. AI");
JMenuItem uciMenuItem = new JMenuItem("UCI"); JMenuItem uciMenuItem = new JMenuItem("UCI");
naturalMenuItem.addActionListener((evt) -> startGame(Game.createNatural(boardPane))); naturalMenuItem.addActionListener((evt) -> startGame(Game.createNatural(boardPane)));
aiMenuItem.addActionListener((evt) -> { aiMenuItem.addActionListener((evt) -> {
@ -53,11 +53,12 @@ public class MenuBar extends JMenuBar {
JOptionPane.QUESTION_MESSAGE); JOptionPane.QUESTION_MESSAGE);
if (enginePath != null) startGame(Game.createUCI(boardPane, enginePath)); if (enginePath != null) startGame(Game.createUCI(boardPane, enginePath));
}); });
gameMenu.add(naturalMenuItem); gameMenu.add(naturalMenuItem);
gameMenu.add(aiMenuItem); gameMenu.add(aiMenuItem);
gameMenu.add(aiVsAiMenuItem); gameMenu.add(aiVsAiMenuItem);
gameMenu.add(uciMenuItem); gameMenu.add(uciMenuItem);
add(gameMenu); add(gameMenu);