Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 26a21ca6a7433c0b37bb2627cb05596c5c9d4b6b @EnriqueVidal committed Jun 12, 2011
Showing with 233 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +9 −0 css/canvas.css
  3. +16 −0 index.html
  4. +55 −0 js/main.js
  5. +22 −0 php/calculator.php
  6. +14 −0 php/cell.php
  7. +113 −0 php/cellgrid.php
4 .gitignore
@@ -0,0 +1,4 @@
+*.swp
+*~
+.DS_Store
+
9 css/canvas.css
@@ -0,0 +1,9 @@
+#canvas tr td {
+ border: solid 1px #333;
+ width: 15px;
+ height: 15px;
+}
+
+.selected-cell {
+ background-color: #e58f21;
+}
16 index.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>life</title>
+ <link rel="stylesheet" type="text/css" href="css/canvas.css" />
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+ <script type="text/javascript" src="js/main.js"></script>
+ </head>
+ <body>
+ <table id="canvas" style="boder: 1px solid #333;">
+ </table>
+
+ <button id="send-cells">Step</button>
+ <input type="checkbox" id="auto">Auto</button>
+ </body>
+</html>
55 js/main.js
@@ -0,0 +1,55 @@
+$(function(){
+ // Create the canvas
+ for (i=0; i<20; i++)
+ {
+ $("#canvas").append("<tr></tr>")
+ for (j=0; j<20; j++)
+ {
+ $("tr:last").append('<td class="cell" id="[' + i + ',' + j +']" selected="false"></td>');
+ }
+ }
+
+ $('.cell').live('click', function() {
+ if ($(this).attr("selected") == "false")
+ {
+ $(this).attr("selected", true)
+ $(this).addClass('selected-cell');
+ return false;
+ }
+
+ $(this).attr("selected", false)
+ $(this).removeClass('selected-cell');
+ });
+
+ $('#send-cells').click(function(){
+ cells = new Array();
+ $(".selected-cell").each(function(value) {
+ cells[value] = this.id;
+ });
+
+ $.getJSON("php/calculator.php", { cells: cells, width: 20, height: 20 }, function(data){
+
+ $(".selected-cell").each(function(){
+ $(this).removeClass('selected-cell');
+ $(this).attr('selected', false);
+ });
+
+ $(data.surviving_cells).each(function(){
+ arr = String(this);
+
+ // for some reason passin "#" + arr to the $() jQuery method didn't work
+ cell = $(document)[0].getElementById(arr);
+ $(cell).addClass('selected-cell');
+ $(cell).attr('selected', true);
+ });
+
+ if ($("#auto:checked").length == 1)
+ $.doTimeout(3000, $("#send-cells").click().delay(3000));
+ });
+ });
+ });
+
+function kill_or_born_cells()
+{
+
+}
22 php/calculator.php
@@ -0,0 +1,22 @@
+<?php
+ require_once 'cellgrid.php';
+
+ $grid = new CellGrid($_GET['width'], $_GET['height']);
+ $cells_sent = $_GET['cells'];
+
+ // cells come in a [x,y] format, we break them into an array and remove the brackets
+
+ for ($i = 0; $i < count($cells_sent); $i++)
+ {
+ $coordinates = explode(",", $cells_sent[$i]);
+ $row = str_replace("[", "", $coordinates[0]);
+ $column = str_replace("]", "", $coordinates[1]);
+
+ $grid->living_cells(array($row, $column));
+ }
+
+ $cells = $grid->step();
+ echo json_encode($cells);
+
+ //echo "\n" . print_r($cells["surviving_cells"], true);
+?>
14 php/cell.php
@@ -0,0 +1,14 @@
+<?php
+ class Cell
+ {
+ private $column;
+ private $row;
+ private $neigbour;
+
+ function __construct($column, $row)
+ {
+ $this->column = $column;
+ $this->row = $row;
+ }
+ }
+?>
113 php/cellgrid.php
@@ -0,0 +1,113 @@
+<?php
+ class CellGrid
+ {
+ private $width;
+ private $height;
+ private $grid;
+ private $next_grid;
+ private $living_cells = array();
+
+ public function __construct($width, $height)
+ {
+ // set a widht and height to know where the edge of the grid is
+ $this->width = $width;
+ $this->height = $height;
+
+ // Setup new empty grid
+ for ($i = 0; $i < $height; $i++)
+ for ($j = 0; $j < $width; $j++)
+ $this->grid[$i][$j] = 0;
+
+ $this->next_grid = $this->grid;
+ }
+
+ public function step()
+ {
+ $surviving_cells = array();
+
+ for ($column = 0; $column < $this->height; $column++)
+ for ($row = 0; $row < $this->width; $row++)
+ {
+ if ($this->grid[$column][$row] == 0 && $this->neighbours($row, $column) == 3)
+ {
+ $this->next_grid[$column][$row] = 1;
+ $surviving_cells[count($surviving_cells)] = "[" . $column. ",". $row ."]";
+ }
+
+ if ($this->grid[$column][$row] == 1 && ( $this->neighbours($row, $column) == 2 || $this->neighbours($row, $column) == 3))
+ {
+ $this->next_grid[$column][$row] = 1;
+ $surviving_cells[count($surviving_cells)] = "[" . $column. ",". $row ."]";
+ }
+ }
+
+ return array("new_grid" => $this->next_grid, "surviving_cells" => $surviving_cells);
+ }
+
+ public function living_cells($coordinates = null)
+ {
+ if ( gettype($coordinates) == "array" && count($coordinates) == 2)
+ {
+ $next_element = count($this->living_cells);
+
+ // We save our living cells coordinates and update the status of the grid
+ $this->living_cells[$next_element] = $coordinates;
+ $this->grid[$coordinates[0]][$coordinates[1]] = 1;
+ }
+ return $this->living_cells;
+ }
+
+ public function get_neighbour_count()
+ {
+ $cell = 0;
+
+ foreach($this->living_cells as $living_cell)
+ {
+ $count[$cell] = $this->neighbours($living_cell[0], $living_cell[1]);
+ $cell++;
+ }
+
+ return $count;
+ }
+
+ // We count each cell's neighbours
+ private function neighbours($row, $column)
+ {
+ $count = 0;
+
+ if ( $row > 0 && $row > 0 )
+ if ( $this->grid[$column - 1][$row - 1] == 1)
+ $count++;
+
+ if ($column > 0)
+ if ($this->grid[$column - 1][$row] == 1)
+ $count++;
+
+ if ( $row < $this->width && $column > 0 )
+ if ( $this->grid[$column - 1][$row + 1] == 1 )
+ $count++;
+
+ if ($row > 0)
+ if ($this->grid[$column][$row -1] == 1)
+ $count++;
+
+ if ($row < $this->width)
+ if ($this->grid[$column][$row + 1] == 1)
+ $count++;
+
+ if ($row > 0 && $column < $this->height)
+ if ( $this->grid[$column + 1][$row - 1] == 1)
+ $count++;
+
+ if ($column < 19)
+ if ($this->grid[$column + 1][$row] == 1)
+ $count++;
+
+ if ($row < 19 && $column < $this->height)
+ if ($this->grid[$column + 1][$row + 1] == 1)
+ $count++;
+
+ return $count;
+ }
+ }
+?>

0 comments on commit 26a21ca

Please sign in to comment.
Something went wrong with that request. Please try again.