Moved tests in test source folder, replaced GameModeDialog by MenuBar

This commit is contained in:
Kai S. K. Engelbart 2019-07-16 11:58:51 +02:00
parent 7402653f17
commit f65d7ae2a3
6 changed files with 95 additions and 101 deletions

View File

@ -2,6 +2,11 @@
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="res"/> <classpathentry kind="src" path="res"/>
<classpathentry kind="src" output="bin_test" path="test">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.metadata .metadata
bin/ bin/
/bin_test/
tmp/ tmp/
*.tmp *.tmp
*.bak *.bak

View File

@ -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: <strong>Chess</strong><br>
* File: <strong>GameModeDialog.java</strong><br>
* Created: <strong>06.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
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<Color, Player> 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();
}
}

View File

@ -19,6 +19,7 @@ import dev.kske.chess.game.Game;
public class MainWindow { public class MainWindow {
private JFrame mframe; private JFrame mframe;
private BoardPane boardPane;
private Game game; private Game game;
/** /**
@ -54,10 +55,12 @@ public class MainWindow {
mframe.setBounds(100, 100, 494, 565); mframe.setBounds(100, 100, 494, 565);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoardPane boardPane = new BoardPane(); boardPane = new BoardPane();
boardPane.getBoardComponent().setBoard(new Board()); boardPane.getBoardComponent().setBoard(new Board());
mframe.getContentPane().add(boardPane, BorderLayout.CENTER); mframe.getContentPane().add(boardPane, BorderLayout.CENTER);
mframe.setJMenuBar(new MenuBar(this));
JPanel toolPanel = new JPanel(); JPanel toolPanel = new JPanel();
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH); mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
@ -66,11 +69,10 @@ public class MainWindow {
toolPanel.add(btnRestart); toolPanel.add(btnRestart);
mframe.pack(); mframe.pack();
mframe.setLocationRelativeTo(null); 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 Game getGame() { return game; }
public void setGame(Game game) { this.game = game; } public void setGame(Game game) { this.game = game; }

View File

@ -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: <strong>Chess</strong><br>
* File: <strong>MenuBar.java</strong><br>
* Created: <strong>16.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
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<Color, Player> 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();
}
}