Using reflection in FENString, fixed input field in MainWindow
This commit is contained in:
parent
f70cd85f2c
commit
198267c3b1
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
Reference in New Issue
Block a user