<commit_before>var cellCreator = require('./cell.js');
var interval;
var ticksPerSecond = 60;
var generationNumber = 0;
var maxRow = 0;
var maxCol = 0;
var globalCurrentEnvironment = {};
var globalLiveCells = {};

var killGame = function(){
	clearInterval(interval);
};

var initializeGame = function (initialLiveCells, maxRows, maxCols){
	if(!maxRows){
		maxRow = 29;
	}
	if(!maxCol){
		maxCol = 49;
	}
	globalLiveCells = initialLiveCells;
}

var runGame = function(io){
	var liveCells = globalLiveCells;
	var currentEnvironment = globalCurrentEnvironment;
	countNeighbors(liveCells, currentEnvironment);
	var game = function(){
		debugger;
		var nextGeneration = nextGen(currentEnvironment, liveCells);
		io.sockets.emit('nextGen', ++generationNumber, nextGeneration.cellsBorn, nextGeneration.cellsDied);
		console.log('generation: #%d', generationNumber);
		currentEnvironment = nextGeneration.nextEnvironment;
		globalCurrentEnvironment = currentEnvironment;
		globalLiveCells = nextGeneration.liveCells;
	};
	interval = setInterval(game, 3000/*1000/ticksPerSecond*/);
};

var currentLiveCells = function(){
	return globalLiveCells;
};

var getGenerationNumber = function(){
	return generationNumber;
};

var resetGame = function(){
	console.log('reseting generation number')
	generationNumber = 0;
	globalLiveCells = {};
	globalCurrentEnvironment = {};
}

var changeGridSize = function(rows, cols){
	maxRow = rows - 1;
	maxCol = cols - 1;
}

var nextGen = function(currentEnvironment, liveCells){
	//debugger;
	var cellsBorn = cellsDied = {};
	var nextEnvironment = currentEnvironment;
	for(cell in currentEnvironment){
		//debugger;
		if(currentEnvironment[cell].live && (currentEnvironment[cell].neighbors < 2 || currentEnvironment[cell].neighbors > 3)){
			killCell(currentEnvironment[cell], nextEnvironment, liveCells, cellsDied);
		}
		else if(!currentEnvironment[cell].live && currentEnvironment[cell].neighbors === 3){
			cellBorn(currentEnvironment[cell], nextEnvironment, liveCells, cellsBorn);
		}
	}
	return {
		'nextEnvironment':nextEnvironment,
		'cellsBorn':cellsBorn,
		'cellsDied':cellsDied, 
		'liveCells':liveCells
	}
};

var killCell = function(cell, environment, liveCells, cellsDied){
	cell.live = false;
	delete liveCells['cell' + cell.id];
	cellsDied['cell' + cell.id] = cell;
	updateNeighborCount(cell, environment, liveCells, -1);
};

var cellBorn = function(cell, environment, liveCells, cellsBorn){
	cell.live = true;
	liveCells['cell' + cell.id] = cell;
	cellsBorn['cell' + cell.id] = cell;
	updateNeighborCount(cell, environment, liveCells, 1);
};

var countNeighbors = function(liveCells, currentEnvironment){
	for(cell in liveCells){
		updateNeighborCount(liveCells[cell], currentEnvironment, liveCells, 1);
	}
};

