99 lines
3.1 KiB
Java
99 lines
3.1 KiB
Java
import java.awt.*;
|
|
|
|
/**
|
|
* King piece class
|
|
*/
|
|
public class King extends APiece {
|
|
|
|
/**
|
|
* Create new King piece
|
|
* @param player player
|
|
* @param x piece's X location
|
|
* @param y piece's Y location
|
|
*/
|
|
public King(Player player, int x, int y) {
|
|
super(player, x, y);
|
|
player.setKing(this);
|
|
}
|
|
|
|
/**
|
|
* Paint the King piece
|
|
* @param g2 Graphics2D
|
|
*/
|
|
@Override
|
|
public void paint(Graphics2D g2) {
|
|
|
|
double[] xObject1 = new double[]{46, 54, 54, 63, 63, 54, 54, 66, 77, 85, 15, 23, 34, 46, 46, 37, 37, 46};
|
|
double[] yObject1 = new double[]{0, 0, 8, 8, 16, 16, 19, 24, 24, 31, 31, 24, 24, 19, 16, 16, 8, 8};
|
|
|
|
double[] xObject2 = new double[]{15, 85, 70, 70, 30, 30};
|
|
double[] yObject2 = new double[]{35, 35, 51, 58, 58, 51};
|
|
|
|
paintObjects(g2, new Shape[]{getStand(), getObject(xObject1, yObject1), getObject(xObject2, yObject2)});
|
|
|
|
}
|
|
|
|
/**
|
|
* Get King's possible moves
|
|
* @param attack force the attack mode
|
|
* @return two-dimensional array of moves
|
|
*/
|
|
@Override
|
|
public boolean[][] getPossibleMoves(boolean attack) {
|
|
boolean[][] moves = new boolean[chessboard.SQUARE_COUNT][chessboard.SQUARE_COUNT];
|
|
for(int i = x-1; i <= x+1; i++) {
|
|
for(int j = y-1; j <= y+1; j++) {
|
|
setPossibleMove(moves, i, j);
|
|
}
|
|
}
|
|
if(!attack && checkCastling(-1)) setPossibleMove(moves, 1, y);
|
|
if(!attack && checkCastling(1)) setPossibleMove(moves, 6, y);
|
|
return moves;
|
|
}
|
|
|
|
/**
|
|
* Check if left (-1) or right (1) castling is possible
|
|
* @param direction -1 for left, 1 for right
|
|
* @return castling possibility
|
|
*/
|
|
private boolean checkCastling(int direction) {
|
|
if(moveCount == 0 && !player.inCheck() && checkRook(direction)) {
|
|
for (int pX = x + direction; (pX > 0 && pX < 7); pX += direction) {
|
|
if(chessboard.isEndangered(pX, y) || chessboard.getPiece(pX, y) != null) return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Check if left or right Rook is able to do castling
|
|
* @param direction -1 for left Rook, 1 for right Rook
|
|
* @return rook castling ability
|
|
*/
|
|
private boolean checkRook(int direction) {
|
|
int rookX = direction == -1 ? 0 : 7;
|
|
APiece rook = chessboard.getPiece(rookX, y);
|
|
return rook != null && rook instanceof Rook && rook.getMoveCount() == 0;
|
|
}
|
|
|
|
/**
|
|
* Move the piece to the new position
|
|
* @param newX new X position
|
|
* @param newY new Y position
|
|
* @param animate animate piece's move
|
|
*/
|
|
@Override
|
|
public void move(int newX, int newY, boolean animate) {
|
|
super.move(newX, newY, animate);
|
|
if(moveCount == 1 && x == 6) {
|
|
APiece rook = chessboard.getPiece(7, y);
|
|
if(rook != null) rook.setPosition(5, y);
|
|
}
|
|
if(moveCount == 1 && x == 1) {
|
|
APiece rook = chessboard.getPiece(0, y);
|
|
if(rook != null) rook.setPosition(3, y);
|
|
}
|
|
}
|
|
}
|