Skip to content

Commit

Permalink
Reduce number of lines of code
Browse files Browse the repository at this point in the history
  • Loading branch information
xaizek committed Apr 20, 2012
1 parent 60e7413 commit 3cfaf2c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 116 deletions.
Expand Up @@ -16,27 +16,8 @@ public class BishopRules
{
public boolean isMovePossible()
{
// check move direction
final int yDiff = endPosition.getY() - startPosition.getY();
final int xDiff = endPosition.getX() - startPosition.getX();
if (Math.abs(yDiff) != Math.abs(xDiff))
{
return false;
}

// check move with fight
final Piece endPiece = getPiece(board, endPosition);
if (endPiece != null && endPiece.getColor() != opponentColor)
{
return false;
}

// check pieces on the way
if (arePiecesOnTheWay())
{
return false;
}

return true;
return (yAbsDiff == xAbsDiff &&
checkFight() &&
!arePiecesOnTheWay());
}
}
Expand Up @@ -16,21 +16,6 @@ public class KingRules
{
public boolean isMovePossible()
{
// check move distance
final int yAbsDiff = Math.abs(endPosition.getY() - startPosition.getY());
final int xAbsDiff = Math.abs(endPosition.getX() - startPosition.getX());
if (yAbsDiff > 1 || xAbsDiff > 1)
{
return false;
}

// check move with fight
final Piece endPiece = getPiece(board, endPosition);
if (endPiece != null && endPiece.getColor() != opponentColor)
{
return false;
}

return true;
return ((yAbsDiff <= 1 && xAbsDiff <= 1) && checkFight());
}
}
Expand Up @@ -16,20 +16,7 @@ public class KnightRules
{
public boolean isMovePossible()
{
final int yAbsDiff = Math.abs(endPosition.getY() - startPosition.getY());
final int xAbsDiff = Math.abs(endPosition.getX() - startPosition.getX());

// check move distances
if (!(yAbsDiff == 2 && xAbsDiff == 1 || yAbsDiff == 1 && xAbsDiff == 2))
return false;

final Piece endPiece = getPiece(board, endPosition);

// check simple move
if (endPiece == null)
return true;

// check simple move
return endPiece.getColor() == opponentColor;
return (((yAbsDiff == 2 && xAbsDiff == 1 || yAbsDiff == 1 && xAbsDiff == 2)) &&
checkFight());
}
}
Expand Up @@ -23,9 +23,6 @@ else if (ourColor == Color.BLACK && startPosition.getY() == 6)
maxStep = 2;

// check move distance
final int yDiff = endPosition.getY() - startPosition.getY();
final int yAbsDiff = Math.abs(yDiff);
final int xAbsDiff = Math.abs(endPosition.getX() - startPosition.getX());
if (yAbsDiff > maxStep)
return false;

Expand All @@ -34,7 +31,7 @@ else if (ourColor == Color.BLACK && startPosition.getY() == 6)
if (yDiff*allowed < 0)
return false;

final Piece endPiece = getPiece(board, endPosition);
final Piece endPiece = getPiece(endPosition);

// check simple move
if (endPiece == null)
Expand All @@ -44,7 +41,7 @@ else if (ourColor == Color.BLACK && startPosition.getY() == 6)
// check long move
final int middleY = startPosition.getY() + (int)Math.signum(yDiff);
Position middlePosition = new StandardPosition(startPosition.getX(), middleY);
Piece middlePiece = getPiece(board, middlePosition);
Piece middlePiece = getPiece(middlePosition);
if (middlePiece != null)
return false;
}
Expand All @@ -58,7 +55,7 @@ else if (xAbsDiff == 1)
}

// check move with fight
if (endPiece.getColor() == opponentColor)
if (endPiece.getColor() != ourColor)
return xAbsDiff == 1;

return false;
Expand All @@ -69,7 +66,7 @@ private boolean isEnPassant()
if (!previousMoveExists)
return false;

if (getPiece(board, prevEndPosition).getName() != ChessPieceName.PAWN)
if (getPiece(prevEndPosition).getName() != ChessPieceName.PAWN)
return false;

