<commit_before>"use strict";

const _ = require("lodash");

function isValidMove(gameState, proposedMove) {
    const proposedSpace = proposedMove.currentPosition + proposedMove.numberOfSpaces * (gameState.isPlayerOne ? 1 : -1);
    const isPlayerOne = gameState.isPlayerOne;
    const proposedBoardSpace = gameState.board[proposedSpace];
    const currentBoardSpace = gameState.board[proposedMove.currentPosition];
     
    const activePlayer = isPlayerOne ? gameState.playerOne : gameState.playerTwo;
    
    function canMoveOffBoard() {
        if (isPlayerOne && gameState.playerOne.initialPieces === 0 && gameState.playerOne.barPieces === 0) {
            return _(17).range().every(num => gameState.board[num].isPlayerOne !== isPlayerOne);
        } else if (gameState.playerTwo.initialPieces === 0 && gameState.playerTwo.barPieces === 0){
            return _(24).range().drop(6).every(num => gameState.board[num].isPlayerOne !== isPlayerOne);
        }
        
        return false;
    }
    
    const currentPieceDoesNotExist = proposedMove.currentPosition >= 0 && proposedMove.currentPosition <= 23 && currentBoardSpace.isPlayerOne !== isPlayerOne;
    if (currentPieceDoesNotExist) {
        return false;
    }
    
    // checks to ensure that if there are pieces on the bar that the player is not trying to move a different piece
    if (activePlayer.barPieces && proposedMove.isBar === false) {
        return false;
    }
    
    // checks whether the player is trying to move pieces off the board and if that is allowed
    if (isPlayerOne && proposedSpace > 23) {
        if (canMoveOffBoard()) {
            return true;
        } else {
            return false;
        }
    } else if (!isPlayerOne && proposedSpace < 0) {
        if (canMoveOffBoard()) {
            return true;
        } else {
            return false;
        }
    }
    
    // checks whether the space is already occupied by the current player, is empty, or is occupied by only 1 piece of the opposing player
    if (isPlayerOne === proposedBoardSpace.isPlayerOne || proposedBoardSpace.isPlayerOne === null || (proposedBoardSpace.isPlayerOne !== isPlayerOne && proposedBoardSpace.numPieces === 1)) {
        return true;
    }
    
    return false;
}

module.exports = isValidMove;<commit_msg>Refactor isValidMove<commit_after>"use strict";

const _ = require("lodash");

function isValidMove(gameState, proposedMove) {
    const proposedSpace = proposedMove.currentPosition + proposedMove.numberOfSpaces * (gameState.isPlayerOne ? 1 : -1);
    const isPlayerOne = gameState.isPlayerOne;
    const proposedBoardSpace = gameState.board[proposedSpace];
    const currentBoardSpace = gameState.board[proposedMove.currentPosition];
     
    const activePlayer = isPlayerOne ? gameState.playerOne : gameState.playerTwo;
    
    function canMoveOffBoard() {
        if (isPlayerOne && gameState.playerOne.initialPieces === 0 && gameState.playerOne.barPieces === 0) {
            return _(17).range().every(num => gameState.board[num].isPlayerOne !== isPlayerOne);
        } else if (gameState.playerTwo.initialPieces === 0 && gameState.playerTwo.barPieces === 0){
            return _(24).range().drop(6).every(num => gameState.board[num].isPlayerOne !== isPlayerOne);
        }
        
        return false;
    }
    
    const currentPieceDoesNotExist = proposedMove.currentPosition >= 0 && proposedMove.currentPosition <= 23 && currentBoardSpace.isPlayerOne !== isPlayerOne;
    if (currentPieceDoesNotExist) {
        return false;
    }
    
    // checks to ensure that if there are pieces on the bar that the player is not trying to move a different piece
    if (activePlayer.barPieces && proposedMove.isBar === false) {
        return false;
    }
    
    // checks whether the player is trying to move pieces off the board and if that is allowed
    if ((isPlayerOne && proposedSpace > 23) || (!isPlayerOne && proposedSpace < 0)) {
        return canMoveOffBoard();
    }
    
    // checks whether the space is already occupied by the current player, is empty, or is occupied by only 1 piece of the opposing player
    if (isPlayerOne === proposedBoardSpace.isPlayerOne || proposedBoardSpace.isPlayerOne === null || (proposedBoardSpace.isPlayerOne !== isPlayerOne && proposedBoardSpace.numPieces === 1)) {
        return true;
    }
    
    return false;
}

module.exports = isValidMove;
{'fraction_non_alphanumeric': 0.10381644706951386, 'fraction_numerical': 0.008632439800090867, 'mean_word_length': 3.103448275862069, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4898152', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1208, 'n_words': 328}