{"size": 1345, "ext": "js", "max_stars_repo_path": "src/classicTransitionRule.js", "max_stars_repo_name": "fibo/games-of-life", "max_stars_repo_stars_event_min_datetime": "2015-05-31T01:14:32.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-18T17:38:56.000Z", "max_issues_repo_path": "src/classicTransitionRule.js", "max_issues_repo_name": "fibo/games-of-life", "max_issues_repo_issues_event_min_datetime": "2016-07-10T10:29:56.000Z", "max_issues_repo_issues_event_max_datetime": "2020-01-06T12:52:30.000Z", "max_forks_repo_path": "src/classicTransitionRule.js", "max_forks_repo_name": "fibo/games-of-life", "max_forks_repo_forks_event_min_datetime": "2016-07-06T08:12:08.000Z", "max_forks_repo_forks_event_max_datetime": "2019-10-25T14:30:08.000Z"}
{"max_stars_count": 26, "max_issues_count": 3, "max_forks_count": 6, "avg_line_length": 26.9, "max_line_length": 96, "alphanum_fraction": 0.682527881}
var countAliveNeighbours = require('./countAliveNeighbours')

/**
 * Implements the followinf rules.
 *
 * 1. Any live cell with fewer than `a` live neighbours dies, as if caused by under-population.
 * 2. Any live cell with `a` or `b` live neighbours lives on to the next generation.
 * 3. Any live cell with more than `b` live neighbours dies, as if by overcrowding.
 * 4. Any dead cell with exactly `c` live neighbours becomes a live cell, as if by reproduction.
 *
 * where in the classic *Game of Life*,
 *
 * ```
 * a = 2
 * b = 3
 * c = 3
 * ```
 *
 * @param {Number} a under population threshold
 * @param {Number} b over crowding threshold
 * @param {Number} c reproduction value
 * @param {Function} getNeighboursOf
 * @param {Function} isAlive
 *
 * @returns {Function} nextStatusOf (cell)
 */

function classicTransitionRule (a, b, c, getNeighboursOf, isAlive) {
  var countAliveNeighboursOf = countAliveNeighbours.bind(null, getNeighboursOf, isAlive)

  /**
   * @param {*} cell
   * @returns {Boolean} status
   */

  function nextStatusOf (cell) {
    var numNeighboursAlive = countAliveNeighboursOf(cell)

    if (isAlive(cell)) {
      return ((numNeighboursAlive >= a) && (numNeighboursAlive <= b))
    } else {
      return (numNeighboursAlive === c)
    }
  }

  return nextStatusOf
}

module.exports = classicTransitionRule
