private void EngineMessage(string message) { if (message.Contains(UciCommands.bestmove)) // Message is in the form: bestmove <move> ponder <move> { if (!message.Contains("ponder")) { CheckMate = true; } var move = message.Split(' ').ElementAt(1); var position1 = move.Take(2); var position2 = move.Skip(2); var enginePiece = BoardItems.OfType <ChessPiece>(). Where(p => p.Rank == int.Parse(position1.ElementAt(1).ToString()) & p.File == position1.ElementAt(0)).Single(); if (enginePiece.Color == PlayerColor) // Player made illegal move { RemoveLastMove(); ResetSomeMembers(); } else { if (playerWantsToMovePiece) { ctxTaskFactory.StartNew(() => Chess.MovePiece(selectedPiece, targetSquare, BoardItems)).Wait(); DeeperMoveAnalysis(); } else if (playerWantsToCapturePiece) { ctxTaskFactory.StartNew(() => Chess.CapturePiece(selectedPiece, targetPiece, BoardItems)).Wait(); DeeperMoveAnalysis(); } else // Engine move { moves.Append(" " + move); var pieceToCapture = BoardItems.OfType <ChessPiece>(). Where(p => p.Rank == int.Parse(position2.ElementAt(1).ToString()) & p.File == position2.ElementAt(0)).SingleOrDefault(); if (pieceToCapture != null) { ctxTaskFactory.StartNew(() => Chess.CapturePiece(enginePiece, pieceToCapture, BoardItems)).Wait(); } else { targetSquare = BoardItems.OfType <BoardSquare>(). Where(s => s.Rank == int.Parse(position2.ElementAt(1).ToString()) & s.File == position2.ElementAt(0)).SingleOrDefault(); ctxTaskFactory.StartNew(() => Chess.MovePiece(enginePiece, targetSquare, BoardItems)).Wait(); } IsEngineThinking = false; } } } }