Improved BoardOverlay, disabled color swap in natural-vs-natural game

This commit is contained in:
delvh 2019-07-23 10:38:19 +02:00
parent 91962c01e0
commit 309495cfae
4 changed files with 76 additions and 56 deletions

View File

@ -87,7 +87,8 @@ public class Game {
players.get(board.getActiveColor()).requestMove();
}
overlayComponent.displayArrow(move);
} else player.requestMove();
} else
player.requestMove();
}
public void start() {
@ -121,4 +122,6 @@ public class Game {
}
public Board getBoard() { return board; }
public Map<Color, Player> getPlayers() { return players; }
}

View File

@ -10,6 +10,7 @@ import javax.swing.JPanel;
import dev.kske.chess.board.Piece.Color;
import dev.kske.chess.game.Game;
import dev.kske.chess.game.NaturalPlayer;
/**
* Project: <strong>Chess</strong><br>
@ -20,6 +21,7 @@ import dev.kske.chess.game.Game;
public class MainWindow {
private JFrame mframe;
private JButton btnRestart, btnSwapColors;
private BoardPane boardPane;
private Game game;
private Color activeColor;
@ -53,7 +55,7 @@ public class MainWindow {
*/
private void initialize() {
mframe = new JFrame();
mframe.setResizable(false);
mframe.setResizable(true);
mframe.setBounds(100, 100, 494, 565);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -62,12 +64,10 @@ public class MainWindow {
boardPane = new BoardPane();
mframe.getContentPane().add(boardPane, BorderLayout.CENTER);
mframe.setJMenuBar(new MenuBar(this));
JPanel toolPanel = new JPanel();
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
JButton btnRestart = new JButton("Restart");
btnRestart = new JButton("Restart");
btnRestart.addActionListener((evt) -> {
if (game != null)
game.reset();
@ -75,7 +75,7 @@ public class MainWindow {
});
activeColor = Color.WHITE;
JButton btnSwapColors = new JButton("Play as black");
btnSwapColors = new JButton("Play as black");
btnSwapColors.addActionListener((evt) -> {
game.swapColors();
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
@ -85,6 +85,8 @@ public class MainWindow {
toolPanel.add(btnRestart);
toolPanel.add(btnSwapColors);
mframe.setJMenuBar(new MenuBar(this));
mframe.pack();
mframe.setLocationRelativeTo(null);
}
@ -101,5 +103,7 @@ public class MainWindow {
if (this.game != null)
this.game.disconnect();
this.game = game;
btnSwapColors.setEnabled(!(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer
&& game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer));
}
}

View File

@ -48,10 +48,10 @@ public class MenuBar extends JMenuBar {
uciMenuItem.addActionListener((evt) -> {
String enginePath = JOptionPane.showInputDialog(getParent(),
"Enter the path to a UCI-compatible chess engine:",
"Engine selection",
"Enter the path to a UCI-compatible chess engine:", "Engine selection",
JOptionPane.QUESTION_MESSAGE);
if (enginePath != null) startGame(Game.createUCI(boardPane, enginePath));
if (enginePath != null)
startGame(Game.createUCI(boardPane, enginePath));
});
gameMenu.add(naturalMenuItem);
@ -59,7 +59,6 @@ public class MenuBar extends JMenuBar {
gameMenu.add(aiVsAiMenuItem);
gameMenu.add(uciMenuItem);
add(gameMenu);
// Start a game

View File

@ -1,5 +1,6 @@
package dev.kske.chess.ui;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
@ -47,18 +48,29 @@ public class OverlayComponent extends JComponent {
g.setColor(Color.green);
int radius = tileSize / 4;
for (Position dot : dots)
g.fillOval(dot.x * tileSize + tileSize / 2 - radius / 2,
dot.y * tileSize + tileSize / 2 - radius / 2,
radius,
radius);
g.fillOval(dot.x * tileSize + tileSize / 2 - radius / 2, dot.y * tileSize + tileSize / 2 - radius / 2,
radius, radius);
}
// Draw an arrow representing the last move and mark its position and destination
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 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) {
@ -112,5 +124,7 @@ public class OverlayComponent extends JComponent {
repaint();
}
public int getTileSize() { return boardPane.getTileSize(); }
public int getTileSize() {
return boardPane.getTileSize();
}
}