<commit_before>//
//  Position_legalMoves.swift
//  kjchess
//
//  Copyright © 2017 <NAME>. All rights reserved.
//

extension Position {

    /// Generate sequence of legal moves for this `Position`.
    ///
    /// This method is `mutating` because it might temporarily
    /// apply a move to check whether the king is left in check.
    public mutating func legalMoves() -> [Move] {
        guard let kingLocation = board.kingLocation(player: toMove) else {
            return possibleMoves()
        }

        let isInCheck = isAttacked(location: kingLocation, by: toMove.opponent)

        return possibleMoves().filter {
            isLegal(move: $0, kingLocation: kingLocation, isInCheck: isInCheck)
        }
    }

    /// Generate array of possible moves for this `Position`.
    ///
    /// The generated moves will all be valid in the sense that
    /// the piece can perform the move/capture. However, this
    /// method does not verify that the move will not leave
    /// the player's king in check or that it doesn't result
    /// in a repeated board position.
    private func possibleMoves() -> [Move] {
        let piecesLocations = board.pieces(player: toMove)<commit_msg>Construct legalMoves result using append rather that using filter<commit_after>//
//  Position_legalMoves.swift
//  kjchess
//
//  Copyright © 2017 <NAME>. All rights reserved.
//

extension Position {

    /// Generate sequence of legal moves for this `Position`.
    ///
    /// This method is `mutating` because it might temporarily
    /// apply a move to check whether the king is left in check.
    public mutating func legalMoves() -> [Move] {
        let possibles = possibleMoves()

        guard let kingLocation = board.kingLocation(player: toMove) else {
            return possibles
        }

        let isInCheck = isAttacked(location: kingLocation, by: toMove.opponent)

        var result = [Move]()
        result.reserveCapacity(possibles.count)
        for move in possibles {
            if isLegal(move: move, kingLocation: kingLocation, isInCheck: isInCheck) {
                result.append(move)
            }
        }

        return result
    }

    /// Generate array of possible moves for this `Position`.
    ///
    /// The generated moves will all be valid in the sense that
    /// the piece can perform the move/capture. However, this
    /// method does not verify that the move will not leave
    /// the player's king in check or that it doesn't result
    /// in a repeated board position.
    private func possibleMoves() -> [Move] {
        let piecesLocations = board.pieces(player: toMove)
{'fraction_non_alphanumeric': 0.09471281856219095, 'fraction_numerical': 0.003423354887790034, 'mean_word_length': 2.3207070707070705, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5916505', 'n_tokens_mistral': 709, 'n_tokens_neox': 676, 'n_words': 274}