Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package access;
- import representation.Board;
- import representation.BoardSquare;
- import java.awt.*;
- public class MoveValidator {
- /**
- * Iterate over every possible move from @param from and check whether one of the
- * positions is equal to @param to.
- *
- * Note: does not handle the validity if the move would result in a check.
- */
- public boolean isValidMove(Board board, BoardSquare from, BoardSquare to) {
- if (from.getPiece() == null) {
- return false;
- }
- switch (from.getPiece().type) {
- case KING:
- // TODO >needs to be checked
- return isValidKingMove(board, from, to);
- case QUEEN:
- // TODO >needs to be checked
- return isValidQueenMove(board, from, to);
- case ROOK:
- // TODO >needs to be checked
- return isValidRookMove(board, from, to);
- case BISHOP:
- // TODO >needs to be checked
- return isValidBishopMove(board, from, to);
- case KNIGHT:
- // TODO >needs to be checked
- return isValidKnightMove(board, from, to);
- case PAWN:
- // TODO >needs to be checked
- return isValidPawnMove(board, from, to);
- default:
- System.err.println("Error: invalid piece type");
- return false;
- }
- }
- private boolean isValidKingMove(Board board, BoardSquare from, BoardSquare to) {
- boolean emptyLeft = from.posY > 0;
- boolean emptyRight = from.posY < 7;
- boolean emptyUp = from.posX > 0;
- boolean emptyDown = from.posX < 7;
- if (emptyLeft && isDestination(board, to, from.posX, from.posY - 1)) {
- return emptySquare(board, from.posX, from.posY - 1)
- || enemyOnSquare(board, from.posX, from.posY - 1, from.getPiece().color);
- }
- }
- /**
- * Does not take into account en passant capture!
- */
- private boolean isValidPawnMove(Board board, BoardSquare from, BoardSquare to) {
- Color color = from.getPiece().color;
- return color == Color.WHITE ? isValidWhitePawnMove(board, from, to) : isValidBlackPawnMove(board, from, to);
- }
- private boolean isValidBlackPawnMove(Board board, BoardSquare from, BoardSquare to) {
- if (isDestination(board, to, from.posX - 1, from.posY + 1)) {
- return enemyOnSquare(board, from.posX - 1, from.posY - 1, from.getPiece().color);
- }
- if (isDestination(board, to, from.posX - 1, from.posY - 1)) {
- return enemyOnSquare(board, from.posX + 1, from.posY - 1, from.getPiece().color);
- }
- if (from.posX == 6) {
- // valid move one and two downwards
- if (isDestination(board, to, from.posX - 1, from.posY)) {
- return emptySquare(board, from.posX - 1, from.posY);
- }
- if (isDestination(board, to, from.posX - 2, from.posY)) {
- return emptySquare(board, from.posX - 2 , from.posY);
- }
- }
- return false;
- }
- private boolean isValidWhitePawnMove(Board board, BoardSquare from, BoardSquare to) {
- if (isDestination(board, to, from.posX + 1, from.posY + 1)) {
- return enemyOnSquare(board, from.posX + 1, from.posY + 1, from.getPiece().color);
- }
- if (isDestination(board, to, from.posX + 1, from.posY - 1)) {
- return enemyOnSquare(board, from.posX + 1, from.posY - 1, from.getPiece().color);
- }
- if (from.posX == 1) {
- // valid move one and two downwards
- if (isDestination(board, to, from.posX + 1, from.posY)) {
- return emptySquare(board, from.posX + 1, from.posY);
- }
- if (isDestination(board, to, from.posX + 2, from.posY)) {
- return emptySquare(board, from.posX + 2, from.posY);
- }
- }
- return false;
- }
- private boolean isValidBishopMove(Board board, BoardSquare from, BoardSquare to) {
- if (isDestinationDiagonallyLeftUpwards(board, from, to)) {
- return true;
- }
- if (isDestinationDiagonallyLeftDownwards(board, from, to)) {
- return true;
- }
- if (isDestinationDiagonallyRightDownwards(board, from, to)) {
- return true;
- }
- // if the candidate was not found among possible moves, the desired move must be invalid
- return isDestinationDiagonallyRightUpwards(board, from, to);
- }
- private boolean isValidRookMove(Board board, BoardSquare from, BoardSquare to) {
- if (isDestinationLeft(board, from, to)) {
- return true;
- }
- if (isDestinationRight(board, from, to)) {
- return true;
- }
- if (isDestinationDownwards(board, from, to)) {
- return true;
- }
- // if the candidate was not found among possible moves, the desired move must be invalid
- return isDestinationUpwards(board, from, to);
- }
- private boolean isValidQueenMove(Board board, BoardSquare from, BoardSquare to) {
- if(isValidRookMove(board, from, to) || isValidBishopMove(board, from, to)) {
- return true;
- }
- // if the candidate was not found among possible moves, the desired move must be invalid
- return false;
- }
- private boolean isValidKnightMove(Board board, BoardSquare from, BoardSquare to) {
- Color color = from.getPiece().color;
- int x = from.posX;
- int y = from.posY;
- //all possible combinations of knight's moves
- int xChange[] = {-1, 1, -1, 1, 2, 2, -2, -2};
- int yChange[] = {2, 2, -2, -2, -1, 1, -1, 1};
- for(int i = 0; i < 8; i++){
- int newX = x + xChange[i];
- int newY = y + yChange[i];
- if(newX >= 0 && newX <= 7 && newY >= 0 && newY <= 7) {
- if (isDestination(board, to, newX, newY)) {
- return emptySquare(board, newX, newY) || enemyOnSquare(board, newX, newY, color);
- }
- }
- }
- return false;
- }
- private boolean isDestinationLeft(Board board, BoardSquare from, BoardSquare to){
- Color color = from.getPiece().color;
- int x = from.posX;
- int y = from.posY - 1;
- for (; y >= 0 && emptySquare(board, x, y); --y) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return y >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationRight(Board board, BoardSquare from, BoardSquare to){
- Color color = from.getPiece().color;
- int x = from.posX;
- int y = from.posY + 1;
- for (; y <= 7 && emptySquare(board, x, y); ++y) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return y <= 7 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationDownwards(Board board, BoardSquare from, BoardSquare to){
- Color color = from.getPiece().color;
- int x = from.posX + 1;
- int y = from.posY;
- for (; x >= 0 && emptySquare(board, x, y); ++x) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return x >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationUpwards(Board board, BoardSquare from, BoardSquare to){
- Color color = from.getPiece().color;
- int x = from.posX - 1;
- int y = from.posY;
- for (; x >= 0 && emptySquare(board, x, y); --x) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return x >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationDiagonallyRightUpwards(Board board, BoardSquare from, BoardSquare to) {
- Color color = from.getPiece().color;
- int x = from.posX - 1;
- int y = from.posY + 1;
- for (; x >= 0 && y <= 7 && emptySquare(board, x, y); --x, ++y) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return x >= 0 && y <= 7 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationDiagonallyRightDownwards(Board board, BoardSquare from, BoardSquare to) {
- Color color = from.getPiece().color;
- int x = from.posX + 1;
- int y = from.posY + 1;
- for (; x <= 7 && y <= 7 && emptySquare(board, x, y); ++x, ++y) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return x <= 7 && y <= 7 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationDiagonallyLeftDownwards(Board board, BoardSquare from, BoardSquare to) {
- Color color = from.getPiece().color;
- int x = from.posX + 1;
- int y = from.posY - 1;
- for (; x <= 7 && y >= 0 && emptySquare(board, x, y); ++x, --y) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return x <= 7 && y >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestinationDiagonallyLeftUpwards(Board board, BoardSquare from, BoardSquare to) {
- Color color = from.getPiece().color;
- int x = from.posX - 1;
- int y = from.posY - 1;
- for (; x >= 0 && y >= 0 && emptySquare(board, x, y); --x, --y) {
- if (isDestination(board, to, x, y)) {
- return true;
- }
- }
- return x >= 0 && y >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
- }
- private boolean isDestination(Board board, BoardSquare to, int x, int y) {
- return board.getPieces()[x][y] == to;
- }
- private boolean emptySquare(Board board, int posX, int posY) {
- return board.getPieces()[posX][posY].isEmpty();
- }
- private boolean enemyOnSquare(Board board, int posX, int posY, Color thisPieceColor) {
- return board.getPieces()[posX][posY].getPiece().color != thisPieceColor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement