Skip to content

Instantly share code, notes, and snippets.

@blvrd
Created August 1, 2016 04:34
Show Gist options
  • Save blvrd/72d86069ec1634b0037a7f3b2de025db to your computer and use it in GitHub Desktop.
Save blvrd/72d86069ec1634b0037a7f3b2de025db to your computer and use it in GitHub Desktop.
Game of Life starting point
var gameOfLife = {
width: 12,
height: 12,
createAndShowBoard: function () {
// create <table> element
var goltable = document.createElement("tbody");
// build Table HTML
var tablehtml = '';
for (var h=0; h<this.height; h++) {
tablehtml += "<tr id='row+" + h + "'>";
for (var w=0; w<this.width; w++) {
tablehtml += "<td data-status='dead' id='" + w + "-" + h + "'></td>";
}
tablehtml += "</tr>";
}
goltable.innerHTML = tablehtml;
// add table to the #board element
var board = document.getElementById('board');
board.appendChild(goltable);
// once html elements are added to the page, attach events to them
this.setupBoardEvents();
},
forEachCell: function (iteratorFunc) {
/*
Write forEachCell here. You will have to visit
each cell on the board, call the "iteratorFunc" function,
and pass into func, the cell and the cell's x & y
coordinates. For example: iteratorFunc(cell, x, y)
*/
},
setupBoardEvents: function() {
// each board cell has an CSS id in the format of: "x-y"
// where x is the x-coordinate and y the y-coordinate
// use this fact to loop through all the ids and assign
// them "on-click" events that allow a user to click on
// cells to setup the initial state of the game
// before clicking "Step" or "Auto-Play"
// clicking on a cell should toggle the cell between "alive" & "dead"
// for ex: an "alive" cell be colored "blue", a dead cell could stay white
// EXAMPLE FOR ONE CELL
// Here is how we would catch a click event on just the 0-0 cell
// You need to add the click event on EVERY cell on the board
var onCellClick = function (e) {
// QUESTION TO ASK YOURSELF: What is "this" equal to here?
// how to set the style of the cell when it's clicked
if (e.target.dataset.status === 'dead') {
this.setCellAlive(e.target)
} else {
this.setCellDead(e.target)
}
};
var tbody = document.getElementsByTagName('tbody')[0];
tbody.onclick = onCellClick.bind(this);
},
setCellAlive: function(cell) {
cell.className = 'alive'
cell.dataset.status = 'alive'
cell.dataset.nextStatus = null
},
setCellDead: function(cell) {
cell.className = 'dead'
cell.dataset.status = 'dead'
cell.dataset.nextStatus = null
},
getCoordinates: function(cell) {
var coordinates = {}
var coordinateArrary = cell.id.split('-')
coordinates.x = parseInt(coordinateArrary[0])
coordinates.y = parseInt(coordinateArrary[1])
return coordinates
},
step: function () {
// Here is where you want to loop through all the cells
// on the board and determine, based on it's neighbors,
// whether the cell should be dead or alive in the next
// evolution of the game.
//
// You need to:
// 1. Count alive neighbors for all cells
// 2. Set the next state of all cells based on their alive neighbors
},
enableAutoPlay: function () {
// Start Auto-Play by running the 'step' function
// automatically repeatedly every fixed time interval
}
};
gameOfLife.createAndShowBoard();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment