/// <summary> /// Ask the engine to do a move /// </summary> /// <param name="move">The move to do</param> /// <returns>True if the move was valid and therefore has been done</returns> public bool DoMove(Move move) { //No reason to move if it's the same square if (move.StartCoordinate == move.TargetCoordinate) { return(false); } var piece = Board.PieceAt(move.StartCoordinate); var targetPiece = Board.PieceAt(move.TargetCoordinate); //TODO generate exception if (!_ruleGroups.Handle(move, Board)) { return(false); } ICompensableCommand command; switch (move.PieceType) { case Type.King when targetPiece?.Type == Type.Rook && move.PieceColor == targetPiece.Color || Math.Abs(move.TargetCoordinate.X - move.StartCoordinate.X) == 2: command = new CastlingCommand(move, Board); break; case Type.Pawn when targetPiece == null && move.StartCoordinate.X != move.TargetCoordinate.X: command = new EnPassantCommand(move, Board); break; case Type.Pawn when move.TargetCoordinate.Y == (move.PieceColor == Color.White ? 0 : 7): command = new PromoteCommand(move, Board); break; default: command = new MoveCommand(move, Board); break; } //En passant if (move.PieceColor == Color.White) { if (_enPassantPawnWhite != null) { _enPassantPawnWhite.EnPassant = false; _enPassantPawnWhite = null; } } else { if (_enPassantPawnBlack != null) { _enPassantPawnBlack.EnPassant = false; _enPassantPawnBlack = null; } } if (move.PieceType == Type.Pawn && Math.Abs(move.StartCoordinate.Y - move.TargetCoordinate.Y) == 2) { if (move.PieceColor == Color.White) { _enPassantPawnWhite = (Pawn)piece; _enPassantPawnWhite.EnPassant = true; } else { _enPassantPawnBlack = (Pawn)piece; _enPassantPawnBlack.EnPassant = true; } } //Number of moves since last capture if (Board.PieceAt(move.TargetCoordinate) == null) { _container.HalfMoveSinceLastCapture++; } else { _container.HalfMoveSinceLastCapture = 0; } _conversation.Execute(command); _moves.Add(command); return(true); }
/// <summary> /// Ask the engine to do a move /// </summary> /// <param name="move">The move to do</param> /// <returns>True if the move was valid and therefore has been done</returns> public bool DoMove(Move move) { //No reason to move if it's the same square if (move.StartCoordinate == move.TargetCoordinate) { return(false); } Piece piece = Board.PieceAt(move.StartCoordinate); Piece targetPiece = Board.PieceAt(move.TargetCoordinate); //TODO gérer exception if (_ruleGroups.Handle(move, Board)) { ICompensableCommand command; if ((move.PieceType == Type.King) && (((targetPiece?.Type == Type.Rook) && (move.PieceColor == targetPiece.Color)) || (Math.Abs(move.TargetCoordinate.X - move.StartCoordinate.X) == 2))) { command = new CastlingCommand(move, Board); } else if ((move.PieceType == Type.Pawn) && (targetPiece == null) && (move.StartCoordinate.X != move.TargetCoordinate.X)) { command = new EnPassantCommand(move, Board); } else if ((move.PieceType == Type.Pawn) && (move.TargetCoordinate.Y == (move.PieceColor == Color.White ? 0 : 7))) { command = new PromoteCommand(move, Board); } else { command = new MoveCommand(move, Board); } //En passant if (move.PieceColor == Color.White) { if (_enPassantPawnWhite != null) { _enPassantPawnWhite.EnPassant = false; _enPassantPawnWhite = null; } } else { if (_enPassantPawnBlack != null) { _enPassantPawnBlack.EnPassant = false; _enPassantPawnBlack = null; } } if ((move.PieceType == Type.Pawn) && (Math.Abs(move.StartCoordinate.Y - move.TargetCoordinate.Y) == 2)) { if (move.PieceColor == Color.White) { _enPassantPawnWhite = (Pawn)piece; _enPassantPawnWhite.EnPassant = true; } else { _enPassantPawnBlack = (Pawn)piece; _enPassantPawnBlack.EnPassant = true; } } //Number of moves since last capture if (Board.PieceAt(move.TargetCoordinate) == null) { _container.HalfMoveSinceLastCapture++; } else { _container.HalfMoveSinceLastCapture = 0; } _conversation.Execute(command); _moves.Add(command); return(true); } return(false); }