Added FEN import
This commit is contained in:
parent
c27f764bc0
commit
a0c6c7605e
|
@ -273,31 +273,37 @@ public abstract class AbstractPiece implements IPiece {
|
|||
return moveCount;
|
||||
}
|
||||
|
||||
public void setMoveCount(int moveCount) {
|
||||
this.moveCount = moveCount;
|
||||
}
|
||||
|
||||
public void animateMove(int fromX, int fromY, int toX, int toY) {
|
||||
double startX = chessboard.startX + (fromX*chessboard.SQUARE_SIZE+20*chessboard.pieceScale)*chessboard.boardScale;
|
||||
double startY = chessboard.startY + (fromY*chessboard.SQUARE_SIZE+20*chessboard.pieceScale)*chessboard.boardScale;
|
||||
double endX = chessboard.startX + (toX*chessboard.SQUARE_SIZE+20*chessboard.pieceScale)*chessboard.boardScale;
|
||||
double endY = chessboard.startY + (toY*chessboard.SQUARE_SIZE+20*chessboard.pieceScale)*chessboard.boardScale;
|
||||
double stepX = (endX-startX)/50;
|
||||
double stepY = (endY-startY)/50;
|
||||
double stepX = (endX-startX)/100;
|
||||
double stepY = (endY-startY)/100;
|
||||
Timer timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
int i = 1;
|
||||
double currentX = 0;
|
||||
double currentY = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
double currentX = i * stepX + startX;
|
||||
double currentY = i * stepY + startY;
|
||||
if(i > 50) {
|
||||
chessboard.repaint(getRepaintRectangle(currentX, currentY, chessboard.boardScale*chessboard.pieceScale));
|
||||
if(i > 100) {
|
||||
timer.cancel();
|
||||
setOverride(0, 0);
|
||||
} else {
|
||||
|
||||
currentX = i * stepX + startX;
|
||||
currentY = i * stepY + startY;
|
||||
setOverride(currentX, currentY);
|
||||
}
|
||||
chessboard.repaint(getRepaintRectangle(currentX, currentY, chessboard.boardScale*chessboard.pieceScale));
|
||||
i++;
|
||||
}
|
||||
}, 10, 10);
|
||||
}, 5, 5);
|
||||
}
|
||||
|
||||
}
|
|
@ -18,12 +18,7 @@ public class Chess {
|
|||
window.setMinimumSize(new Dimension(800, 600));
|
||||
window.setBackground(Color.GRAY);
|
||||
|
||||
Chessboard chessboard = new Chessboard();
|
||||
|
||||
PieceColor[] colors = new PieceColor[]{PieceColor.WHITE, PieceColor.BLACK};
|
||||
StartPosition[] startPositions = new StartPosition[]{StartPosition.BOTTOM, StartPosition.TOP};
|
||||
chessboard.setPlayer1(new Player(chessboard, startPositions[0], colors[0]));
|
||||
chessboard.setPlayer2(new Player(chessboard, startPositions[1], colors[1]));
|
||||
Chessboard chessboard = Chessboard.fromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
||||
|
||||
window.add(chessboard);
|
||||
window.pack();
|
||||
|
|
|
@ -75,6 +75,65 @@ public class Chessboard extends JPanel {
|
|||
|
||||
}
|
||||
|
||||
public static Chessboard fromFEN(String input) {
|
||||
|
||||
String[] parts = input.split(" ");
|
||||
if(parts.length != 6) return null;
|
||||
String[] pieces = parts[0].split("/");
|
||||
if(pieces.length != 8) return null;
|
||||
|
||||
Chessboard c = new Chessboard();
|
||||
Player white = new Player(c, StartPosition.BOTTOM, PieceColor.WHITE);
|
||||
Player black = new Player(c, StartPosition.TOP, PieceColor.BLACK);
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
for (String pieceRow : pieces) {
|
||||
for (char item : pieceRow.toCharArray()) {
|
||||
if (Character.isDigit(item)) {
|
||||
x += Integer.parseInt(String.valueOf(item));
|
||||
} else {
|
||||
char pieceType = Character.toLowerCase(item);
|
||||
Player pieceOwner = pieceType != item ? white : black;
|
||||
if(pieceType == 'p') new Pawn(pieceOwner, x, y);
|
||||
if(pieceType == 'r') {
|
||||
IPiece rook = new Rook(pieceOwner, x, y);
|
||||
rook.setMoveCount(1);
|
||||
}
|
||||
if(pieceType == 'n') new Knight(pieceOwner, x, y);
|
||||
if(pieceType == 'b') new Bishop(pieceOwner, x, y);
|
||||
if(pieceType == 'q') new Queen(pieceOwner, x, y);
|
||||
if(pieceType == 'k') new King(pieceOwner, x, y);
|
||||
x++;
|
||||
}
|
||||
}
|
||||
x = 0;
|
||||
y++;
|
||||
}
|
||||
|
||||
if(parts[1].equals("w")) {
|
||||
c.setPlayer1(white);
|
||||
c.setPlayer2(black);
|
||||
} else {
|
||||
c.setPlayer1(black);
|
||||
c.setPlayer2(white);
|
||||
}
|
||||
|
||||
for (char item : parts[2].toCharArray()) {
|
||||
char pieceType = Character.toLowerCase(item);
|
||||
if(pieceType == 'q' || pieceType == 'k') {
|
||||
Player pieceOwner = pieceType != item ? white : black;
|
||||
int rookX = pieceType == 'q' ? 0 : 7;
|
||||
int rookY = pieceType != item ? 7 : 0;
|
||||
IPiece rook = c.getPiece(new PiecePosition(rookX, rookY));
|
||||
if(rook != null && rook.getPlayer() == pieceOwner) rook.setMoveCount(0);
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add passed piece to the specified coordinates
|
||||
* @param piece piece
|
||||
|
@ -309,4 +368,5 @@ public class Chessboard extends JPanel {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -72,6 +72,8 @@ public interface IPiece {
|
|||
|
||||
int getMoveCount();
|
||||
|
||||
void setMoveCount(int moveCount);
|
||||
|
||||
Rectangle getRepaintRectangle(double pieceX, double pieceY, double totalScale);
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ public class King extends AbstractPiece {
|
|||
*/
|
||||
public King(Player player, int x, int y) {
|
||||
super(player, x, y);
|
||||
player.setKing(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,8 +52,7 @@ public class King extends AbstractPiece {
|
|||
}
|
||||
|
||||
private boolean checkCastling(int direction) {
|
||||
boolean rookNoMove = (player.getLeftRook().moveCount == 0 && direction == -1) || (player.getRightRook().moveCount == 0 && direction == 1);
|
||||
if(moveCount == 0 && !player.inCheck() && rookNoMove) {
|
||||
if(moveCount == 0 && !player.inCheck() && checkRook(direction)) {
|
||||
for (int pX = x + direction; (pX > 0 && pX < 7); pX += direction) {
|
||||
if(chessboard.isEndangered(player, pX, y) || chessboard.getPiece(new PiecePosition(pX, y)) != null) return false;
|
||||
}
|
||||
|
@ -61,14 +61,22 @@ public class King extends AbstractPiece {
|
|||
return false;
|
||||
}
|
||||
|
||||
private boolean checkRook(int direction) {
|
||||
int rookX = direction == -1 ? 0 : 7;
|
||||
IPiece rook = chessboard.getPiece(new PiecePosition(rookX, y));
|
||||
return rook != null && rook instanceof Rook && rook.getMoveCount() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(PiecePosition pos, boolean animate) {
|
||||
super.move(pos, animate);
|
||||
if(moveCount == 1 && x == 6) {
|
||||
player.getRightRook().setPosition(new PiecePosition(5, y));
|
||||
IPiece rook = chessboard.getPiece(new PiecePosition(7, y));
|
||||
rook.setPosition(new PiecePosition(5, y));
|
||||
}
|
||||
if(moveCount == 1 && x == 1) {
|
||||
player.getLeftRook().setPosition(new PiecePosition(3, y));
|
||||
IPiece rook = chessboard.getPiece(new PiecePosition(0, y));
|
||||
rook.setPosition(new PiecePosition(3, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,10 +8,6 @@ public class Player {
|
|||
|
||||
private King king;
|
||||
|
||||
private Rook leftRook;
|
||||
|
||||
private Rook rightRook;
|
||||
|
||||
private boolean[][] coverageArea;
|
||||
|
||||
public Player(Chessboard chessboard, StartPosition startPosition, PieceColor color) {
|
||||
|
@ -19,29 +15,12 @@ public class Player {
|
|||
this.color = color;
|
||||
this.startPosition = startPosition;
|
||||
this.coverageArea = new boolean[chessboard.SQUARE_COUNT][chessboard.SQUARE_COUNT];
|
||||
createPieces();
|
||||
}
|
||||
|
||||
public boolean inCheck() {
|
||||
return king.isEndangered();
|
||||
}
|
||||
|
||||
public void createPieces() {
|
||||
int first = startPosition == StartPosition.BOTTOM ? 7 : 0;
|
||||
int second = startPosition == StartPosition.BOTTOM ? 6 : 1;
|
||||
leftRook = new Rook(this, 0, first);
|
||||
rightRook = new Rook(this, 7, first);
|
||||
/*new Knight(this, 1, first);
|
||||
new Knight(this, 6, first);
|
||||
new Queen(this, 3, first);
|
||||
new Bishop(this, 5, first);
|
||||
new Bishop(this, 2, first);*/
|
||||
king = new King(this, 4, first);
|
||||
for (int j = 0; j < 8; j++) {
|
||||
new Pawn(this, j, second);
|
||||
}
|
||||
}
|
||||
|
||||
public Chessboard getChessboard() {
|
||||
return chessboard;
|
||||
}
|
||||
|
@ -66,12 +45,8 @@ public class Player {
|
|||
return king;
|
||||
}
|
||||
|
||||
public Rook getLeftRook() {
|
||||
return leftRook;
|
||||
}
|
||||
|
||||
public Rook getRightRook() {
|
||||
return rightRook;
|
||||
public void setKing(King king) {
|
||||
this.king = king;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue