Skip to content

Instantly share code, notes, and snippets.

@TheSecretSquad
Last active August 29, 2015 14:18
Show Gist options
  • Save TheSecretSquad/701cf1cd4143f5ab1458 to your computer and use it in GitHub Desktop.
Save TheSecretSquad/701cf1cd4143f5ab1458 to your computer and use it in GitHub Desktop.
public interface MovesReceiver {
PathBuilder path(final MoveCount moveCount);
StepBuilder step();
}
public class MoveCount {
private final int number;
public MoveCount(final int number) {
if(number < 0)
throw new NegativeMoveCountException();
this.number = number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + number;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MoveCount other = (MoveCount) obj;
if (number != other.number)
return false;
return true;
}
}
public interface PathBuilder extends RankPathBuilder, FilePathBuilder {
FilePathBuilder forwardAnd();
FilePathBuilder backwardAnd();
RankPathBuilder leftAnd();
RankPathBuilder rightAnd();
}
public interface RankPathBuilder {
MoveFinalizer forward();
MoveFinalizer backward();
}
public interface FilePathBuilder {
MoveFinalizer left();
MoveFinalizer right();
}
public interface StepBuilder extends RankStepBuilder, FileStepBuilder {
FileStepBuilder forwardAnd(final MoveCount moveCount);
FileStepBuilder backwardAnd(final MoveCount moveCount);
RankStepBuilder leftAnd(final MoveCount moveCount);
RankStepBuilder rightAnd(final MoveCount moveCount);
}
public interface RankStepBuilder {
MoveFinalizer forward(final MoveCount moveCount);
MoveFinalizer backward(final MoveCount moveCount);
}
public interface FileStepBuilder {
MoveFinalizer left(final MoveCount moveCount);
MoveFinalizer right(final MoveCount moveCount);
}
public interface MoveFinalizer {
void avoidEnemiesOf(final Piece piece);
void attackEnemies(final Piece piece);
}
public class Pawn extends ChessPiece {
private boolean hasMoved = false;
public Pawn(final Color color) {
super(ChessPieceType.PAWN, color);
}
@Override
public void describeMovesTo(final MovesReceiver movesReceiver) {
if(!hasMoved)
movesReceiver.path(new MoveCount(2)).forward().avoidEnemiesOf(this);
else
movesReceiver.step().forward(new MoveCount(1)).avoidEnemiesOf(this);
movesReceiver.step().forwardAnd(new MoveCount(1)).left(new MoveCount(1)).avoidEnemiesOf(this);
}
public void move() {
hasMoved = true;
}
}
// Test Sample
private Pawn pawn;
@Mock
private MovesReceiver movesReceiver;
@Before
public void setUp() throws Exception {
PathBuilder pathBuilder = mock(PathBuilder.class);
StepBuilder stepBuilder = mock(StepBuilder.class);
FileStepBuilder fileStepBuilder = mock(FileStepBuilder.class);
when(movesReceiver.path(isA(MoveCount.class))).thenReturn(pathBuilder);
when(movesReceiver.step()).thenReturn(stepBuilder);
when(pathBuilder.forward()).thenReturn(mock(MoveFinalizer.class));
when(stepBuilder.forward(isA(MoveCount.class))).thenReturn(mock(MoveFinalizer.class));
when(stepBuilder.forwardAnd(isA(MoveCount.class))).thenReturn(fileStepBuilder);
when(fileStepBuilder.left(isA(MoveCount.class))).thenReturn(mock(MoveFinalizer.class));
pawn = new Pawn(Color.LIGHT);
}
@Test
public void WhenDescribingMoves_ShouldOnlyMoveInAPathTwoForwardIfHasNotMoved() {
pawn.describeMovesTo(movesReceiver);
verify(movesReceiver).path(new MoveCount(2));
verify(pathBuilder).forward();
verify(moveFinalizer).avoidEnemiesOf(isA(Piece.class));
verify(stepBuilder, never()).forward(new MoveCount(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment