Moved tests in test source folder, replaced GameModeDialog by MenuBar
This commit is contained in:
@ -1,43 +0,0 @@
|
||||
package dev.kske.chess.test;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import dev.kske.chess.board.Board;
|
||||
import dev.kske.chess.board.Piece.Color;
|
||||
import dev.kske.chess.board.Queen;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
* File: <strong>BoardTest.java</strong><br>
|
||||
* Created: <strong>08.07.2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||
*/
|
||||
class BoardTest {
|
||||
|
||||
Board board;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
board = new Board();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link dev.kske.chess.board.Board#clone()}.
|
||||
*/
|
||||
@Test
|
||||
void testClone() {
|
||||
Board clone = (Board) board.clone();
|
||||
assertNotSame(clone, board);
|
||||
assertNotSame(clone.getBoardArr(), board.getBoardArr());
|
||||
|
||||
clone.getBoardArr()[0][0] = new Queen(Color.BLACK, clone);
|
||||
assertNotEquals(clone.getBoardArr()[0][0], board.getBoardArr()[0][0]);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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; }
|
||||
|
80
src/dev/kske/chess/ui/MenuBar.java
Normal file
80
src/dev/kske/chess/ui/MenuBar.java
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user