Improved Menu & CPU modes

This commit is contained in:
Filip Znachor 2023-05-02 23:54:43 +02:00
parent be4d94a5bb
commit b9fe92d7ff
5 changed files with 113 additions and 55 deletions

View file

@ -275,7 +275,7 @@ public abstract class APiece {
}
public Rectangle getRepaintRectangle() {
return getRepaintRectangle(0);
return getRepaintRectangle(Chess.menuBar.getHeight());
}
public Rectangle getRepaintRectangle(int offsetY) {
@ -333,7 +333,7 @@ public abstract class APiece {
double currentX = 0;
double currentY = 0;
public void run() {
chessboard.repaint(getRepaintRectangle());
chessboard.getRootPane().repaint(getRepaintRectangle());
if(i > 100) {
timer.cancel();
setOverride(0, 0);
@ -342,7 +342,7 @@ public abstract class APiece {
currentY = i * stepY + startY;
setOverride(currentX, currentY);
}
chessboard.repaint(getRepaintRectangle());
chessboard.getRootPane().repaint(getRepaintRectangle());
i++;
}
}, 5, 5);
@ -362,7 +362,7 @@ public abstract class APiece {
timer.cancel();
scale = 0;
}
chessboard.repaint(getRepaintRectangle());
chessboard.getRootPane().repaint(getRepaintRectangle());
}
}, 10, 10);
}

View file

