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 1dc97ba3de
commit 6824b46539
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 10 additions and 20 deletions

View File

@ -1,5 +1,7 @@
package dev.kske.chess.board; package dev.kske.chess.board;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -80,25 +82,13 @@ public class FENString {
j += Character.getNumericValue(c); j += Character.getNumericValue(c);
} else { } else {
Color color = Character.isUpperCase(c) ? Color.WHITE : Color.BLACK; Color color = Character.isUpperCase(c) ? Color.WHITE : Color.BLACK;
switch (Character.toUpperCase(c)) { try {
case 'K': Constructor<? extends Piece> pieceConstructor = Piece.fromFirstChar(c).getDeclaredConstructor(Color.class, Board.class);
board.getBoardArr()[j][i] = new King(color, board); pieceConstructor.setAccessible(true);
break; board.getBoardArr()[j][i] = pieceConstructor.newInstance(color, board);
case 'Q': } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
board.getBoardArr()[j][i] = new Queen(color, board); | NoSuchMethodException | SecurityException e) {
break; e.printStackTrace();
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;
} }
++j; ++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")); gameNames[i] = String.format("%s vs %s: %s", game.getTag("White"), game.getTag("Black"), game.getTag("Result"));
} }
JComboBox<String> comboBox = new JComboBox<>(gameNames); 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(); board = pgnDB.getGames().get(comboBox.getSelectedIndex()).getBoard();
} else throw new ChessException("The PGN database '" + name + "' is empty!"); } else throw new ChessException("The PGN database '" + name + "' is empty!");
break; break;