Added player management and related UI components
+ Game Mode selection dialog + Game class for turn management + Abstract Player class with NaturalPlayer implementation - Moved mouse input handling from BoardPanel to NaturalPlayer
This commit is contained in:
@ -5,8 +5,6 @@ import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -15,7 +13,6 @@ import javax.swing.JPanel;
|
||||
|
||||
import dev.kske.chess.board.Board;
|
||||
import dev.kske.chess.board.Move;
|
||||
import dev.kske.chess.board.Position;
|
||||
import dev.kske.chess.event.GameEvent;
|
||||
import dev.kske.chess.event.GameEventListener;
|
||||
|
||||
@ -61,33 +58,6 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
||||
});
|
||||
|
||||
setSize(getPreferredSize());
|
||||
|
||||
// Add a mouse adapter for testing piece movement
|
||||
addMouseListener(new MouseAdapter() {
|
||||
|
||||
private Position pos;
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent evt) {
|
||||
if (pos == null) {
|
||||
pos = new Position(evt.getPoint().x / tileSize, evt.getPoint().y / tileSize);
|
||||
|
||||
if (board.get(pos) != null) {
|
||||
displayMoves.clear();
|
||||
displayMoves.addAll(board.getMoves(pos));
|
||||
repaint();
|
||||
}
|
||||
} else {
|
||||
Position dest = new Position(evt.getPoint().x / tileSize, evt.getPoint().y / tileSize);
|
||||
|
||||
if (board.attemptMove(new Move(pos, dest))) {
|
||||
displayMoves.clear();
|
||||
repaint();
|
||||
}
|
||||
pos = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -133,7 +103,26 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts the board to its initial state
|
||||
* Displays move destinations on the board.
|
||||
*
|
||||
* @param moves The moves to display
|
||||
*/
|
||||
public void displayMoves(List<Move> moves) {
|
||||
displayMoves.clear();
|
||||
displayMoves.addAll(moves);
|
||||
repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all display moves.
|
||||
*/
|
||||
public void clearDisplayMoves() {
|
||||
displayMoves.clear();
|
||||
repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts the board to its initial state.
|
||||
*/
|
||||
public void reset() {
|
||||
board.initializeDefaultPositions();
|
||||
@ -149,6 +138,8 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
||||
@Override
|
||||
public Dimension getPreferredSize() { return new Dimension(480, 480); }
|
||||
|
||||
public int getTileSize() { return tileSize; }
|
||||
|
||||
public Board getBoard() { return board; }
|
||||
|
||||
public void setBoard(Board board) {
|
||||
|
49
src/dev/kske/chess/ui/GameModeDialog.java
Normal file
49
src/dev/kske/chess/ui/GameModeDialog.java
Normal file
@ -0,0 +1,49 @@
|
||||
package dev.kske.chess.ui;
|
||||
|
||||
import java.awt.FlowLayout;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JDialog;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Create the dialog.
|
||||
*/
|
||||
public GameModeDialog(BoardPanel boardPanel) {
|
||||
super();
|
||||
setModal(true);
|
||||
setTitle("Game Mode Selection");
|
||||
setBounds(100, 100, 231, 99);
|
||||
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
|
||||
|
||||
JButton btnNatural = new JButton("Game against natural opponent");
|
||||
btnNatural.addActionListener((evt) -> {
|
||||
Map<Color, Player> players = new HashMap<>();
|
||||
players.put(Color.WHITE, new NaturalPlayer(boardPanel.getBoard(), Color.WHITE, boardPanel));
|
||||
players.put(Color.BLACK, new NaturalPlayer(boardPanel.getBoard(), Color.BLACK, boardPanel));
|
||||
new Game(players, boardPanel.getBoard()).start();
|
||||
dispose();
|
||||
});
|
||||
getContentPane().add(btnNatural);
|
||||
|
||||
JButton btnAI = new JButton("Game against AI");
|
||||
getContentPane().add(btnAI);
|
||||
}
|
||||
}
|
@ -64,5 +64,8 @@ public class MainWindow {
|
||||
btnRestart.addActionListener((evt) -> boardPanel.reset());
|
||||
toolPanel.add(btnRestart);
|
||||
mframe.pack();
|
||||
|
||||
// Display dialog for game mode selection
|
||||
new GameModeDialog(boardPanel).setVisible(true);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user