@ -34,10 +34,10 @@ public class Chess {
window.setSize(800, 600);
window.setMinimumSize(new Dimension(800, 600));
window.setJMenuBar(createMenuBar());
chessboard = Chessboard.fromFEN(DEFAULT_FEN);
window.setJMenuBar(createMenuBar());
window.add(chessboard, BorderLayout.CENTER);
window.pack();
@ -59,7 +59,6 @@ public class Chess {
menuBar = new JMenuBar();
JMenu menuGame = new JMenu("Game");
JMenu menuCPU = new JMenu("CPU");
JMenu menuExport = new JMenu("Export");
JMenuItem startNewGame = new JMenuItem("Start new game");
@ -76,32 +75,16 @@ public class Chess {
});
menuGame.add(newGameFromFEN);
JMenuItem toggleBlindMode = new JMenuItem("Toggle blind mode");
menuGame.add(new JPopupMenu.Separator());
JCheckBoxMenuItem toggleBlindMode = new JCheckBoxMenuItem("Blind mode");
toggleBlindMode.addActionListener(l -> {
chessboard.blindMode = !chessboard.blindMode;
toggleBlindMode.setState(chessboard.blindMode);
chessboard.repaint();
});
menuGame.add(toggleBlindMode);
JMenuItem toggleAutomaticRandomOpponent = new JMenuItem("Toggle automatic random opponent");
toggleAutomaticRandomOpponent.addActionListener(l -> {
Player opponent = chessboard.getOtherPlayer();
opponent.toggleAutomaticRandom(!opponent.isAutomaticRandom());
});
menuCPU.add(toggleAutomaticRandomOpponent);
JMenuItem toggleAutomaticSmartOpponent = new JMenuItem("Toggle automatic smart opponent");
toggleAutomaticSmartOpponent.addActionListener(l -> {
Player opponent = chessboard.getOtherPlayer();
if(!opponent.isAutomaticSmart()) {
Stockfish stockfish = opponent.getStockfish();
opponent.toggleAutomaticSmart(stockfish);
} else {
opponent.toggleAutomaticSmart(null);
}
});
menuCPU.add(toggleAutomaticSmartOpponent);
JMenuItem exportAsSvg = new JMenuItem("Export as SVG");
exportAsSvg.addActionListener(l -> exportSVG());
menuExport.add(exportAsSvg);
@ -111,13 +94,90 @@ public class Chess {
menuExport.add(exportAsPng);
menuBar.add(menuGame);
menuBar.add(menuCPU);
menuBar.add(createMenuCPU());
menuBar.add(menuExport);
return menuBar;
}
static void repaintWindow() {
public static JMenu createMenuCPU() {
JMenu menuCPU = new JMenu("CPU");
JCheckBoxMenuItem whiteDisabled = new JCheckBoxMenuItem("White / Disabled");
JCheckBoxMenuItem whiteAutomaticRandom = new JCheckBoxMenuItem("White / Automatic random");
JCheckBoxMenuItem whiteAutomaticSmart = new JCheckBoxMenuItem("White / Automatic smart");
JCheckBoxMenuItem blackDisabled = new JCheckBoxMenuItem("Black / Disabled");
JCheckBoxMenuItem blackAutomaticRandom = new JCheckBoxMenuItem("Black / Automatic random");
JCheckBoxMenuItem blackAutomaticSmart = new JCheckBoxMenuItem("Black / Automatic smart");
Runnable updateWhite = () -> {
String cpu = chessboard.getPlayer1().getCPU();
whiteDisabled.setState(cpu == null);
whiteAutomaticSmart.setState(cpu != null && cpu.equals("smart"));
whiteAutomaticRandom.setState(cpu != null && cpu.equals("random"));
};
Runnable updateBlack = () -> {
String cpu = chessboard.getPlayer2().getCPU();
blackDisabled.setState(cpu == null);
blackAutomaticSmart.setState(cpu != null && cpu.equals("smart"));
blackAutomaticRandom.setState(cpu != null && cpu.equals("random"));
};
whiteDisabled.addActionListener(l -> {
setCPU(chessboard.getPlayer1(), null, updateWhite);
});
menuCPU.add(whiteDisabled);
whiteAutomaticRandom.addActionListener(l -> {
setCPU(chessboard.getPlayer1(), "random", updateWhite);
});
menuCPU.add(whiteAutomaticRandom);
whiteAutomaticSmart.addActionListener(l -> {
setCPU(chessboard.getPlayer1(), "smart", updateWhite);
});
menuCPU.add(whiteAutomaticSmart);
menuCPU.add(new JPopupMenu.Separator());
blackDisabled.addActionListener(l -> {
setCPU(chessboard.getPlayer2(), null, updateBlack);
});
menuCPU.add(blackDisabled);
blackAutomaticRandom.addActionListener(l -> {
setCPU(chessboard.getPlayer2(), "random", updateBlack);
});
menuCPU.add(blackAutomaticRandom);
blackAutomaticSmart.addActionListener(l -> {
setCPU(chessboard.getPlayer2(), "smart", updateBlack);
});
menuCPU.add(blackAutomaticSmart);
updateWhite.run();
updateBlack.run();
return menuCPU;
}
public static void setCPU(Player player, String mode, Runnable update) {
if(mode != null && mode.equals("smart")) {
Stockfish stockfish = player.getStockfish();
if(stockfish != null) {
player.setStockfish(stockfish);
player.setCPU(mode);
}
} else {
player.setCPU(mode);
}
update.run();
}
public static void repaintWindow() {
// Hack to force repaint of the chessboard (repaint is not working)
int winWidth = window.getWidth();
int winHeight = window.getHeight();
@ -125,14 +185,14 @@ public class Chess {
window.setSize(winWidth, winHeight);
}
static void newGame(Chessboard newChessboard) {
public static void newGame(Chessboard newChessboard) {
window.remove(chessboard);
chessboard = newChessboard;
window.add(chessboard, BorderLayout.CENTER);
repaintWindow();
}
static void exportSVG() {
public static void exportSVG() {
SVGGraphics2D g2 = new SVGGraphics2D(1000, 1000);
chessboard.paint(g2, 1000, 1000);
String svg = g2.getSVGElement();
@ -146,7 +206,7 @@ public class Chess {
}
}
static void exportPNG() {
public static void exportPNG() {
BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
chessboard.paint(g, 1000, 1000);

View file

@ -483,6 +483,14 @@ public class Chessboard extends JPanel {
return activePlayer == player1 ? player2 : player1;
}
public Player getPlayer1() {
return player1;
}
public Player getPlayer2() {
return player2;
}
public void changeActivePlayer() {
activePlayer = activePlayer == player1 ? player2 : player1;
getRootPane().repaint();

View file

@ -82,11 +82,11 @@ public class ChessboardMouseAdapter extends MouseAdapter {
Chessboard c = (Chessboard) me.getSource();
APiece piece = c.getSelectedPiece();
if (piece != null) {
c.getRootPane().repaint(piece.getRepaintRectangle(Chess.menuBar.getHeight()));
c.getRootPane().repaint(piece.getRepaintRectangle());
double pieceX = me.getX() - c.SQUARE_SIZE/2 * c.boardScale;
double pieceY = me.getY() - c.SQUARE_SIZE/2 * c.boardScale;
piece.setOverride(pieceX, pieceY);
c.getRootPane().repaint(piece.getRepaintRectangle(Chess.menuBar.getHeight()));
c.getRootPane().repaint(piece.getRepaintRectangle());
}
}

View file

@ -17,9 +17,7 @@ public class Player {
private Stockfish stockfish;
private boolean automaticRandom = false;
private boolean automaticSmart = false;
private String cpu;
public Player(Chessboard chessboard, StartPosition startPosition, PieceColor color) {
this.chessboard = chessboard;
@ -36,8 +34,10 @@ public class Player {
t.schedule(new TimerTask() {
@Override
public void run() {
if(automaticRandom) randomMove();
if(automaticSmart) smartMove();
if(cpu != null) {
if (cpu.equals("random")) randomMove();
if (cpu.equals("smart")) smartMove();
}
t.cancel();
}
}, 1000, 1000);
@ -138,26 +138,16 @@ public class Player {
return myPieces;
}
public void toggleAutomaticRandom(boolean state) {
if(state) automaticSmart = false;
automaticRandom = state;
public void setCPU(String cpu) {
this.cpu = cpu;
}
public boolean isAutomaticRandom() {
return automaticRandom;
public String getCPU() {
return cpu;
}
public void toggleAutomaticSmart(Stockfish stockfish) {
boolean state = stockfish != null;
if(state) {
this.stockfish = stockfish;
automaticRandom = false;
}
automaticSmart = state;
}
public boolean isAutomaticSmart() {
return automaticSmart;
public void setStockfish(Stockfish stockfish) {
this.stockfish = stockfish;
}
}