Skip to content

Commit

Permalink
fix logic in OneMoveForwardHandler class and TwoMovesForwardHandler c…
Browse files Browse the repository at this point in the history
…lass to move within a fixed column
  • Loading branch information
OsamaX01 committed Aug 25, 2022
1 parent 208fd69 commit affdaea
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/handlers/EnemyOnDestinationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package handlers;public class EnemyOnDestinationHandler {
}
2 changes: 2 additions & 0 deletions src/handlers/LeftSquareDiagonallyHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package handlers;public class LeftSquareDiagonallyHandler {
}
10 changes: 7 additions & 3 deletions src/handlers/OneMoveForwardHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
public class OneMoveForwardHandler extends BaseHandler {
@Override
protected boolean isValid(Board board, Square from, Square to) {
if (board.getPieceAt(from).getOwner().getColor() == Color.BLACK) {
return to.getRow() == from.getRow() - 1;
boolean result = to.getColumn() == from.getColumn();
if (board.getPieceAt(from).getOwner().getColor() == Color.WHITE) {
result = result && to.getRow() == from.getRow() + 1;
} else {
result = result && to.getRow() == from.getRow() - 1;
}
return to.getRow() == from.getRow() + 1;

return result;
}
}
2 changes: 2 additions & 0 deletions src/handlers/RightSquareDiagonallyHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package handlers;public class RightSquareDiagonallyHandler {
}
10 changes: 7 additions & 3 deletions src/handlers/TwoMovesForwardHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
public class TwoMovesForwardHandler extends BaseHandler {
@Override
protected boolean isValid(Board board, Square from, Square to) {
if (board.getPieceAt(from).getOwner().getColor() == Color.BLACK) {
return to.getRow() == from.getRow() - 2;
boolean result = to.getColumn() == from.getColumn();
if (board.getPieceAt(from).getOwner().getColor() == Color.WHITE) {
result = result && to.getRow() == from.getRow() + 2;
} else {
result = result && to.getRow() == from.getRow() - 2;
}
return to.getRow() == from.getRow() + 2;

return result;
}
}
24 changes: 24 additions & 0 deletions src/movements/LeftSquareDiagonallyIfEnemyExistsStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package movements;

import board.Board;
import general.Square;
import handlers.*;

public class LeftSquareDiagonallyIfEnemyExists extends BaseHandler {
@Override
protected boolean isValid(Board board, Square from, Square to) {
if (board == null || from == null || to == null) {
throw new IllegalArgumentException("NullPointer argument");
}

BaseHandler handler1 = new ValidStartLocationHandler();
BaseHandler handler2 = new ValidDestinationHandler();
BaseHandler handler3 = new LeftSquareDiagonallyHandler();
BaseHandler handler4 = new EnemyOnDestinationHandler();
handler1.setNext(handler2);
handler2.setNext(handler3);
handler3.setNext(handler4);

return handler1.canHandle(board, from, to);
}
}
2 changes: 2 additions & 0 deletions src/movements/RightSquareDiagonallyIfEnemyExistsStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package movements;public class RightSquareDiagonallyIfEnemyExists {
}

0 comments on commit affdaea

Please sign in to comment.