/// <summary> /// Checks if <paramref name="player"/> is currently under check. /// </summary> /// <typeparam name="TChess">Derived type. <paramref name="chess"/> /// must be <typeparamref name="TChess"/>.</typeparam> /// <param name="chess">A <see cref="Chess"/> board.</param> /// <param name="player"><see langword="true"/> for player with white pieces. Otherwise, black.</param> /// <returns><see langword="true"/> if <paramref name="player"/> is checked. /// Otherwise, <see langword="false"/>.</returns> public static bool IsChecked <TChess>(this IChess chess, bool player) where TChess : Chess { // Types of movements that might threaten a king. // A pawn may promote while threatening. var threateningMoves = new MoveType[] { MoveType.Capture, MoveType.PromoteToKnight, MoveType.PromoteToBishop, MoveType.PromoteToRook, MoveType.PromoteToQueen }.ToList(); // List of opponent's moves that might threaten the king. var enemyMoves = ((TChess)chess) .AllMoves(!player) .Where(m => threateningMoves.Contains(m.Type)) .Where(m => chess.Position.ContainsKey(m.ToSquare)) .Where(m => chess.Position[m.ToSquare] is King).ToList(); // Case enemy moves is an empty list, no piece threatens the king. return(enemyMoves.Count > 0); }