<commit_before>﻿using CAESAR.Chess.Core;
using CAESAR.Chess.Moves;
using CAESAR.Chess.Pieces;
using CAESAR.Chess.Players;
using System.Linq;

namespace CAESAR.Chess.Positions
{
    public static class PositionExtensions
    {
        public static bool IsInCheck(this IPosition position, Side side)
        {
            var opposingSide = side == Side.White ? Side.Black : Side.White;
            var opponentMoves =
                position.Board.Squares.Where(
                    square => square.HasPiece && square.Piece.Side == opposingSide && square.Piece.PieceType != PieceType.King)
                    .Select(square => square.Piece)
                    .SelectMany(piece => piece.Moves);

            // If king can be captured by opponent, current side is in check
            return opponentMoves.FirstOrDefault(x =>
            {
                var normalMove = x as NormalMove;
                if (normalMove == null)
                    return false;
                var square = x.Position.Board.GetSquare(normalMove.DestinationSquareName);
                if (square == null)
                    return false;
                return square.HasPiece && square.Piece.PieceType == PieceType.King;
            }) != null;
        }
    }
}<commit_msg>Added comments<commit_after>﻿using System.Linq;
using CAESAR.Chess.Core;
using CAESAR.Chess.Moves;
using CAESAR.Chess.Pieces;

namespace CAESAR.Chess.Positions
{
    /// <summary>
    ///     Useful extensions for an <seealso cref="IPosition" />.
    /// </summary>
    public static class PositionExtensions
    {
        /// <summary>
        ///     Calculates whether a given <seealso cref="Side" /> is in check, in a given <seealso cref="IPosition" />.
        /// </summary>
        /// <param name="position">The <seealso cref="IPosition" /> in which to calculate.</param>
        /// <param name="side">The <seealso cref="Side" /> for which to calculate.</param>
        /// <returns>
        ///     True if the given <seealso cref="side" /> is in check in the given <seealso cref="position" />, false
        ///     otherwise.
        /// </returns>
        public static bool IsInCheck(this IPosition position, Side side)
        {
            var opposingSide = side == Side.White ? Side.Black : Side.White;
            var opponentMoves =
                position.Board.Squares.Where(
                    square =>
                        square.HasPiece && square.Piece.Side == opposingSide && square.Piece.PieceType != PieceType.King)
                    .Select(square => square.Piece)
                    .SelectMany(piece => piece.Moves);

            // If king can be captured by opponent, current side is in check
            return opponentMoves.FirstOrDefault(x =>
            {
                var normalMove = x as NormalMove;
                if (normalMove == null)
                    return false;
                var square = x.Position.Board.GetSquare(normalMove.DestinationSquareName);
                if (square == null)
                    return false;
                return square.HasPiece && square.Piece.PieceType == PieceType.King;
            }) != null;
        }
    }
}
{'fraction_non_alphanumeric': 0.10789556464296948, 'fraction_numerical': 0.0, 'mean_word_length': 1.7508650519031141, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4146020', 'n_tokens_mistral': 875, 'n_tokens_neox': 842, 'n_words': 217}