<commit_before>const helpers = require('./gameHelpers.js');

module.exports.getMove = function (game) {
  let updatedGame = Object.assign({}, game);
  let moves = this.getAllNextStates(updatedGame);
  let move = this.chooseNextMove(moves);
  if (move === null) {
    move = this.getNaiveMove(updatedGame.board);
  }
  updatedGame.move = move;
  helpers.render(`Hmm, then I choose ${(updatedGame.move)+1}`);
  let board = helpers.newBoard(updatedGame.board, updatedGame.move, updatedGame.player);
  updatedGame.board = board;
  return updatedGame;
}

module.exports.getAllNextStates = function (game) {
  let currentPlayer = game.player;
  let opponent = helpers.togglePlayer(currentPlayer);
  let currentBoard = Object.assign([], game.board);
  let availablePositions = this.getAvailableMoves(currentBoard);

  return availablePositions.reduce(function(nextStates, position) {
    let newState = {};
    newState.position = position;
    newState.boardCP = helpers.newBoard(currentBoard, position, currentPlayer);
    newState.boardOP = helpers.newBoard(currentBoard, position, opponent);
    newState.winnerCP = helpers.checkTerminal(newState.boardCP, currentPlayer);
    newState.winnerOP = helpers.checkTerminal(newState.boardOP, opponent);
    nextStates.push(newState);
    return nextStates;
  }, []);
}

module.exports.getAvailableMoves = function (board) {
  return board.reduce(function (moves, position, i) {
    if (position === 'e')
    moves.push(i);
    return moves;
  }, []);
}

module.exports.chooseNextMove = function (moves) {
  let win = null;
  let preventLoss = null;
  moves.forEach(function(move) {
    if (move.winnerCP === 1)
      win = move.position;
    else if (move.winnerOP === 1)
      preventLoss = move.position;
  });
  if (win !== null)
    return win;
  else if (preventLoss !== null)
    return preventLoss;
  else
    return null;
}

module.exports.getNaiveMove = function (board) {
  let move;
  if (board[4] === 'e')
    move = 4;
  else if (board[0] === 'e')
    move = 0;
  else if (board[2] === 'e')
    move = 2;
  else if (board[6] === 'e')
    move = 6;
  else if (board[8] === 'e')
    move = 8;
  else
    move = board.indexOf('e');
  return move;
}<commit_msg>Adds further if/else to aiplayer getNaiveMove<commit_after>const helpers = require('./gameHelpers.js');

module.exports.getMove = function (game) {
  let updatedGame = Object.assign({}, game);
  let moves = this.getAllNextStates(updatedGame);
  let move = this.chooseNextMove(moves);
  if (move === null) {
    move = this.getNaiveMove(updatedGame.board);
  }
  updatedGame.move = move;
  helpers.render(`Hmm, then I choose ${(updatedGame.move)+1}`);
  let board = helpers.newBoard(updatedGame.board, updatedGame.move, updatedGame.player);
  updatedGame.board = board;
  return updatedGame;
}

module.exports.getAllNextStates = function (game) {
  let currentPlayer = game.player;
  let opponent = helpers.togglePlayer(currentPlayer);
  let currentBoard = Object.assign([], game.board);
  let availablePositions = this.getAvailableMoves(currentBoard);

  return availablePositions.reduce(function(nextStates, position) {
    let newState = {};
    newState.position = position;
    newState.boardCP = helpers.newBoard(currentBoard, position, currentPlayer);
    newState.boardOP = helpers.newBoard(currentBoard, position, opponent);
    newState.winnerCP = helpers.checkTerminal(newState.boardCP, currentPlayer);
    newState.winnerOP = helpers.checkTerminal(newState.boardOP, opponent);
    nextStates.push(newState);
    return nextStates;
  }, []);
}

module.exports.getAvailableMoves = function (board) {
  return board.reduce(function (moves, position, i) {
    if (position === 'e')
    moves.push(i);
    return moves;
  }, []);
}

module.exports.chooseNextMove = function (moves) {
  let win = null;
  let preventLoss = null;
  moves.forEach(function(move) {
    if (move.winnerCP === 1)
      win = move.position;
    else if (move.winnerOP === 1)
      preventLoss = move.position;
  });
  if (win !== null)
    return win;
  else if (preventLoss !== null)
    return preventLoss;
  else
    return null;
}

module.exports.getNaiveMove = function (board) {
  let move;
  if (board[4] === 'e')
    move = 4;
  else if (board[0] === 'X' && board[8] === 'X' || board[2] === 'X' && board[6] === 'X')
    move = 1 || 3 || 5 || 7;
  else if (board[0] === 'e')
    move = 0;
  else if (board[8] === 'e')
    move = 8;
  else if (board[2] === 'e')
    move = 2;
  else if (board[6] === 'e')
    move = 6;
  else
    move = board.indexOf('e');
  return move;
}
{'fraction_non_alphanumeric': 0.1493875765529309, 'fraction_numerical': 0.0074365704286964126, 'mean_word_length': 3.7985309548793285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3692842', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1511, 'n_words': 350}