Added pawn promotion support to LAN

This commit is contained in:
Kai S. K. Engelbart 2019-11-04 18:11:23 +01:00
parent 17677e6858
commit e92664dd07
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
4 changed files with 55 additions and 15 deletions

View File

@ -1,5 +1,6 @@
package dev.kske.chess.board;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -133,25 +134,39 @@ public class Board {
move = new Move(pos, dest);
break;
case "pawnCapture":
dest = Position.fromLAN(m.group("toSquare"));
char file = m.group("fromFile").charAt(0);
int rank = m.group("fromRank") == null ? get(Pawn.class, file) : Integer.parseInt(m.group("fromRank"));
pos = Position.fromLAN(String.format("%c%d", file, rank));
if (m.group("promotedTo") != null) {
}
move = new Move(pos, dest);
dest = Position.fromLAN(m.group("toSquare"));
pos = Position.fromLAN(String.format("%c%d", file, rank));
if (m.group("promotedTo") != null) {
try {
move = new PawnPromotion(pos, dest, Piece.fromFirstChar(m.group("promotedTo").charAt(0)));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | InstantiationException e) {
e.printStackTrace();
}
} else move = new Move(pos, dest);
break;
case "pawnPush":
dest = Position.fromLAN(m.group("toSquare"));
// TODO: Pawn promotion
int step = log.getActiveColor() == Color.WHITE ? 1 : -1;
// One step forward
if (boardArr[dest.x][dest.y + step] != null) pos = new Position(dest.x, dest.y + step);
// Double step forward
else pos = new Position(dest.x, dest.y + 2 * step);
move = new Move(pos, dest);
if (m.group("promotedTo") != null) {
try {
move = new PawnPromotion(pos, dest, Piece.fromFirstChar(m.group("promotedTo").charAt(0)));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | InstantiationException e) {
e.printStackTrace();
}
} else move = new Move(pos, dest);
break;
case "castling":
pos = new Position(4, log.getActiveColor() == Color.WHITE ? 7 : 0);

View File

@ -1,5 +1,6 @@
package dev.kske.chess.board;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
/**
@ -26,9 +27,6 @@ public class Move {
public Move(int xPos, int yPos, int xDest, int yDest) { this(new Position(xPos, yPos), new Position(xDest, yDest)); }
// TODO: Pawn promotion
public static Move fromLAN(String move) { return new Move(Position.fromLAN(move.substring(0, 2)), Position.fromLAN(move.substring(2))); }
public void execute(Board board) {
// Move the piece to the move's destination square and clean the old position
board.set(dest, board.get(pos));
@ -41,6 +39,20 @@ public class Move {
board.set(dest, capturedPiece);
}
public static Move fromLAN(String move) {
Position pos = Position.fromLAN(move.substring(0, 2));
Position dest = Position.fromLAN(move.substring(2));
if (move.length() == 5) {
try {
return new PawnPromotion(pos, dest, Piece.fromFirstChar(move.charAt(4)));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| InstantiationException e) {
e.printStackTrace();
return null;
}
} else return new Move(pos, dest);
}
public String toLAN() { return getPos().toLAN() + getDest().toLAN(); }
public boolean isHorizontal() { return getyDist() == 0; }

View File

@ -1,5 +1,6 @@
package dev.kske.chess.board;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
@ -74,7 +75,8 @@ public class Pawn extends Piece {
moves.add(new PawnPromotion(pos, dest, Rook.class));
moves.add(new PawnPromotion(pos, dest, Knight.class));
moves.add(new PawnPromotion(pos, dest, Bishop.class));
} catch (NoSuchMethodException | SecurityException e) {
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| InstantiationException e) {
e.printStackTrace();
}
} else moves.add(move);

View File

@ -15,16 +15,24 @@ import dev.kske.chess.board.Piece.Color;
*/
public class PawnPromotion extends Move {
private Constructor<? extends Piece> promotionPieceConstructor;
private final Constructor<? extends Piece> promotionPieceConstructor;
private final char promotionPieceChar;
public PawnPromotion(Position pos, Position dest, Class<? extends Piece> promotionPiece) throws NoSuchMethodException, SecurityException {
public PawnPromotion(Position pos, Position dest, Class<? extends Piece> promotionPiece) throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
super(pos, dest);
// Cache piece constructor
promotionPieceConstructor = promotionPiece.getDeclaredConstructor(Color.class, Board.class);
promotionPieceConstructor.setAccessible(true);
// Cache piece first char
promotionPieceChar = (char) promotionPiece.getMethod("firstChar").invoke(promotionPieceConstructor.newInstance(null, null));
}
public PawnPromotion(int xPos, int yPos, int xDest, int yDest, Class<? extends Piece> promotionPiece)
throws NoSuchMethodException, SecurityException {
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
InstantiationException {
this(new Position(xPos, yPos), new Position(xDest, yDest), promotionPiece);
}
@ -40,7 +48,10 @@ public class PawnPromotion extends Move {
@Override
public void revert(Board board, Piece capturedPiece) {
board.set(dest, new Pawn(board.get(dest).getColor(), board));
super.revert(board, capturedPiece);
board.set(pos, new Pawn(board.get(dest).getColor(), board));
}
@Override
public String toLAN() { return pos.toLAN() + dest.toLAN() + promotionPieceChar; }
}