var updateNeighborCount = function(cell, environment, liveCells, change){
	var row = cell.row;
	var col = cell.col;
	var newID;
	var isAlive;
	//exclude out of bounds cells
	if(row < 0 || col < 0 || row > maxRow || col > maxCol){
		return;
	}
	//top left neighbor
	if(row > 0 && col > 0){
		isAlive = false;
		newID = (row - 1) + '-' + (col - 1);
		if(liveCells['cell' + newID]){
			liveCells['cell' + newID].neighbors+= change;
			isAlive = true;
		}
		if(!environment['cell' + newID]){
			environment['cell' + newID] = cellCreator.createCell(newID, row - 1, col - 1, isAlive, 0); //{'id': newID, 'row': row - 1, 'col': col - 1, live: isAlive, 'neighbors': 0};
		}
		environment['cell' + newID].neighbors+= change;
	}
	//top neighbor
	if(row > 0){<commit_msg>fixing stuff that didnt let it work<commit_after>var cellCreator = require('./cell.js');
var _ = require('lodash');
var interval;
var ticksPerSecond = 10;
var generationNumber = 0;
var maxRow = 0;
var maxCol = 0;
var globalCurrentEnvironment = {};
var globalLiveCells = {};

var killGame = function(){
	clearInterval(interval);
};

var initializeGame = function (initialLiveCells, maxRows, maxCols){
	if(!maxRows){
		maxRow = 29;
	}
	if(!maxCol){
		maxCol = 49;
	}
	globalLiveCells = initialLiveCells;
}

var runGame = function(io){
	var game = function(liveCells){
		currentEnvironment = {};
		countNeighbors(liveCells, currentEnvironment);
		var nextGeneration = nextGen(currentEnvironment, liveCells);
		io.sockets.emit('nextGen', ++generationNumber, nextGeneration.cellsBorn, nextGeneration.cellsDied);
		console.log('generation: #%d', generationNumber);
		globalLiveCells = nextGeneration.liveCells;
	};
	interval = setInterval(game, 1000/ticksPerSecond,  _.clone(globalLiveCells, true));
};

var currentLiveCells = function(){
	return globalLiveCells;
};

var getGenerationNumber = function(){
	return generationNumber;
};

var resetGame = function(){
	console.log('reseting generation number')
	generationNumber = 0;
	globalLiveCells = {};
	globalCurrentEnvironment = {};
}

var changeGridSize = function(rows, cols){
	maxRow = rows - 1;
	maxCol = cols - 1;
}

var nextGen = function(currentEnvironment, liveCells){
	var cellsBorn = {};
	var cellsDied = {};
	var nextEnvironment = _.clone(currentEnvironment, true);

	for(cell in currentEnvironment){
		if(currentEnvironment[cell].live && (currentEnvironment[cell].neighbors < 2 || currentEnvironment[cell].neighbors > 3)){
			killCell(nextEnvironment[cell], nextEnvironment, liveCells, cellsDied);
		}
		else if(!currentEnvironment[cell].live && currentEnvironment[cell].neighbors === 3){
			cellBorn(nextEnvironment[cell], nextEnvironment, liveCells, cellsBorn);
		}
	}
	return {
		'nextEnvironment':nextEnvironment,
		'cellsBorn':cellsBorn,
		'cellsDied':cellsDied, 
		'liveCells':liveCells
	}
};

var killCell = function(cell, environment, liveCells, cellsDied){
	cell.live = false;
	delete liveCells['cell' + cell.id];
	cellsDied['cell' + cell.id] = cell;
	updateNeighborCount(cell, environment, liveCells, -1);
};

var cellBorn = function(cell, environment, liveCells, cellsBorn){
	cell.live = true;
	liveCells['cell' + cell.id] = cell;
	cellsBorn['cell' + cell.id] = cell;
	updateNeighborCount(cell, environment, liveCells, 1);
};

var countNeighbors = function(liveCells, environment){
	for(cell in liveCells){
		updateNeighborCount(liveCells[cell], environment, liveCells, 1);
	}
};

var updateNeighborCount = function(cell, environment, liveCells, change){
	var row = cell.row;
	var col = cell.col;
	var newID;
	var isAlive;

	//exclude out of bounds cells
	if(row < 0 || col < 0 || row > maxRow || col > maxCol){
		return;
	}
	//top left neighbor
	if(row > 0 && col > 0){
		isAlive = false;
		newID = (row - 1) + '-' + (col - 1);
		if(liveCells['cell' + newID]){
			liveCells['cell' + newID].neighbors += change;
			isAlive = true;
		}
		if(!environment['cell' + newID]){
			environment['cell' + newID] = cellCreator.createCell(newID, row - 1, col - 1, isAlive, 0); //{'id': newID, 'row': row - 1, 'col': col - 1, live: isAlive, 'neighbors': 0};
		}
		environment['cell' + newID].neighbors+= change;
	}
	//top neighbor
	if(row > 0){
{'fraction_non_alphanumeric': 0.14440746635798002, 'fraction_numerical': 0.010707567645782087, 'mean_word_length': 5.13854351687389, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '6365269', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2234, 'n_words': 537}