Sven Hendriks avatar Sven Hendriks committed 839dbd3

Initial import.

Comments (0)

Files changed (2)

+jQuery conway plugin
+====================
+
+Author and license
+------------------
+
+Copyright (c) 2011, Sven Hendriks (hendriks.sven@googlemail.com)
+Licensed under the GPL Version 2 license.
+
+Overview
+--------
+
+This piece of code implements Conway's Game of Life as
+a jQuery plugin.
+
+It supports separate layers of activity, each one simulating
+its own conway universe. Different layers use different colors.
+The colors of overlapping active cells from different layers 
+are mixed for your viewing pleasure.
+
+Dependencies
+------------
+
+This code relies on the jQuery xcolor plugin. This can be found
+here: http://www.xarg.org/project/jquery-color-plugin-xcolor/
+
+Usage
+-----
+
+1. You need to define a div element like so:
+
+<div id="letsrock"></div>
+
+2. Then call the conway plugin:
+
+<script type="text/javascript">
+    $(document).ready(function() {
+        $('#letsrock').conway({
+            rows: 8,
+            cols: 16
+        });
+        $('#letsrock').conway('start');
+    });
+</script>
+
+/**
+ * jQuery conway plugin
+ * Copyright (c) 2011, Sven Hendriks (hendriks.sven@googlemail.com)
+ * Licensed under the GPL Version 2 license.
+ *
+ * This piece of code implements Conway's Game of Life as
+ * a jQuery plugin.
+ *
+ * See the README file for more information about what this
+ * plugin is about and how to use it.
+ *
+ * Dependencies
+ * ------------
+ *
+ * This code relies on the jQuery xcolor plugin. This can be found
+ * here: http://www.xarg.org/project/jquery-color-plugin-xcolor/
+ *
+ * @author Sven Hendriks
+ * @version 0.0.1b
+ *
+ * @see http://projects.sven-hendriks.de/conway
+ **/
+
+(function($) {
+    var grid = [];
+    var conf = {};
+    var stopped = false;
+    var colors = ['#006000', '#600000', '#000060'];
+    var layers = 3;
+
+    randomfill = function() {
+        var i = 0, j = 0;
+        for(i = 0; i < conf.rows; i += 1) {
+            for(j = 0; j < conf.cols; j += 1) {
+                grid[i][j] = Math.round(Math.random());
+            }
+        }            
+    };
+
+    print = function() {
+        var i = 0, j = 0;
+        for(i = 0; i < conf.rows; i += 1) {
+            for(j = 0; j < conf.cols; j += 1) {
+                var color = '#101010';
+                for(bit = 0, op = 1; bit < layers; bit +=1, op <<= 1) {
+                    if((grid[i][j] & op) !== 0) {
+                        color = $.xcolor.combine(color, colors[bit]);
+                    }
+                }
+                $('#conway_cell_' + i + '_' + j).css({'background-color': color});
+            }
+        } 
+    };
+
+    is_dead = function(grid, old_grid) {
+        var i,j;
+        for(i = 0; i < conf.rows; i += 1) {
+            for(j = 0; j < conf.cols; j += 1) {
+                if(grid[i][j] !== old_grid[i][j]) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    };
+
+    has_how_many_neighbours = function(grid, i, j, how_many_bits) {
+        var k, l, bit, op;
+        var sums = [];
+
+        // prepare array for return values
+        for(k = 0; k < how_many_bits; k += 1) {
+            sums.push(0);
+        }
+
+        for(k = i-1; k <= i+1; k += 1) {
+            for(l = j-1; l <= j+1; l += 1) {
+                if( ! (k===i && l===j)) {
+                //    continue;
+                //}
+                if(k < 0) {
+                    y = conf.rows - 1;
+                } else if(k >= conf.rows) {
+                    y = 0;
+                } else {
+                    y = k;
+                }
+                if(l < 0) {
+                    x = conf.cols - 1;
+                } else if(l >= conf.cols) {
+                    x = 0;
+                } else {
+                    x = l;
+                }
+
+                // loop over bits
+                for(bit = 0, op = 1; bit < how_many_bits; bit += 1, op <<= 1) {
+                    if((grid[y][x] & op) !== 0) {
+                        sums[bit] += 1;
+                    }
+                }
+                }
+            }
+        }
+        return sums;
+    };
+
+    step = function() {
+        var old_grid = $.extend(true, [], grid); // deep copy grid data
+        var i, j, bit, op;
+        for(i = 0; i < conf.rows; i += 1) {
+            for(j = 0; j < conf.cols; j += 1) {
+                nbs = has_how_many_neighbours(old_grid, i, j, layers);
+                for(bit = 0, op = 1; bit < layers; bit += 1, op <<= 1) {
+                    if((grid[i][j] & op) === 0) { 
+                        if (nbs[bit] === 3) {
+                            grid[i][j] |= op;
+                        }
+                    } else if(nbs[bit] < 2 || nbs[bit] > 3) {
+                        grid[i][j] &= (7 - op);
+                    }
+                }
+            }
+        }
+
+
+        if(is_dead(grid, old_grid)) {
+            randomfill();
+        }
+
+        print();
+
+        if( ! stopped) {
+            setTimeout(step, 200);
+        }
+    };
+
+    var methods = {
+        init : function(params) { 
+            $.extend(conf, params);
+            return $(this).each(function() {
+                // resize element
+                $(this).css({
+                    position: 'absolute',
+                    width: $(document).width(), 
+                    height: $(document).height(), 
+                    'z-index': -1000,
+                    top: 0,
+                    left: 0 
+                });
+
+                // empty element
+                $(this).empty();
+
+                // create table with rows x cols cells as well as internal datastructures
+                grid = [];
+
+                var table = '<table width="100%" height="100%">';
+                var i = 0;
+
+                for(i = 0; i < conf.rows; i += 1) {
+                    table += '<tr>';
+                    var grid_row = [];
+                    for(j = 0; j < conf.cols; j += 1) {
+                        table += '<td class="conway-cell" id="conway_cell_' + i + '_' + j + '">&nbsp;</td>';
+                        grid_row[j] = 0;
+                    }
+                    grid[i] = grid_row;
+                    table += '</tr>';
+                }
+                table += '</table>';
+        
+                $(this).html(table);
+
+                randomfill();
+
+                // init glider
+                grid[0][1] |= 2;
+                grid[1][2] |= 2;
+                grid[2][0] |= 2;
+                grid[2][1] |= 2;
+                grid[2][2] |= 2;
+
+                // init spaceship
+                grid[3][1] |= 4;
+                grid[3][2] |= 4;
+                grid[3][3] |= 4;
+                grid[3][4] |= 4;
+                grid[4][0] |= 4;
+                grid[4][4] |= 4;
+                grid[5][4] |= 4;
+                grid[6][0] |= 4;
+                grid[6][3] |= 4;
+
+                print();
+            });
+        },
+        stop: function() {
+            stopped = true;
+        },
+        start : function() {
+            stopped = false;
+            step();
+        }
+    };
+
+     $.fn.conway = function(method) {
+       if ( methods[method] ) {
+          return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
+        } else if ( typeof method === 'object' || ! method ) {
+          return methods.init.apply( this, arguments );
+        } else {
+          $.error( 'Method ' +  method + ' does not exist on jQuery.conway' );
+        }    
+    };
+})(jQuery);
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.