Show Validator.java syntax highlighted
package chess.logic;
import chess.data.Piece;
import chess.data.Ply;
import chess.data.Square;
/**
* Validates the users plies, and contains
* all possible moves for pieces.
*
* @author Juho Karppinen
* @version $Id: Validator.java 102 2004-11-12 14:31:37Z jkarppin $
*/
public class Validator {
// the possible directions of a bishop
public final static int[][] bishopDirs = {
{-1,-1},{-1,1},{1,-1},{1,1}
};
// the possible directions of a rookie
public final static int[][] rookDirs = {
{-1,0},{1,0},{0,-1},{0,1}
};
// the possible moves of a knight
public final static int [] [] knightMoves = {
{ -2, -1}, { -2, 1},
{ -1, -2}, { -1, 2},
{ 1, -2}, { 1, 2},
{ 2, -1}, { 2, 1}
};
// the possible moves of a king
public final static int [] [] kingMoves = {
{ -1, 0}, { -1, 1},
{ 0, 1}, { 1, 1},
{ 1, 0}, { 1, -1},
{ 0, -1}, { -1, -1}
};
private GameState curState;
public Validator(GameState state) {
curState = state;
}
/**
* Validate the ply
* @param ply
* @return Error message if move is not allowed, null otherwise
*/
public String validatePlayerPly(Ply ply) {
int startCol = Square.getColumn(ply.getStartPosition());
int startRow = Square.getRow(ply.getStartPosition());
int endCol = Square.getColumn(ply.getEndPosition());
int endRow = Square.getRow(ply.getEndPosition());
int myColor = ply.getPiece().getColor();
int direction = ply.getPiece().getColor();
Piece targetPiece = curState.pieceAt(ply.getEndPosition());
boolean eating = (targetPiece != null);
if(eating && (targetPiece.getColor() == myColor)) {
return "cannot eat own piece";
}
if(ply.getPiece().getType() == Piece.PAWN) {
int cols = Math.abs(endCol-startCol);
int rows = Math.abs(endRow-startRow);
if(rows == 1) {
if( eating && cols == 1 )
return null; // eats
else if( ! eating && cols == 0)
return null; // one forward
} else if( rows==2 && cols == 0
&& ply.getPiece().getHomePosition() == curState.pos(ply.getPiece())
&& !eating
&& curState.pieceAt(startCol, startRow+direction) == null) {
// can move 2 forward
return null;
}
} else if(ply.getPiece().getType() == Piece.BISHOP) {
return canDiagonal(startCol, startRow, endCol, endRow);
} else if(ply.getPiece().getType() == Piece.KNIGHT) {
for(int i=0; i < knightMoves.length; i++) {
if((startCol + knightMoves[i][0] == endCol)
&& (startRow + knightMoves[i][1] == endRow))
return null;
else
continue;
}
} else if(ply.getPiece().getType() == Piece.KING) {
for(int i=0; i < kingMoves.length; i++) {
if((startCol + kingMoves[i][0] == endCol)
&& (startRow + kingMoves[i][1] == endRow))
return null;
else
continue;
}
} else if(ply.getPiece().getType() == Piece.ROOK) {
int cols = endCol-startCol;
int rows = endRow-startRow;
if(cols!=0 && rows!=0 ) {
return "not horizontal/vertical";
} else if(startCol == endCol){
return canVertical(startRow, startCol, endCol);
} else {
return canHorizontal(startRow, startCol, endCol);
}
} else if(ply.getPiece().getType() == Piece.QUEEN) {
if(startCol == endCol){
return canVertical(startRow, startCol, endCol);
} else if(startRow == endRow){
return canHorizontal(startRow, startCol, endCol);
} else {
return canDiagonal(startCol, startRow, endCol, endRow);
}
}
return "end of valid moves";
}
/**
* Can move horizontally without any interfere
* @param y
* @param startX
* @param endX
* @return
*/
private String canHorizontal(int y, int startX, int endX) {
for(int x = Math.min(startX, endX) + 1; x < Math.max(startX, endX); x++) {
if(curState.pieceAt(x, y) != null) {
return "horizontal move: someone blocks the way";
}
}
return null;
}
/**
* Can move vertically
* @param x
* @param startY
* @param endY
* @return
*/
private String canVertical(int x, int startY, int endY) {
for(int y = Math.min(startY, endY) + 1; y < Math.max(startY, endY); y++) {
if(curState.pieceAt(x, y) != null) {
return "vertical move: someone blocks the way";
}
}
return null;
}
/**
* Can move diagonally
*
* @param startX
* @param startY
* @param endX
* @param endY
* @return
*/
private String canDiagonal(int startX, int startY, int endX, int endY) {
int diffX = Math.abs(endX-startX);
int diffY = Math.abs(endY-startY);
if(diffX != diffY) {
return "diagonal move: not diagonal";
} else {
int col = startX;
int row = startY;
col += (endX > startX ? 1 : -1);
row += (endY > startY ? 1 : -1);
while(col != endX && row != endY) {
if(curState.pieceAt(col, row) != null) {
return "diagonal move: someone blocks the way";
}
col += (endX > startX ? 1 : -1);
row += (endY > startY ? 1 : -1);
}
return null;
}
}
}
See more files for this project here