Commit baf5b2cd by Paktalin

Refactoring in MoveStrategy and its tests

parent ec7d9442
......@@ -6,19 +6,16 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
*/
public class KingMoveStrategy extends MoveStrategy{
public class KingMoveStrategy implements MoveStrategy{
@Override
public boolean isMovable(Position from, Position to) {
initialize(from, to);
return !(distantRows() || distantColumns());
boolean nearbyRows = zeroOrOneDistance(from.getRow(), to.getRow());
boolean nearbyColumns = zeroOrOneDistance(from.getIntColumn(), to.getIntColumn());
return nearbyRows && nearbyColumns;
}
private boolean distantRows() {
return Math.abs(toRow - fromRow) > 1;
}
private boolean distantColumns() {
return Math.abs(toColumn - fromColumn) > 1;
private boolean zeroOrOneDistance(int from, int to) {
return Math.abs(to - from) <= 1;
}
}
......@@ -6,16 +6,6 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
*/
public abstract class MoveStrategy {
int fromRow, fromColumn;
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);
public interface MoveStrategy {
boolean isMovable(Position from, Position to);
}
......@@ -6,23 +6,21 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
*/
public class QueenMoveStrategy extends MoveStrategy {
public class QueenMoveStrategy implements MoveStrategy {
@Override
public boolean isMovable(Position from, Position to) {
initialize(from, to);
return sameRow() || sameColumn() || diagonal();
boolean sameRow = sameCoordinate(from.getRow(), to.getRow());
boolean sameColumn = sameCoordinate(from.getIntColumn(), to.getIntColumn());
boolean diagonal = diagonal(from, to);
return sameRow || sameColumn || diagonal;
}
private boolean sameRow() {
return fromRow == toRow;
private boolean sameCoordinate(int from, int to) {
return from == to;
}
private boolean sameColumn() {
return fromColumn == toColumn;
}
private boolean diagonal() {
return Math.abs(toRow - fromRow) == Math.abs(toColumn - fromColumn);
private boolean diagonal(Position from, Position to) {
return Math.abs(to.getRow() - from.getRow()) == Math.abs(to.getIntColumn() - from.getIntColumn());
}
}
package com.paktalin.chess.moves;
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.
*/
class KingMoveStrategyTest extends MoveStrategyTest {
@Override
MoveStrategy moveStrategy() {
return new KingMoveStrategy();
}
class KingMoveStrategyTest {
@Override
Position accessiblePosition() {
return Position.create("a6");
}
private Position initialPosition = Position.create("b5");
private MoveStrategy moveStrategy = new KingMoveStrategy();
@Override
Position notAccessiblePosition() {
return Position.create("d4");
@Test
void isMovable() {
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;
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.
*/
class QueenMoveStrategyTest extends MoveStrategyTest {
@Override
MoveStrategy moveStrategy() {
return new QueenMoveStrategy();
}
class QueenMoveStrategyTest {
@Override
Position accessiblePosition() {
return Position.create("d3");
}
private Position initialPosition = Position.create("b5");
private MoveStrategy moveStrategy = new QueenMoveStrategy();
@Override
Position notAccessiblePosition() {
return Position.create("f7");
@Test
void isMovable() {
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