Commit baf5b2cd by Paktalin

Refactoring in MoveStrategy and its tests

parent ec7d9442
...@@ -6,19 +6,16 @@ import com.paktalin.chess.Position; ...@@ -6,19 +6,16 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
public class KingMoveStrategy extends MoveStrategy{ public class KingMoveStrategy implements MoveStrategy{
@Override @Override
public boolean isMovable(Position from, Position to) { public boolean isMovable(Position from, Position to) {
initialize(from, to); boolean nearbyRows = zeroOrOneDistance(from.getRow(), to.getRow());
return !(distantRows() || distantColumns()); boolean nearbyColumns = zeroOrOneDistance(from.getIntColumn(), to.getIntColumn());
return nearbyRows && nearbyColumns;
} }
private boolean distantRows() { private boolean zeroOrOneDistance(int from, int to) {
return Math.abs(toRow - fromRow) > 1; return Math.abs(to - from) <= 1;
}
private boolean distantColumns() {
return Math.abs(toColumn - fromColumn) > 1;
} }
} }
...@@ -6,16 +6,6 @@ import com.paktalin.chess.Position; ...@@ -6,16 +6,6 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
public abstract class MoveStrategy { public interface MoveStrategy {
int fromRow, fromColumn; boolean isMovable(Position from, Position to);
int toRow, toColumn;
void initialize(Position from, Position to) {
fromRow = from.getRow();
fromColumn = from.getIntColumn();
toRow = to.getRow();
toColumn = to.getIntColumn();
}
public abstract boolean isMovable(Position from, Position to);
} }
...@@ -6,23 +6,21 @@ import com.paktalin.chess.Position; ...@@ -6,23 +6,21 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
public class QueenMoveStrategy extends MoveStrategy { public class QueenMoveStrategy implements MoveStrategy {
@Override @Override
public boolean isMovable(Position from, Position to) { public boolean isMovable(Position from, Position to) {
initialize(from, to); boolean sameRow = sameCoordinate(from.getRow(), to.getRow());
return sameRow() || sameColumn() || diagonal(); boolean sameColumn = sameCoordinate(from.getIntColumn(), to.getIntColumn());
boolean diagonal = diagonal(from, to);
return sameRow || sameColumn || diagonal;
} }
private boolean sameRow() { private boolean sameCoordinate(int from, int to) {
return fromRow == toRow; return from == to;
} }
private boolean sameColumn() { private boolean diagonal(Position from, Position to) {
return fromColumn == toColumn; return Math.abs(to.getRow() - from.getRow()) == Math.abs(to.getIntColumn() - from.getIntColumn());
}
private boolean diagonal() {
return Math.abs(toRow - fromRow) == Math.abs(toColumn - fromColumn);
} }
} }
package com.paktalin.chess.moves; package com.paktalin.chess.moves;
import com.paktalin.chess.Position; import com.paktalin.chess.Position;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
class KingMoveStrategyTest extends MoveStrategyTest { class KingMoveStrategyTest {
@Override
MoveStrategy moveStrategy() {
return new KingMoveStrategy();
}
@Override private Position initialPosition = Position.create("b5");
Position accessiblePosition() { private MoveStrategy moveStrategy = new KingMoveStrategy();
return Position.create("a6");
}
@Override @Test
Position notAccessiblePosition() { void isMovable() {
return Position.create("d4"); assertTrue(moveStrategy.isMovable(initialPosition, Position.create("a6")));
assertFalse(moveStrategy.isMovable(initialPosition, Position.create("d4")));
} }
} }
\ No newline at end of file
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Created by Paktalin on 31/05/2018.
*/
abstract class MoveStrategyTest {
private Position accessiblePosition, notAccessiblePosition, initialPosition;
private MoveStrategy moveStrategy;
@BeforeEach
void init() {
initialPosition = Position.create("b5");
moveStrategy = moveStrategy();
accessiblePosition = accessiblePosition();
notAccessiblePosition = notAccessiblePosition();
}
abstract MoveStrategy moveStrategy();
abstract Position accessiblePosition();
abstract Position notAccessiblePosition();
@Test
void isMovable() {
assertTrue(moveStrategy.isMovable(initialPosition, accessiblePosition));
assertFalse(moveStrategy.isMovable(initialPosition, notAccessiblePosition));
}
}
package com.paktalin.chess.moves; package com.paktalin.chess.moves;
import com.paktalin.chess.Position; import com.paktalin.chess.Position;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
class QueenMoveStrategyTest extends MoveStrategyTest { class QueenMoveStrategyTest {
@Override
MoveStrategy moveStrategy() {
return new QueenMoveStrategy();
}
@Override private Position initialPosition = Position.create("b5");
Position accessiblePosition() { private MoveStrategy moveStrategy = new QueenMoveStrategy();
return Position.create("d3");
}
@Override @Test
Position notAccessiblePosition() { void isMovable() {
return Position.create("f7"); assertTrue(moveStrategy.isMovable(initialPosition, Position.create("d3")));
assertFalse(moveStrategy.isMovable(initialPosition, Position.create("f7")));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment