Permalink
Browse files

Reduce number of lines of code

  • Loading branch information...
1 parent 60e7413 commit 3cfaf2ca94a15c8842a987156987a69c4cecdbef @xaizek committed Apr 20, 2012
@@ -16,27 +16,8 @@
{
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());
}
}
@@ -16,21 +16,6 @@
{
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());
}
}
@@ -16,20 +16,7 @@
{
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());
}
}
@@ -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;
@@ -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)
@@ -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;
}
@@ -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;
@@ -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)
@@ -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)
@@ -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;
@@ -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;
}
@@ -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.
*/
@@ -18,27 +16,8 @@
{
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());
}
}
@@ -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.
*/
@@ -18,27 +16,8 @@
{
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.