Replace anonymous classes with lambda in CuckooChess

This commit is contained in:
Hamidreza Bayat 2019-04-28 19:49:52 +04:30
parent 7b6841b9fb
commit 6a90c4e650
2 changed files with 42 additions and 72 deletions

View File

@ -43,13 +43,11 @@ public class AppletGUI extends javax.swing.JApplet implements GUIInterface {
public void init() {
ctrl = new ChessController(this);
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
cbp = (ChessBoardPainter)ChessBoard;
ctrl.newGame(PlayerWhite.isSelected(), ttLogSize, true);
ctrl.startGame();
}
java.awt.EventQueue.invokeAndWait(() -> {
initComponents();
cbp = (ChessBoardPainter)ChessBoard;
ctrl.newGame(PlayerWhite.isSelected(), ttLogSize, true);
ctrl.startGame();
});
} catch (Exception ex) {
ex.printStackTrace();
@ -140,11 +138,7 @@ public class AppletGUI extends javax.swing.JApplet implements GUIInterface {
NewGame.setText("New Game");
NewGame.setFocusable(false);
NewGame.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewGameActionPerformed(evt);
}
});
NewGame.addActionListener(evt -> NewGameActionPerformed(evt));
SettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Settings"));
SettingsPanel.setFocusable(false);
@ -167,27 +161,15 @@ public class AppletGUI extends javax.swing.JApplet implements GUIInterface {
TimeSlider.setPaintTicks(true);
TimeSlider.setValue(5);
TimeSlider.setFocusable(false);
TimeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
TimeSliderStateChanged(evt);
}
});
TimeSlider.addChangeListener(evt -> TimeSliderStateChanged(evt));
ShowThinking.setText("Show Thinking");
ShowThinking.setFocusable(false);
ShowThinking.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
ShowThinkingStateChanged(evt);
}
});
ShowThinking.addChangeListener(evt -> ShowThinkingStateChanged(evt));
FlipBoard.setText("Flip Board");
FlipBoard.setFocusable(false);
FlipBoard.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
FlipBoardStateChanged(evt);
}
});
FlipBoard.addChangeListener(evt -> FlipBoardStateChanged(evt));
javax.swing.GroupLayout SettingsPanelLayout = new javax.swing.GroupLayout(SettingsPanel);
SettingsPanel.setLayout(SettingsPanelLayout);
@ -233,21 +215,13 @@ public class AppletGUI extends javax.swing.JApplet implements GUIInterface {
Forward.setDefaultCapable(false);
Forward.setFocusPainted(false);
Forward.setFocusable(false);
Forward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ForwardActionPerformed(evt);
}
});
Forward.addActionListener(evt -> ForwardActionPerformed(evt));
Backward.setText("<-");
Backward.setDefaultCapable(false);
Backward.setFocusPainted(false);
Backward.setFocusable(false);
Backward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BackwardActionPerformed(evt);
}
});
Backward.addActionListener(evt -> BackwardActionPerformed(evt));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
@ -425,14 +399,12 @@ public class AppletGUI extends javax.swing.JApplet implements GUIInterface {
}
public void requestPromotePiece() {
runOnUIThread(new Runnable() {
public void run() {
Object[] options = { "Queen", "Rook", "Bishop", "Knight" };
int choice = JOptionPane.showOptionDialog(
cbp, "Promote pawn to?", "Pawn Promotion",
0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
ctrl.reportPromotePiece(choice);
}
runOnUIThread(() -> {
Object[] options = { "Queen", "Rook", "Bishop", "Knight" };
int choice = JOptionPane.showOptionDialog(
cbp, "Promote pawn to?", "Pawn Promotion",
0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
ctrl.reportPromotePiece(choice);
});
}

View File

@ -241,35 +241,33 @@ public class EngineControl {
}
tt.nextGeneration();
final int srchmaxDepth = maxDepth;
engineThread = new Thread(new Runnable() {
public void run() {
Move m = null;
if (ownBook && !analyseMode) {
Book book = new Book(false);
m = book.getBookMove(pos);
engineThread = new Thread(() -> {
Move m = null;
if (ownBook && !analyseMode) {
Book book = new Book(false);
m = book.getBookMove(pos);
}
if (m == null) {
m = sc.iterativeDeepening(srchMoves, srchmaxDepth, maxNodes, false);
}
while (ponder || infinite) {
// We should not respond until told to do so. Just wait until
// we are allowed to respond.
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
break;
}
if (m == null) {
m = sc.iterativeDeepening(srchMoves, srchmaxDepth, maxNodes, false);
}
while (ponder || infinite) {
// We should not respond until told to do so. Just wait until
// we are allowed to respond.
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
break;
}
}
Move ponderMove = getPonderMove(pos, m);
synchronized (threadMutex) {
if (ponderMove != null) {
os.printf("bestmove %s ponder %s%n", moveToString(m), moveToString(ponderMove));
} else {
os.printf("bestmove %s%n", moveToString(m));
}
engineThread = null;
sc = null;
}
Move ponderMove = getPonderMove(pos, m);
synchronized (threadMutex) {
if (ponderMove != null) {
os.printf("bestmove %s ponder %s%n", moveToString(m), moveToString(ponderMove));
} else {
os.printf("bestmove %s%n", moveToString(m));
}
engineThread = null;
sc = null;
}
});
engineThread.start();