Skip to content

Commit

Permalink
Some refactoring to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
lrozenblyum committed Mar 15, 2015
1 parent 111316a commit 767715a
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/main/java/com/leokom/chess/engine/PositionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,11 @@ final class PositionGenerator {
* @return new position, after move from squareFrom
*/
Position generate( Move move ) {
if ( move == null ) {
throw new IllegalArgumentException( "Move must be not null" );
}

if ( move == Move.RESIGN ) {
//TODO: technically side to move is useless for such terminal position?
//it indicates a side of possible move IF the position wouldn't be terminal
final Position result = new Position( source.getSideToMove().opposite() );
source.copyStateTo( result );
//TODO: should checkmate move also set this flag?
result.setTerminal( source.getSideToMove().opposite() );
return result;
return getResignPosition();
}



if ( source.getPiece( move.getFrom() ) == null ) {
throw new IllegalArgumentException( "Source square is empty : " + move.getFrom() );
}

if ( source.getPiece( move.getFrom() ).getSide() != source.getSideToMove() ) {
throw new IllegalArgumentException( "Wrong side to move : " + move + ". Currently it's turn of " + source.getSideToMove() );
}
validateStandardMove( move );

String squareFrom = move.getFrom();
String moveTo = move.getTo();
Expand All @@ -69,6 +51,30 @@ Position generate( Move move ) {
}
}

private void validateStandardMove( Move move ) {
if ( move == null ) {
throw new IllegalArgumentException( "Move must be not null" );
}

if ( source.getPiece( move.getFrom() ) == null ) {
throw new IllegalArgumentException( "Source square is empty : " + move.getFrom() );
}

if ( source.getPiece( move.getFrom() ).getSide() != source.getSideToMove() ) {
throw new IllegalArgumentException( "Wrong side to move : " + move + ". Currently it's turn of " + source.getSideToMove() );
}
}

private Position getResignPosition() {
//TODO: technically side to move is useless for such terminal position?
//it indicates a side of possible move IF the position wouldn't be terminal
final Position result = new Position( source.getSideToMove().opposite() );
source.copyStateTo( result );
//TODO: should checkmate move also set this flag?
result.setTerminal( source.getSideToMove().opposite() );
return result;
}

private Position processRookMove( String squareFrom, String move ) {
final Position result = processMoveWithoutSideEffects( squareFrom, move );
//TODO: do I need setting this flag after castling?
Expand Down

0 comments on commit 767715a

Please sign in to comment.