Implemented LogFrame, added menu item for opening it

This commit is contained in:
Kai S. K. Engelbart 2019-08-03 21:48:10 +02:00
parent cd01647247
commit e6d4d37848
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
4 changed files with 38 additions and 11 deletions

View File

@ -510,6 +510,11 @@ public class Board implements Cloneable {
*/ */
public Piece[][] getBoardArr() { return boardArr; } public Piece[][] getBoardArr() { return boardArr; }
/**
* @return The move log
*/
public Log getLog() { return log; }
/** /**
* @return The active color for the next move * @return The active color for the next move
*/ */

View File

@ -53,7 +53,7 @@ public class Log implements Cloneable {
moves.clear(); moves.clear();
activeColor = Color.WHITE; activeColor = Color.WHITE;
} }
@Override @Override
public Object clone() { public Object clone() {
Log log = null; Log log = null;
@ -69,12 +69,14 @@ public class Log implements Cloneable {
public Color getActiveColor() { return activeColor; } public Color getActiveColor() { return activeColor; }
public List<LoggedMove> getLoggedMoves() { return moves; }
public static class LoggedMove { public static class LoggedMove {
public final Move move; public final Move move;
public final Piece capturedPiece; public final Piece capturedPiece;
public final Position enPassant; public final Position enPassant;
public final int fullmoveCounter, halfmoveClock; public final int fullmoveCounter, halfmoveClock;
public LoggedMove(Move move, Piece capturedPiece, Position enPassant, int fullmoveCounter, int halfmoveClock) { public LoggedMove(Move move, Piece capturedPiece, Position enPassant, int fullmoveCounter, int halfmoveClock) {
this.move = move; this.move = move;

View File

@ -1,14 +1,17 @@
package dev.kske.chess.ui; package dev.kske.chess.ui;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable; import javax.swing.JTable;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import dev.kske.chess.board.Move; import dev.kske.chess.board.Log;
import dev.kske.chess.board.Log.LoggedMove;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
@ -23,12 +26,16 @@ public class LogFrame extends JFrame {
private JPanel mcontentPane; private JPanel mcontentPane;
private JTable mtable; private JTable mtable;
private Log log;
/** /**
* Create the frame. * Create the frame.
*/ */
public LogFrame() { public LogFrame(Log log) {
this.log = log;
setTitle("Move History"); setTitle("Move History");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300); setBounds(100, 100, 450, 300);
mcontentPane = new JPanel(); mcontentPane = new JPanel();
mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
@ -36,12 +43,19 @@ public class LogFrame extends JFrame {
setContentPane(mcontentPane); setContentPane(mcontentPane);
mtable = new JTable(); mtable = new JTable();
mtable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "White", "Black" }));
mtable.setEnabled(false); mtable.setEnabled(false);
mcontentPane.add(mtable, BorderLayout.CENTER); mcontentPane.add(new JScrollPane(mtable), BorderLayout.CENTER);
update();
} }
public void add(Move move) { // TODO: call this method after a move
public void update() {
final List<LoggedMove> moves = log.getLoggedMoves();
String[][] data = new String[moves.size() / 2 + moves.size() % 2][2];
for (int i = 0; i < data.length; i++) {
data[i][0] = moves.get(i * 2).move.toString();
if (i < data.length - 1) data[i][1] = moves.get(i * 2 + 1).move.toString();
}
mtable.setModel(new DefaultTableModel(data, new String[] { "White", "Black" }));
} }
} }

View File

@ -70,6 +70,12 @@ public class MenuBar extends JMenuBar {
private void initToolsMenu() { private void initToolsMenu() {
JMenu toolsMenu = new JMenu("Tools"); JMenu toolsMenu = new JMenu("Tools");
// TODO: prevent multiple initializations
JMenuItem showLogMenuItem = new JMenuItem("Show move log");
showLogMenuItem
.addActionListener((evt) -> new LogFrame(mainWindow.getGame().getBoard().getLog()).setVisible(true));
toolsMenu.add(showLogMenuItem);
JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN"); JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN");
exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit() exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit()
.getSystemClipboard() .getSystemClipboard()