I made Conway's Game of Life in JavaScript and was hoping someone could give me some pointers regarding my logic of checking adjacent cells. I know there must be a better way, but at the same time, it works.
JS:
var board = [["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],
["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]];
//boolean to tell if cell is alive
var alive = new Boolean(null);
//counter will be used to see how many cells are alive
var counter;
//number of initial alive cells
var number = Math.floor((Math.random()*900)+5);
//coordinates for alive cells
var xcor;
var ycor;
//make table and assign ID to all cells
var idAssign;
document.write("<table border='1px>'");
for (var x = 0; x < 30; x++ ) {
document.write("<tr>");
for (var y = 0; y < 30; y ++) {
idAssign = x.toString() + y.toString();
document.write("<td id='" + idAssign + "'>oo</td>");
}
document.write("</tr>");
}
document.write("</table>");
//generate coordinates for the cells that are alive
for (var i=0; i < number; i++) {
xcor = Math.floor((Math.random()*29)+0);
ycor = Math.floor((Math.random()*29)+0);
board[xcor][ycor] = "x";
}
function run(){
//loop to check all cells
for (var x=0; x != 30; x++) {
for (var y=29; y != -1; y--) {
//reset counter and boolean after every iteration
counter = 0;
alive = null;
//evaluation of cells
var check;
check = x.toString() + y.toString();
//check current cell
if(board[x][y] == "x") {
alive = true;
}
else{
alive = false;
}
//BOUNDS
//Handle left bound
if (y == 0) {
if (board[x ][(y + 1)] == "x"){
counter ++;
}
//Left bottom corner
if (x == 29) {
if (board[(x - 1)][y] == "x") {
counter ++;
}
if (board[(x - 1)][(y + 1)] == "x") {
counter ++;
}
}
//Left top corner
else if (x == 0) {
if (board[(x + 1)][y] == "x") {
counter ++;
}
if (board[(x + 1)][(y + 1)] == "x") {
counter ++;
}
}
else{
if (board[(x - 1)][y] == "x") {
counter ++;
}
if (board[(x - 1)][(y + 1)] == "x") {
counter ++;
}
if (board[x][(y + 1)] == "x") {
counter ++;
}
if (board[(x + 1)][y] == "x") {
counter ++;
}
if (board[(x + 1)][(y + 1)] == "x") {
counter ++;
}
}
}
//handle right bound
else if (y == 29) {
if (board[(x)][(y - 1)] == "x"){
counter ++;
}
//right bottom corner
if (x == 29) {
if (board[(x - 1)][y] == "x") {
counter ++;
}
if (board[(x - 1)][(y - 1)] == "x") {
counter ++;
}
}
//right top corner
else if (x == 0) {
if (board[x + 1][y] == "x") {
counter ++;
}
if (board[(x + 1)][(y - 1)] == "x") {
counter ++;
}
}
else{
if (board[(x - 1)][y] == "x") {
counter ++;
}
if (board[(x - 1)][(y - 1)] == "x") {
counter ++;
}
if (board[x][(y - 1)] == "x") {
counter ++;
}
if (board[(x + 1)][y] == "x") {
counter ++;
}
if (board[(x + 1)][(x - 1)] == "x") {
counter ++;
}
}
}
else{
//Top bounds
if (x == 0) {
if (board[x][(y + 1)] == "x") {
counter ++;
}
if (board[x][(y - 1)] == "x") {
counter ++;
}
if (board[(x + 1)][y] == "x") {
counter ++;
}
if (board[(x + 1)][y + 1] == "x") {
counter ++;
}
if (board[(x + 1)][y - 1] == "x") {
counter ++;
}
}
//Bottom bounds
else if (x == 29) {
if (board[x][(y + 1)] == "x") {
counter ++;
}
if (board[x][(y - 1)] == "x") {
counter ++;
}
if (board[(x - 1)][y] == "x") {
counter ++;
}
if (board[(x - 1)][(y + 1)] == "x") {
counter ++;
}
if (board[(x - 1)][(y - 1)] == "x") {
counter ++;
}
}
else{
//center of board
if (board[x][(y + 1)] == "x") {
counter ++;
}
if (board[x][(y - 1)] == "x") {
counter ++;
}
if (board[(x - 1)][y] == "x") {
counter ++;
}
if (board[(x - 1)][(y + 1)] == "x") {
counter ++;
}
if (board[(x - 1)][(y - 1)] == "x") {
counter ++;
}
if (board[(x + 1)][(y + 1)] == "x") {
counter ++;
}
if (board[(x + 1)][(y - 1)] == "x") {
counter ++;
}
}
} //end board checking
//apply rules to cell
if (alive == true) {
if (counter < 2) {
board[x][y] = "";
}
if (counter > 3) {
board[x][y] = "";
}
}
else if (alive == false) {
if (counter == 3) {
board[x][y] = "x";
}
else if (counter != 3){
board[x][y] = "";
}
}
}//Inner FOR
}// Outer FOR
var IDcheck;
for (var x= 0; x < 30; x ++) {
for (var y = 0; y <30; y ++) {
IDcheck = x.toString() + y.toString();
if (board[x][y] == "x" ) {
document.getElementById(IDcheck).className = 'active';
}
else{
document.getElementById(IDcheck).className = 'NotActive';
}
}
}
}//WHILE
HTML:
<html>
<head>
<!-- Author: Justin Murphy >
<--- Start Date: November 27, 2013 >
<--- File: Display.html >
<--- Supporting Files: Life.js -->
<title>Conway's Game Of Life</title>
<link rel='stylesheet' type='text/css' href='style.css'>
<script src='Life.js'></script>
</head>
<body onload='setInterval(function(){run();},100)'>
</body>
</html>
The reason I have all spaces set to the array manually is because, instead of randomly generating the starting plots for the bacteria, one could comment out that code and place them manually to see that sort of patterns they can make.
function(){run();}
in yoursetInterval
call can be more succinctly writtenrun
- sincerun
is already a function taking no arguments, there's no reason I can think of to wrap it in an anonymous function with no arguments. Also, remember thatvar
only scopes to the current function (including code written above thevar
declaration), not current{}
pair; sofor (var x=0; x != 30; x++)
gives a misleading impression of the scope ofx
. \$\endgroup\$