Commit e05159ff authored by kovacsdavid's avatar kovacsdavid

Add example

parent a30e2848
/*
* Game of life implementáció
*
* Kovács Dávid
* Valamikor 2016 környéke
*/
$(document).ready(function () {
var GameOfLife = function () {
this.fieldControl = [];
this.cellsLocation = [];
this.currentField = [];
this.x = 0;
this.y = 0;
this.round;
this.tableSize = 17;
this.population = 5;
};
GameOfLife.prototype.init = function () {
this.initTable();
this.initFieldControl();
this.initCellsLocation();
this.initCurrentField();
this.initCells();
};
/* creating the table */
GameOfLife.prototype.initTable = function () {
var table = document.getElementById("tablegof");
var html = "";
var counter = 1;
for (var i = 0; i < this.tableSize; i++) {
html += '<tr>';
for (var j = 0; j < this.tableSize; j++) {
html += '<td id="' + counter + '"></td>';
counter += 1;
}
html += "</tr>";
}
table.innerHTML = html;
};
/* boolean array to identify the cells */
GameOfLife.prototype.initCellsLocation = function () {
for (var i = 0; i < this.tableSize; i++) {
this.cellsLocation[i] = new Array();
for (var j = 0; j < this.tableSize; j++) {
this.cellsLocation[i].push(false);
}
}
};
/* snapshot of the field at the begining of the round */
GameOfLife.prototype.initCurrentField = function () {
for (var i = 0; i < this.tableSize; i++) {
this.currentField[i] = new Array();
for (var j = 0; j < this.tableSize; j++) {
this.currentField[i].push(false);
}
}
};
/* an array of the tablecells for the visualization */
GameOfLife.prototype.initFieldControl = function () {
var counter = 1;
for (var i = 0; i < this.tableSize; i++) {
this.fieldControl[i] = new Array();
for (var j = 0; j < this.tableSize; j++) {
this.fieldControl[i].push(document.getElementById(counter));
counter++;
}
}
};
/* method for colorize the field
*
* @param | x | x coordinate | int
* @param | y | y coordiante | int
*/
GameOfLife.prototype.colorizeField = function (x, y) {
this.fieldControl[y][x].style.background = "#3669a2";
};
/*
* this method register a dead cell
*
* @param | x | x coordinate | int
* @param | y | y coordiante | int
*/
GameOfLife.prototype.registerDeadCell = function (x, y) {
this.fieldControl[y][x].style.background = "#fff";
this.cellsLocation[y][x] = false;
};
GameOfLife.prototype.amIACell = function (x, y, decision) {
if (decision === true) {
this.colorizeField(x, y);
}
this.cellsLocation[y][x] = decision;
};
GameOfLife.prototype.initCells = function () {
for (var i = 0; i < this.tableSize; i++) {
for (var j = 0; j < this.tableSize; j++) {
this.amIACell(i, j, false);
}
}
//Pentadecathlon
//top
this.amIACell(3, 5, true);
this.amIACell(4, 5, true);
this.amIACell(4, 4, true);
this.amIACell(4, 6, true);
this.amIACell(5, 5, true);
this.amIACell(5, 4, true);
this.amIACell(5, 3, true);
this.amIACell(5, 6, true);
this.amIACell(5, 7, true);
//bottom
this.amIACell(12, 7, true);
this.amIACell(12, 6, true);
this.amIACell(12, 5, true);
this.amIACell(12, 4, true);
this.amIACell(12, 3, true);
this.amIACell(13, 4, true);
this.amIACell(13, 5, true);
this.amIACell(13, 6, true);
this.amIACell(14, 5, true);
};
GameOfLife.prototype.isMyBuddy = function (x, y) {
if (typeof this.currentField[y] !== "undefined" &&
typeof this.currentField[y][x] !== "undefined" &&
this.currentField[y][x]) {
return true;
}
return false;
};
GameOfLife.prototype.countMyBuddies = function (x, y) {
var buddies = 0;
if (this.isMyBuddy(x - 1, y)) {
buddies += 1;
}
if (this.isMyBuddy(x + 1, y)) {
buddies += 1;
}
if (this.isMyBuddy(x, y + 1)) {
buddies += 1;
}
if (this.isMyBuddy(x, y - 1)) {
buddies += 1;
}
if (this.isMyBuddy(x + 1, y + 1)) {
buddies += 1;
}
if (this.isMyBuddy(x - 1, y + 1)) {
buddies += 1;
}
if (this.isMyBuddy(x + 1, y - 1)) {
buddies += 1;
}
if (this.isMyBuddy(x - 1, y - 1)) {
buddies += 1;
}
return buddies;
};
GameOfLife.prototype.amIDead = function (x, y) {
if (this.countMyBuddies(x, y) < 2 || this.countMyBuddies(x, y) > 3) {
return true;
}
return false;
};
GameOfLife.prototype.startRound = function () {
var stopper = 4000;
round = setInterval(function () {
for (var i = 0; i < GoF.cellsLocation.length; i++) {
for (var j = 0; j < GoF.cellsLocation[i].length; j++) {
GoF.currentField[i][j] = GoF.cellsLocation[i][j];
}
}
for (var i = 0; i < GoF.tableSize; i++) {
for (var j = 0; j < GoF.tableSize; j++) {
if (GoF.amIDead(j, i)) {
GoF.registerDeadCell(j, i);
}
if (GoF.countMyBuddies(j, i) === 3) {
GoF.amIACell(j, i, true);
}
}
}
stopper--;
if (stopper == 0) {
clearInterval(round);
$("#gofbtn").prop("disabled", false);
$("#gofbtn").html("Újra!");
}
}, 500);
};
var GoF = new GameOfLife();
GoF.init();
GoF.startRound();
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment