|
@ -4,6 +4,13 @@ import java.util.ArrayList; |
|
|
|
|
|
|
|
|
public class ChessBoard { |
|
|
public class ChessBoard { |
|
|
|
|
|
|
|
|
|
|
|
public enum MoveFeedback { |
|
|
|
|
|
ENEMYBEATEN, |
|
|
|
|
|
MOVE, |
|
|
|
|
|
OUTSIDEOFBOARD, |
|
|
|
|
|
INVALID |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private ChessFigure[][] board; |
|
|
private ChessFigure[][] board; |
|
|
|
|
|
|
|
|
public ChessBoard() { |
|
|
public ChessBoard() { |
|
@ -88,14 +95,14 @@ public class ChessBoard { |
|
|
return false; |
|
|
return false; |
|
|
ChessFigure figure = board[sourceY][sourceX]; |
|
|
ChessFigure figure = board[sourceY][sourceX]; |
|
|
//Destination has same Team |
|
|
//Destination has same Team |
|
|
if(board[destY][destX] != null) |
|
|
|
|
|
if(board[destY][destX].getTeam() == figure.getTeam()) |
|
|
|
|
|
|
|
|
if (board[destY][destX] != null) |
|
|
|
|
|
if (board[destY][destX].getTeam() == figure.getTeam()) |
|
|
return false; |
|
|
return false; |
|
|
int deltaX = destX - sourceX; |
|
|
int deltaX = destX - sourceX; |
|
|
int deltaY = sourceY - destY; |
|
|
int deltaY = sourceY - destY; |
|
|
//Pawn special case |
|
|
//Pawn special case |
|
|
if(figure.getType() == ChessFigure.Type.PAWN){ |
|
|
|
|
|
if(board[destY][destX] != null) { |
|
|
|
|
|
|
|
|
if (figure.getType() == ChessFigure.Type.PAWN) { |
|
|
|
|
|
if (board[destY][destX] != null) { |
|
|
if (board[destY][destX].getTeam() == figure.getTeam()) |
|
|
if (board[destY][destX].getTeam() == figure.getTeam()) |
|
|
return false; |
|
|
return false; |
|
|
if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) |
|
|
if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) |
|
@ -103,13 +110,13 @@ public class ChessBoard { |
|
|
if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) |
|
|
if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
if(figure.isRelativeMoveValid(deltaX, deltaY)) |
|
|
|
|
|
|
|
|
if (figure.isRelativeMoveValid(deltaX, deltaY)) |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
if(figure.isRelativeMoveValid(deltaX, deltaY)){ |
|
|
|
|
|
if(figure.getType() == ChessFigure.Type.KNIGHT) |
|
|
|
|
|
|
|
|
if (figure.isRelativeMoveValid(deltaX, deltaY)) { |
|
|
|
|
|
if (figure.getType() == ChessFigure.Type.KNIGHT) |
|
|
return true; |
|
|
return true; |
|
|
if(validateCleanPath(sourceX, sourceY, destX, destY)) |
|
|
|
|
|
|
|
|
if (validateCleanPath(sourceX, sourceY, destX, destY)) |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -126,7 +133,7 @@ public class ChessBoard { |
|
|
sourceY += stepY; |
|
|
sourceY += stepY; |
|
|
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) |
|
|
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) |
|
|
return false; |
|
|
return false; |
|
|
if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) |
|
|
|
|
|
|
|
|
if (Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) |
|
|
return true; |
|
|
return true; |
|
|
while (!(sourceX == destX && sourceY == destY)) { |
|
|
while (!(sourceX == destX && sourceY == destY)) { |
|
|
if (board[sourceY][sourceX] != null) |
|
|
if (board[sourceY][sourceX] != null) |
|
@ -142,6 +149,22 @@ public class ChessBoard { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public MoveFeedback moveFigure(int sourceX, int sourceY, final int destX, final int destY) { |
|
|
|
|
|
if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) |
|
|
|
|
|
return MoveFeedback.OUTSIDEOFBOARD; |
|
|
|
|
|
if (validateMove(sourceX, sourceY, destX, destY)) { |
|
|
|
|
|
MoveFeedback feedback = MoveFeedback.INVALID; |
|
|
|
|
|
if (board[destY][destX] == null) |
|
|
|
|
|
feedback = MoveFeedback.MOVE; |
|
|
|
|
|
else |
|
|
|
|
|
feedback = MoveFeedback.ENEMYBEATEN; |
|
|
|
|
|
board[destY][destX] = board[sourceY][sourceX]; |
|
|
|
|
|
board[sourceY][sourceX] = null; |
|
|
|
|
|
return feedback; |
|
|
|
|
|
} |
|
|
|
|
|
return MoveFeedback.INVALID; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private int getStepWidth(int delta) { |
|
|
private int getStepWidth(int delta) { |
|
|
if (delta == 0) |
|
|
if (delta == 0) |
|
|
return 0; |
|
|
return 0; |
|
|