var Chess = (function(Chess) {
    'use strict';

    Chess.Game = (function() {

        var buildEnPassantEligiblePawn = function() { //todo delegate this stuff
            return this.getBoard().getPieceByPosition(
                new Chess.Movement.Position(
                    this.__internal__.data.enPassantContext.position.x,
                    this.__internal__.data.enPassantContext.position.y
                )
            );
        };

        var buildEnPassantEligiblePawnPosition = function() {  //todo delegate this stuff
            return new Chess.Movement.Position(
                this.__internal__.data.enPassantContext.position.x,
                this.__internal__.data.enPassantContext.position.y
            );
        };

        var getEnPassantCoordinator = function() {  //todo delegate this stuff
            if(null === this.__internal__.enPassantCoordinator) {
                this.__internal__.enPassantCoordinator = new Chess.Movement.Pawn.EnPassantCoordinator(
                    this.getBoard(),
                    this.__internal__.data.enPassantContext ? buildEnPassantEligiblePawn.call(this) : null,
                    this.__internal__.data.enPassantContext ? buildEnPassantEligiblePawnPosition.call(this) : null
                );
            }
            return this.__internal__.enPassantCoordinator;
        };

        function Game(data) {
            this.__internal__ = {
                data: data,
                board: null,
                coordinator: null,
                enPassantCoordinator: null
            };
        }

        Game.prototype.getBoard = function() {
            if(null === this.__internal__.board) {
                this.__internal__.board = new Chess.Board.Board();
                this.__internal__.board.initPieces(this.__internal__.data.pieces || []);
            }
            return this.__internal__.board;
        };

        Game.prototype.getCoordinator = function() { //todo delegate this stuff
            if(null === this.__internal__.coordinator) {
                this.__internal__.coordinator = new Chess.Movement.Coordinator(
                    this.getBoard(),
                    new Chess.Piece.ColorSwitcher(
                        new Chess.Piece.Color(this.__internal__.data.playingColor || Chess.Piece.Color.WHITE)
                    ),
                    new Chess.Movement.DisplacementsCalculator(this),
                    new Chess.Movement.Pawn.EnPassantContext(
                        getEnPassantCoordinator.call(this),
                        new Chess.Movement.Pawn.PawnDisplacementAnalyser()
                    )
                );
            }
            return this.__internal__.coordinator; //todo utiliser un proxy!
        };

        Game.prototype.exportToJson = function() { //todo rename in export (because it is not a json)
            //todo delegate this stuff
            var data = {
                playingColor: this.getCoordinator().getPlayingColor().getValue(),
                pieces: this.getBoard().exportPieces()
            };

            var pawnPosition = getEnPassantCoordinator.call(this).getPawnPosition();
            if(pawnPosition) {
                data.enPassantContext = {
                    position: {
                        x: pawnPosition.getX(),
                        y: pawnPosition.getY()
                    }
                };
            }

            return data;
        };

        Game.prototype.isInCheck = function(changePlayingColor) {//todo delegate this stuff
            var builder = new Chess.Simulator.GameStateBuilder();
            builder.createGameState(this, !changePlayingColor);
            var movablePieces = builder.getGameState().getMovablePieces();
            for(var i = 0, iLen = movablePieces.length; i < iLen; ++i) {
                var eligibleSquares = movablePieces[i].getEligibleSquares();
                for(var j = 0, jLen = eligibleSquares.length; j < jLen; ++j) {
                    if(eligibleSquares[j].getSquare().getPiece() instanceof Chess.Piece.Type.King) {
                        return true;
                    }
                }
            }
            return false;

            /*

             var builder = new Chess.Simulator.GameStateBuilder();
             if(forNotPlayingColor){
             debugger;
             builder.createGameState(this, this.getCoordinator().getPlayingColor());
             }else{
             var colorSwitcher = new Chess.Piece.ColorSwitcher(this.getCoordinator().getPlayingColor());
             builder.createGameState(this, colorSwitcher.getNotPlayingColor());
             }

             var movablePieces = builder.getGameState().getMovablePieces();
             for(var i = 0, iLen = movablePieces.length; i < iLen; ++i) {
             var eligibleSquares = movablePieces[i].getEligibleSquares();
             for(var j = 0, jLen = eligibleSquares.length; j < jLen; ++j) {
             if(eligibleSquares[j].getSquare().getPiece() instanceof Chess.Piece.Type.King) {
             return true;
             }
             }
             }
             return false;
            * */
        };

        return Game;

    })();

    return Chess;

})(Chess || {});
@@ -3519,35 +3519,9 @@
 r) %7B
-//todo delegate this stuff
 %0A
+
     
@@ -3662,1476 +3662,71 @@
     
-var movablePieces = builder.getGameState().getMovablePieces();%0A            for(var i = 0, iLen = movablePieces.length; i %3C iLen; ++i) %7B%0A                var eligibleSquares = movablePieces%5Bi%5D.getEligibleSquares();%0A                for(var j = 0, jLen = eligibleSquares.length; j %3C jLen; ++j) %7B%0A                    if(eligibleSquares%5Bj%5D.getSquare().getPiece() instanceof Chess.Piece.Type.King) %7B%0A                        return true;%0A                    %7D%0A                %7D%0A            %7D%0A            return false;%0A%0A            /*%0A%0A             var builder = new Chess.Simulator.GameStateBuilder();%0A             if(forNotPlayingColor)%7B%0A             debugger;%0A             builder.createGameState(this, this.getCoordinator().getPlayingColor());%0A             %7Delse%7B%0A             var colorSwitcher = new Chess.Piece.ColorSwitcher(this.getCoordinator().getPlayingColor());%0A             builder.createGameState(this, colorSwitcher.getNotPlayingColor());%0A             %7D%0A%0A             var movablePieces = builder.getGameState().getMovablePieces();%0A             for(var i = 0, iLen = movablePieces.length; i %3C iLen; ++i) %7B%0A             var eligibleSquares = movablePieces%5Bi%5D.getEligibleSquares();%0A             for(var j = 0, jLen = eligibleSquares.length; j %3C jLen; ++j) %7B%0A             if(eligibleSquares%5Bj%5D.getSquare().getPiece() instanceof Chess.Piece.Type.King) %7B%0A             return true;%0A             %7D%0A             %7D%0A             %7D%0A             return false;%0A            * */
+return Boolean(builder.getGameState().getKingKillers().length);
 %0A   

use getKingKillers in isInCheck