<commit_before>/*!
 * Forest Ecosystem
 * codenameyau.github.io
 *
 * Challenge Description:
 * http://redd.it/27h53e
 * http://codegolf.stackexchange.com/q/35322/30051
 */
'use strict';


/****************************
 * GridSimulation - Backend *
 ****************************/
function GridSimulation(settings) {
  this.initializeSettings(settings);
  this.initializeGrid();
  this.initializeEventHandlers();
}

GridSimulation.prototype.update = function() {
  console.log('Updating');
};

GridSimulation.prototype.pause = function() {
  this.settings.running = false;
  console.info('Paused');
};

GridSimulation.prototype.resume = function() {
  this.settings.running = true;
  console.info('Resuming');
};

GridSimulation.prototype.togglePause = function() {
  if (this.settings.running) { this.pause(); }
  else { this.resume(); }
};

GridSimulation.prototype.initializeSettings = function(settings) {
  this.settings = settings;
  this.checkProperty(this.settings, 'gridRows', 50);
  this.checkProperty(this.settings, 'gridCols', 50);
  this.checkProperty(this.settings, 'cellSize', 5);
  this.checkProperty(this.settings, 'running', true);
};

GridSimulation.prototype.initializeGrid = function() {
  var gridRows = this.settings.gridRows;
  var gridCols = this.settings.gridCols;
  this.grid = [];
  for (var i=0; i<gridCols; i++) {
    this.grid[i] = [];
  }
};

GridSimulation.prototype.initializeEventHandlers = function() {
  window.addEventListener('focus', this.resume.bind(this), false);
  window.addEventListener('blur', this.pause.bind(this), false);
  window.addEventListener('keydown', this._keyboardInputHandler.bind(this), false);
};

GridSimulation.prototype.checkProperty = function(object, property, value) {
  if (object && typeof object[property] === 'undefined') {
    object[property] = value;
  }
};

GridSimulation.prototype._keyboardInputHandler = function(event) {
  switch (event.which) {

  case 32: // spacebar
    this.togglePause();
    break;

  }
};


/******************************
 * GridCanvas - Visualization *
 ******************************/
function GridCanvas(gridObject) {
  this.grid = gridObject;
  this.initializeCanvas();
}

GridCanvas.prototype.initializeCanvas = function() {
  var settings = this.grid.settings;
  this.canvas = document.getElementById(settings.canvasID);
  this.canvas.width  = settings.gridCols * settings.cellSize;
  this.canvas.height = settings.gridRows * settings.cellSize;
  this.ctx = this.canvas.getContext('2d');
  this.ctx.fillStyle = 'rgb(200, 0, 0)';
  this.ctx.fillRect(0, 0, 50, 50);
};


/**************************
 * Program Initialization *
 **************************/
(function() {

  // Specify configuration
  var CONFIG = {
    gridRows: 50,
    gridCols: 50,
    cellSize: 5,
    canvasID: 'imagination',
  };

  // GridSimulation: handles the backend simulation
  // GridCanvas: visualizes the simulation with canvas
  var simulationGrid = new GridSimulation(CONFIG);
  var simulationCanvas = new GridCanvas(simulationGrid);
<commit_msg>Passing canvas into sumulation grid<commit_after>/*!
 * Forest Ecosystem
 * codenameyau.github.io
 *
 * Challenge Description:
 * http://redd.it/27h53e
 * http://codegolf.stackexchange.com/q/35322/30051
 */
'use strict';


/******************************
 * GridCanvas - Visualization *
 ******************************/
function GridCanvas(config) {
  this.initializeSettings(config);
  this.initializeCanvas();
}

GridCanvas.prototype.initializeSettings = function(settings) {
  this.settings = settings;
  this.checkProperty(this.settings, 'gridRows', 50);
  this.checkProperty(this.settings, 'gridCols', 50);
  this.checkProperty(this.settings, 'cellSize', 5);
  this.checkProperty(this.settings, 'delay', 500);
  this.checkProperty(this.settings, 'running', true);
};

GridCanvas.prototype.initializeCanvas = function() {
  this.canvas = document.getElementById(this.settings.canvasID);
  this.canvas.width  = this.settings.gridCols * this.settings.cellSize;
  this.canvas.height = this.settings.gridRows * this.settings.cellSize;
  this.ctx = this.canvas.getContext('2d');
};

GridCanvas.prototype.checkProperty = function(object, property, value) {
  if (object && typeof object[property] === 'undefined') {
    object[property] = value;
  }
};


/****************************
 * GridSimulation - Backend *
 ****************************/
function GridSimulation(canvas) {
  this.canvas = canvas;
  this.initializeGrid();
  this.initializeSimulation();
  this.initializeEventHandlers();
}

GridSimulation.prototype.update = function() {
  console.log('Updating');
};

GridSimulation.prototype.pause = function() {
  this.simulation.running = false;
  console.info('Paused');
};

GridSimulation.prototype.resume = function() {
  this.simulation.running = true;
  console.info('Resuming');
};

GridSimulation.prototype.togglePause = function() {
  if (this.simulation.running) { this.pause(); }
  else { this.resume(); }
};

GridSimulation.prototype.initializeGrid = function() {
  var gridRows = this.canvas.settings.gridRows;
  var gridCols = this.canvas.settings.gridCols;
  this.grid = [];
  for (var i=0; i<gridCols; i++) {
    this.grid[i] = [];
  }
};

GridSimulation.prototype.initializeSimulation = function() {
  this.simulation = {
    running: true,
    time: 0,
  };
};

GridSimulation.prototype.initializeEventHandlers = function() {
  window.addEventListener('focus', this.resume.bind(this), false);
  window.addEventListener('blur', this.pause.bind(this), false);
  window.addEventListener('keydown', this._keyboardInputHandler.bind(this), false);
};

GridSimulation.prototype._keyboardInputHandler = function(event) {
  switch (event.which) {

  case 32: // spacebar
    this.togglePause();
    break;

  }
};

GridSimulation.prototype.run = function() {
  console.info('Running');
};



/**************************
 * Program Initialization *
 **************************/
(function() {

  // Specify configuration
  var CONFIG = {
    gridRows: 50,
    gridCols: 50,
    cellSize: 10,
    delay: 1000,
    canvasID: 'imagination',
  };

  // GridCanvas: visualizes the simulation with canvas
  // GridSimulation: handles the backend simulation
  var simulationCanvas = new GridCanvas(CONFIG);
  var simulation = new GridSimulation(simulationCanvas);
  simulation.run();

{'fraction_non_alphanumeric': 0.19105562579013907, 'fraction_numerical': 0.012010113780025285, 'mean_word_length': 4.743194192377495, 'pattern_counts': {'":': 0, '<': 5, '<?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': '357721', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1899, 'n_words': 336}