From f65d7ae2a3a5618c9ed63dda4251248367fa4cd8 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 16 Jul 2019 11:58:51 +0200 Subject: [PATCH] Moved tests in test source folder, replaced GameModeDialog by MenuBar --- .classpath | 5 + .gitignore | 3 +- src/dev/kske/chess/ui/GameModeDialog.java | 94 ------------------- src/dev/kske/chess/ui/MainWindow.java | 14 +-- src/dev/kske/chess/ui/MenuBar.java | 80 ++++++++++++++++ .../dev/kske/chess/test/BoardTest.java | 0 6 files changed, 95 insertions(+), 101 deletions(-) delete mode 100644 src/dev/kske/chess/ui/GameModeDialog.java create mode 100644 src/dev/kske/chess/ui/MenuBar.java rename {src => test}/dev/kske/chess/test/BoardTest.java (100%) diff --git a/.classpath b/.classpath index 8623669..83f2c4a 100644 --- a/.classpath +++ b/.classpath @@ -2,6 +2,11 @@ + + + + + diff --git a/.gitignore b/.gitignore index bb6204f..91e622d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .metadata bin/ +/bin_test/ tmp/ *.tmp *.bak @@ -20,4 +21,4 @@ local.properties .recommenders/ # Annotation Processing -.apt_generated/ \ No newline at end of file +.apt_generated/ diff --git a/src/dev/kske/chess/ui/GameModeDialog.java b/src/dev/kske/chess/ui/GameModeDialog.java deleted file mode 100644 index d7e04c5..0000000 --- a/src/dev/kske/chess/ui/GameModeDialog.java +++ /dev/null @@ -1,94 +0,0 @@ -package dev.kske.chess.ui; - -import java.awt.FlowLayout; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.util.HashMap; -import java.util.Map; - -import javax.swing.JButton; -import javax.swing.JDialog; - -import dev.kske.chess.board.Board; -import dev.kske.chess.board.Piece.Color; -import dev.kske.chess.game.Game; -import dev.kske.chess.game.NaturalPlayer; -import dev.kske.chess.game.Player; -import dev.kske.chess.game.ai.AIPlayer; - -/** - * Project: Chess
- * File: GameModeDialog.java
- * Created: 06.07.2019
- * Author: Kai S. K. Engelbart - */ -public class GameModeDialog extends JDialog { - - private static final long serialVersionUID = 5470026233924735607L; - - private final MainWindow parent; - private final Board board; - private final OverlayComponent overlayComponent; - private final BoardComponent boardComponent; - private final Map players; - - /** - * Create the dialog. - */ - public GameModeDialog(MainWindow parent, BoardPane boardPane) { - super(); - setModal(true); - setTitle("Game Mode Selection"); - setBounds(100, 100, 231, 133); - setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); - - // Exit application when the close button is pressed - addWindowListener(new WindowAdapter() { - - @Override - public void windowClosing(WindowEvent evt) { - System.exit(0); - } - }); - - this.parent = parent; - boardComponent = boardPane.getBoardComponent(); - overlayComponent = boardPane.getOverlayComponent(); - board = boardComponent.getBoard(); - players = new HashMap<>(); - - JButton btnNatural = new JButton("Game against natural opponent"); - btnNatural.addActionListener((evt) -> { - players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent)); - players.put(Color.BLACK, new NaturalPlayer(board, Color.BLACK, overlayComponent)); - startGame(); - }); - getContentPane().add(btnNatural); - - JButton btnAI = new JButton("Game against AI"); - btnAI.addActionListener((evt) -> { - players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent)); - players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 5)); - startGame(); - }); - getContentPane().add(btnAI); - - JButton btnAI2 = new JButton("AI against AI"); - btnAI2.addActionListener((evt) -> { - players.put(Color.WHITE, new AIPlayer(board, Color.WHITE, 5)); - players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 4)); - startGame(); - }); - getContentPane().add(btnAI2); - - setLocationRelativeTo(null); - } - - private void startGame() { - Game game = new Game(players, boardComponent); - parent.setGame(game); - game.start(); - dispose(); - } -} diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index 2c9c7fd..c423013 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -18,8 +18,9 @@ import dev.kske.chess.game.Game; */ public class MainWindow { - private JFrame mframe; - private Game game; + private JFrame mframe; + private BoardPane boardPane; + private Game game; /** * Launch the application. @@ -54,10 +55,12 @@ public class MainWindow { mframe.setBounds(100, 100, 494, 565); mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - BoardPane boardPane = new BoardPane(); + boardPane = new BoardPane(); boardPane.getBoardComponent().setBoard(new Board()); mframe.getContentPane().add(boardPane, BorderLayout.CENTER); + mframe.setJMenuBar(new MenuBar(this)); + JPanel toolPanel = new JPanel(); mframe.getContentPane().add(toolPanel, BorderLayout.NORTH); @@ -66,11 +69,10 @@ public class MainWindow { toolPanel.add(btnRestart); mframe.pack(); mframe.setLocationRelativeTo(null); - - // Display dialog for game mode selection - new GameModeDialog(this, boardPane).setVisible(true); } + public BoardPane getBoardPane() { return boardPane; } + public Game getGame() { return game; } public void setGame(Game game) { this.game = game; } diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java new file mode 100644 index 0000000..16f41b3 --- /dev/null +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -0,0 +1,80 @@ +package dev.kske.chess.ui; + +import java.util.HashMap; +import java.util.Map; + +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; + +import dev.kske.chess.board.Board; +import dev.kske.chess.board.Piece.Color; +import dev.kske.chess.game.Game; +import dev.kske.chess.game.NaturalPlayer; +import dev.kske.chess.game.Player; +import dev.kske.chess.game.ai.AIPlayer; + +/** + * Project: Chess
+ * File: MenuBar.java
+ * Created: 16.07.2019
+ * Author: Kai S. K. Engelbart + */ +public class MenuBar extends JMenuBar { + + private static final long serialVersionUID = -7221583703531248228L; + + private final MainWindow mainWindow; + private final OverlayComponent overlayComponent; + private final BoardComponent boardComponent; + private final Board board; + private final Map players; + + public MenuBar(MainWindow mainWindow) { + this.mainWindow = mainWindow; + overlayComponent = mainWindow.getBoardPane().getOverlayComponent(); + boardComponent = mainWindow.getBoardPane().getBoardComponent(); + board = boardComponent.getBoard(); + players = new HashMap<>(); + + initGameMenu(); + } + + private void initGameMenu() { + JMenu gameMenu = new JMenu("Game"); + + JMenuItem naturalMenuItem = new JMenuItem("Game against natural opponent"); + JMenuItem aiMenuItem = new JMenuItem("Game against artificial opponent"); + JMenuItem aiVsAiMenuItem = new JMenuItem("Watch AI vs. AI"); + + naturalMenuItem.addActionListener((evt) -> { + players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent)); + players.put(Color.BLACK, new NaturalPlayer(board, Color.BLACK, overlayComponent)); + startGame(); + }); + + aiMenuItem.addActionListener((evt) -> { + players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent)); + players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 5)); + startGame(); + }); + + aiVsAiMenuItem.addActionListener((evt) -> { + players.put(Color.WHITE, new AIPlayer(board, Color.WHITE, 5)); + players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 4)); + startGame(); + }); + + gameMenu.add(naturalMenuItem); + gameMenu.add(aiMenuItem); + gameMenu.add(aiVsAiMenuItem); + + add(gameMenu); + } + + private void startGame() { + Game game = new Game(players, boardComponent); + mainWindow.setGame(game); + game.start(); + } +} diff --git a/src/dev/kske/chess/test/BoardTest.java b/test/dev/kske/chess/test/BoardTest.java similarity index 100% rename from src/dev/kske/chess/test/BoardTest.java rename to test/dev/kske/chess/test/BoardTest.java