Using reflection in FENString, fixed input field in MainWindow

This commit is contained in:
Kai S. K. Engelbart 2019-11-05 05:51:41 +01:00
parent f70cd85f2c
commit 198267c3b1
2 changed files with 10 additions and 20 deletions

View File

@ -1,5 +1,7 @@
package dev.kske.chess.board;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -80,25 +82,13 @@ public class FENString {
j += Character.getNumericValue(c);
} else {
Color color = Character.isUpperCase(c) ? Color.WHITE : Color.BLACK;
switch (Character.toUpperCase(c)) {
case 'K':
board.getBoardArr()[j][i] = new King(color, board);
break;
case 'Q':
board.getBoardArr()[j][i] = new Queen(color, board);
break;
case 'R':
board.getBoardArr()[j][i] = new Rook(color, board);
break;
case 'N':
board.getBoardArr()[j][i] = new Knight(color, board);
break;
case 'B':
board.getBoardArr()[j][i] = new Bishop(color, board);
break;
case 'P':
board.getBoardArr()[j][i] = new Pawn(color, board);
break;
try {
Constructor<? extends Piece> pieceConstructor = Piece.fromFirstChar(c).getDeclaredConstructor(Color.class, Board.class);
pieceConstructor.setAccessible(true);
board.getBoardArr()[j][i] = pieceConstructor.newInstance(color, board);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
++j;
}

View File

@ -147,7 +147,7 @@ public class MainWindow extends JFrame {
gameNames[i] = String.format("%s vs %s: %s", game.getTag("White"), game.getTag("Black"), game.getTag("Result"));
}
JComboBox<String> comboBox = new JComboBox<>(gameNames);
JOptionPane.showInputDialog(this, comboBox, "Select a game", JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(this, comboBox, "Select a game", JOptionPane.QUESTION_MESSAGE);
board = pgnDB.getGames().get(comboBox.getSelectedIndex()).getBoard();
} else throw new ChessException("The PGN database '" + name + "' is empty!");
break;