Improved BoardOverlay, disabled color swap in natural-vs-natural game
This commit is contained in:
parent
91962c01e0
commit
309495cfae
@ -87,7 +87,8 @@ public class Game {
|
|||||||
players.get(board.getActiveColor()).requestMove();
|
players.get(board.getActiveColor()).requestMove();
|
||||||
}
|
}
|
||||||
overlayComponent.displayArrow(move);
|
overlayComponent.displayArrow(move);
|
||||||
} else player.requestMove();
|
} else
|
||||||
|
player.requestMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@ -121,4 +122,6 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Board getBoard() { return board; }
|
public Board getBoard() { return board; }
|
||||||
|
|
||||||
|
public Map<Color, Player> getPlayers() { return players; }
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import javax.swing.JPanel;
|
|||||||
|
|
||||||
import dev.kske.chess.board.Piece.Color;
|
import dev.kske.chess.board.Piece.Color;
|
||||||
import dev.kske.chess.game.Game;
|
import dev.kske.chess.game.Game;
|
||||||
|
import dev.kske.chess.game.NaturalPlayer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
@ -20,6 +21,7 @@ import dev.kske.chess.game.Game;
|
|||||||
public class MainWindow {
|
public class MainWindow {
|
||||||
|
|
||||||
private JFrame mframe;
|
private JFrame mframe;
|
||||||
|
private JButton btnRestart, btnSwapColors;
|
||||||
private BoardPane boardPane;
|
private BoardPane boardPane;
|
||||||
private Game game;
|
private Game game;
|
||||||
private Color activeColor;
|
private Color activeColor;
|
||||||
@ -53,7 +55,7 @@ public class MainWindow {
|
|||||||
*/
|
*/
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
mframe = new JFrame();
|
mframe = new JFrame();
|
||||||
mframe.setResizable(false);
|
mframe.setResizable(true);
|
||||||
mframe.setBounds(100, 100, 494, 565);
|
mframe.setBounds(100, 100, 494, 565);
|
||||||
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
@ -62,12 +64,10 @@ public class MainWindow {
|
|||||||
boardPane = new BoardPane();
|
boardPane = new BoardPane();
|
||||||
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);
|
||||||
|
|
||||||
JButton btnRestart = new JButton("Restart");
|
btnRestart = new JButton("Restart");
|
||||||
btnRestart.addActionListener((evt) -> {
|
btnRestart.addActionListener((evt) -> {
|
||||||
if (game != null)
|
if (game != null)
|
||||||
game.reset();
|
game.reset();
|
||||||
@ -75,7 +75,7 @@ public class MainWindow {
|
|||||||
});
|
});
|
||||||
|
|
||||||
activeColor = Color.WHITE;
|
activeColor = Color.WHITE;
|
||||||
JButton btnSwapColors = new JButton("Play as black");
|
btnSwapColors = new JButton("Play as black");
|
||||||
btnSwapColors.addActionListener((evt) -> {
|
btnSwapColors.addActionListener((evt) -> {
|
||||||
game.swapColors();
|
game.swapColors();
|
||||||
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
|
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
|
||||||
@ -85,6 +85,8 @@ public class MainWindow {
|
|||||||
toolPanel.add(btnRestart);
|
toolPanel.add(btnRestart);
|
||||||
toolPanel.add(btnSwapColors);
|
toolPanel.add(btnSwapColors);
|
||||||
|
|
||||||
|
mframe.setJMenuBar(new MenuBar(this));
|
||||||
|
|
||||||
mframe.pack();
|
mframe.pack();
|
||||||
mframe.setLocationRelativeTo(null);
|
mframe.setLocationRelativeTo(null);
|
||||||
}
|
}
|
||||||
@ -101,5 +103,7 @@ public class MainWindow {
|
|||||||
if (this.game != null)
|
if (this.game != null)
|
||||||
this.game.disconnect();
|
this.game.disconnect();
|
||||||
this.game = game;
|
this.game = game;
|
||||||
|
btnSwapColors.setEnabled(!(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer
|
||||||
|
&& game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,10 +48,10 @@ public class MenuBar extends JMenuBar {
|
|||||||
|
|
||||||
uciMenuItem.addActionListener((evt) -> {
|
uciMenuItem.addActionListener((evt) -> {
|
||||||
String enginePath = JOptionPane.showInputDialog(getParent(),
|
String enginePath = JOptionPane.showInputDialog(getParent(),
|
||||||
"Enter the path to a UCI-compatible chess engine:",
|
"Enter the path to a UCI-compatible chess engine:", "Engine selection",
|
||||||
"Engine selection",
|
|
||||||
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);
|
||||||
@ -59,7 +59,6 @@ public class MenuBar extends JMenuBar {
|
|||||||
gameMenu.add(aiVsAiMenuItem);
|
gameMenu.add(aiVsAiMenuItem);
|
||||||
gameMenu.add(uciMenuItem);
|
gameMenu.add(uciMenuItem);
|
||||||
|
|
||||||
|
|
||||||
add(gameMenu);
|
add(gameMenu);
|
||||||
|
|
||||||
// Start a game
|
// Start a game
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.kske.chess.ui;
|
package dev.kske.chess.ui;
|
||||||
|
|
||||||
|
import java.awt.BasicStroke;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
@ -47,18 +48,29 @@ public class OverlayComponent extends JComponent {
|
|||||||
g.setColor(Color.green);
|
g.setColor(Color.green);
|
||||||
int radius = tileSize / 4;
|
int radius = tileSize / 4;
|
||||||
for (Position dot : dots)
|
for (Position dot : dots)
|
||||||
g.fillOval(dot.x * tileSize + tileSize / 2 - radius / 2,
|
g.fillOval(dot.x * tileSize + tileSize / 2 - radius / 2, dot.y * tileSize + tileSize / 2 - radius / 2,
|
||||||
dot.y * tileSize + tileSize / 2 - radius / 2,
|
radius, radius);
|
||||||
radius,
|
|
||||||
radius);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw an arrow representing the last move and mark its position and destination
|
||||||
if (arrow != null) {
|
if (arrow != null) {
|
||||||
g.setColor(new Color(255, 0, 0, 127));
|
|
||||||
Point pos = new Point(arrow.pos.x * tileSize + tileSize / 2, arrow.pos.y * tileSize + tileSize / 2);
|
Point pos = new Point(arrow.pos.x * tileSize + tileSize / 2, arrow.pos.y * tileSize + tileSize / 2);
|
||||||
Point dest = new Point(arrow.dest.x * tileSize + tileSize / 2, arrow.dest.y * tileSize + tileSize / 2);
|
Point dest = new Point(arrow.dest.x * tileSize + tileSize / 2, arrow.dest.y * tileSize + tileSize / 2);
|
||||||
((Graphics2D) g).fill(createArrowShape(pos, dest));
|
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setStroke(new BasicStroke(3));
|
||||||
|
|
||||||
|
g2d.setColor(Color.yellow);
|
||||||
|
g2d.drawRect(arrow.pos.x * tileSize, arrow.pos.y * tileSize, tileSize, tileSize);
|
||||||
|
g2d.drawRect(arrow.dest.x * tileSize, arrow.dest.y * tileSize, tileSize, tileSize);
|
||||||
|
|
||||||
|
Shape arrowShape = createArrowShape(pos, dest);
|
||||||
|
g.setColor(new Color(255, 0, 0, 127));
|
||||||
|
g2d.fill(arrowShape);
|
||||||
|
g2d.setColor(Color.black);
|
||||||
|
g2d.draw(arrowShape);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Shape createArrowShape(Point pos, Point dest) {
|
private Shape createArrowShape(Point pos, Point dest) {
|
||||||
@ -112,5 +124,7 @@ public class OverlayComponent extends JComponent {
|
|||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTileSize() { return boardPane.getTileSize(); }
|
public int getTileSize() {
|
||||||
|
return boardPane.getTileSize();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user