Chess/src/Pawn.java

116 lines
3.7 KiB
Java

import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* Pawn piece class
*/
public class Pawn extends APiece {
/**
* Create new Pawn piece
* @param player player
* @param x piece's X location
* @param y piece's Y location
*/
public Pawn(Player player, int x, int y) {
super(player, x, y);
}
/**
* Paint the Pawn piece
* @param g2 Graphics2D
*/
@Override
public void paint(Graphics2D g2) {
paintObjects(g2, new Shape[]{getStand(), getEllipse(30, 20, 40, 40)});
}
/**
* Get Pawn'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];
int directionY = player.getStartPosition() == StartPosition.TOP ? 1 : -1;
for (int i = -1; i <= 1; i++) {
boolean isPieceThere = chessboard.getPiece(x+i, y+directionY) != null;
if((i != 0 && (attack || isPieceThere)) || (i == 0 && (!attack && !isPieceThere))) setPossibleMove(moves, x+i, y+directionY);
}
boolean firstPlaceEmpty = chessboard.getPiece(x, y+directionY*2) == null;
boolean secondPlaceEmpty = chessboard.getPiece(x, y+directionY) == null;
if(moveCount == 0 && firstPlaceEmpty && secondPlaceEmpty && !attack) setPossibleMove(moves, x, y + directionY*2);
for (int directionX : new int[]{-1, 1}) {
if(checkEnPassant(directionX, directionY)) setPossibleMove(moves, x+directionX, y+directionY);
}
return moves;
}
/**
* Check if en passant possible
* @param directionX en passant X direction
* @param directionY Pawn's Y direction
* @return true, if possible
*/
private boolean checkEnPassant(int directionX, int directionY) {
PiecePosition removedPiece = new PiecePosition(x+directionX, y);
PiecePosition movedPiece = new PiecePosition(x+directionX, y+directionY);
APiece piece = chessboard.getPiece(removedPiece);
if(!(piece instanceof Pawn)) return false;
int pieceY = piece.getPlayer().getStartPosition() == StartPosition.TOP ? 3 : 4;
if(!chessboard.lastMove[y][x+directionX] || y != pieceY || piece.getMoveCount() != 1) return false;
chessboard.addPiece(this, movedPiece);
chessboard.removePiece(x, y);
chessboard.removePiece(removedPiece);
boolean inCheck = player.inCheck();
chessboard.removePiece(movedPiece);
chessboard.addPiece(this, x, y);
chessboard.addPiece(piece, removedPiece);
if(inCheck) return false;
return true;
}
/**
* 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) {
int directionY = player.getStartPosition() == StartPosition.TOP ? 1 : -1;
for (int directionX : new int[]{-1, 1}) {
if(checkEnPassant(directionX, directionY) && newX == x+directionX) {
APiece piece = chessboard.getPiece(x+directionX, y);
if(piece != null) piece.remove(true);
}
}
super.move(newX, newY, animate);
int changeY = player.getStartPosition() == StartPosition.BOTTOM ? 0 : chessboard.SQUARE_COUNT-1;
if(y == changeY) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
new Queen(player, x, y);
}
}, animate ? 500 : 0);
}
}
}