package de.movope.game;

public class MoveEvaluatorForPieceSet {

    private ChessBoard board;

    public MoveEvaluatorForPieceSet(ChessBoard board) {
        this.board = board;
    }

    public static MoveEvaluatorForPieceSet on(ChessBoard board) {
        return new MoveEvaluatorForPieceSet(board);
    }

    public MoveEvaluation analyse(Color color) {
        MoveEvaluation evaluation = getMoveEvaluationForAllPiecesOf(color);
        evaluation.filterMovesBy(move -> !kingInCheckAfter(move, color));
        return evaluation;
    }

    private boolean kingInCheckAfter(Move move, Color color) {
        board.move(move.getFrom(), move.getTo());
        boolean checkmate = new KingInCheck(getMoveEvaluationForAllPiecesOf(color.invert())).test(board);
        board.move(move.getTo(), move.getFrom());
        return checkmate;
    }

    private MoveEvaluation getMoveEvaluationForAllPiecesOf(Color color) {
        MoveEvaluation evaluation = MoveEvaluation.empty();
        for (String square : board.getSquaresWithPiece(color)) {
            MoveEvaluation ofSquare = MoveEvaluatorForPiece.on(board).analyse(Square.create(square));
            evaluation = evaluation.join(ofSquare);
        }
        return evaluation;
    }
}

@@ -419,24 +419,31 @@
 AllPiecesOf(
+board, 
 color);%0A    
@@ -609,37 +609,85 @@
 olor) %7B%0A        
-board
+ChessBoard copy = new ChessBoard(board);%0A        copy
 .move(move.getFr
@@ -719,27 +719,14 @@
     
-boolean checkmate =
+return
  new
@@ -770,16 +770,22 @@
 iecesOf(
+copy, 
 color.in
@@ -802,90 +802,13 @@
 est(
-board);%0A        board.move(move.getTo(), move.getFrom());%0A        return checkmate
+copy)
 ;%0A  
@@ -871,16 +871,34 @@
 iecesOf(
+ChessBoard board, 
 Color co

use copy-constructor of ChessBoard to evaluate different moves