if (Math.abs(prevEndPosition.getY() - prevStartPosition.getY()) != 2)
Expand Down
Expand Up @@ -27,6 +27,12 @@ public void setCurrentMove(String move)
{
startPosition = parsePosition(move.substring(0, 2));
endPosition = parsePosition(move.substring(2, 4));

yDiff = endPosition.getY() - startPosition.getY();
xDiff = endPosition.getX() - startPosition.getX();

yAbsDiff = Math.abs(yDiff);
xAbsDiff = Math.abs(xDiff);
}

public void setPreviousMove(String move)
Expand All @@ -48,24 +54,28 @@ private Position parsePosition(String position)

public abstract boolean isMovePossible();

protected Piece getPiece(Board board, Position position)
protected Piece getPiece(Position position)
{
final Area endArea = board.getAreaAt(position.getX(), position.getY());
return endArea.getPiece();
}

protected boolean checkFight()
{
final Piece endPiece = getPiece(endPosition);
return (endPiece == null || endPiece.getColor() == opponentColor);
}

protected boolean arePiecesOnTheWay()
{
final int yDiff = endPosition.getY() - startPosition.getY();
final int xDiff = endPosition.getX() - startPosition.getX();
final int yDelta = yDiff/((yDiff != 0) ? Math.abs(yDiff) : 1);
final int xDelta = xDiff/((xDiff != 0) ? Math.abs(xDiff) : 1);
final int yDelta = yDiff/((yDiff != 0) ? yAbsDiff : 1);
final int xDelta = xDiff/((xDiff != 0) ? xAbsDiff : 1);

Position runner = new StandardPosition(startPosition.getX() + xDelta,
startPosition.getY() + yDelta);
while (!runner.equals(endPosition))
{
final Piece piece = getPiece(board, runner);
final Piece piece = getPiece(runner);
if (piece != null)
{
return true;
Expand All @@ -76,16 +86,20 @@ protected boolean arePiecesOnTheWay()
return false;
}

protected Board board;
protected Color ourColor;
protected Color opponentColor;
protected boolean previousMoveExists;

protected int yDiff, xDiff;
protected int yAbsDiff, xAbsDiff;

// positions of current move
protected Position startPosition;
protected Position endPosition;

// positions of previous move
protected Position prevStartPosition;
protected Position prevEndPosition;

private Board board;
private Color opponentColor;
}
Expand Up @@ -8,8 +8,6 @@
import ua.edu.donntu.cs.chess.components.model.chess.ChessPieceName;
import ua.edu.donntu.cs.chess.components.model.impl.StandardPosition;

import java.util.Map;

/**
* Description.
*/
Expand All @@ -18,27 +16,8 @@ public class QueenRules
{
public boolean isMovePossible()
{
// check move direction
final int yDiff = endPosition.getY() - startPosition.getY();
final int xDiff = endPosition.getX() - startPosition.getX();
if (Math.abs(yDiff) != Math.abs(xDiff) && (yDiff != 0 && xDiff != 0))
{
return false;
}

// check move with fight
final Piece endPiece = getPiece(board, endPosition);
if (endPiece != null && endPiece.getColor() != opponentColor)
{
return false;
}

// check pieces on the way
if (arePiecesOnTheWay())
{
return false;
}

return true;
return ((yAbsDiff == xAbsDiff || (yAbsDiff == 0 || xAbsDiff == 0)) &&
checkFight() &&
!arePiecesOnTheWay());
}
}
Expand Up @@ -8,8 +8,6 @@
import ua.edu.donntu.cs.chess.components.model.chess.ChessPieceName;
import ua.edu.donntu.cs.chess.components.model.impl.StandardPosition;

import java.util.Map;

/**
* Description.
*/
Expand All @@ -18,27 +16,8 @@ public class RookRules
{
public boolean isMovePossible()
{
// check move direction
final int yDiff = endPosition.getY() - startPosition.getY();
final int xDiff = endPosition.getX() - startPosition.getX();
if (yDiff != 0 && xDiff != 0)
{
return false;
}

// check move with fight
final Piece endPiece = getPiece(board, endPosition);
if (endPiece != null && endPiece.getColor() != opponentColor)
{
return false;
}

// check pieces on the way
if (arePiecesOnTheWay())
{
return false;
}

return true;
return ((yAbsDiff == 0 || xAbsDiff == 0) &&
checkFight() &&
!arePiecesOnTheWay());
}
}

0 comments on commit 3cfaf2c

Please sign in to comment.