Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="game"></canas>  
              
            
!

CSS

              
                #game{
  margin: 100px;
  border: 1px solid #000;
  width: 500px;  
  height: 500px;
}
              
            
!

JS

              
                # Format for cell and @world:
class GameOfLife
  # default settings
  cell_size: 10 # in px,
  roll_num: 50 # num of cell
  column_num: 50 # num of cell
  draw_rate: 100 # in milliseconds
  born_prob: 0.1 # probability for a life to born
  alive_color: '#999'
  dead_color: '#eee'
  grid_color: '#ccc'
  
  create: () ->
    @world = @create_world()
    @loop()
    return
  
  # ------- Initiate The World ------- #
  
  # callback function to do something on each cell
  travel: (callback) ->
    for row in [0...@roll_num]
      for column in [0...@column_num]
        callback.call this, row: row, column: column
  
  create_world: ->
    @draw_world()
    # for every cell, decide that cell being alive depends on chance of born_prob
    @travel (cell) =>
      cell.status = (Math.random() < @born_prob)
      cell
  
  draw_world: ->
    @canvas = document.getElementById 'game'
    @canvas.width = @cell_size * @column_num
    @canvas.height = @cell_size * @roll_num
    @canvas_context = @canvas.getContext '2d'
  
  # ------- Methods to Create Life ------- #
  
  draw: (cell) ->
    coords = [cell.row * @cell_size, cell.column * @cell_size, @cell_size, @cell_size]
    @canvas_context.strokeStyle = @grid_color #to ad colors
    @canvas_context.strokeRect.apply @canvas_context, coords
    @canvas_context.fillStyle = if cell.status then @dead_color else @dead_color
    @canvas_context.fillRect.apply @canvas_context, coords 
    @canvas_context.fillStyle = if cell.status then @alive_color else @dead_color
    @canvas_context.beginPath()
    @canvas_context.arc(cell.row * @cell_size, cell.column * @cell_size, @cell_size/4, 0, 2 * Math.PI, false)
    @canvas_context.closePath();
    @canvas_context.fill()
 
  # game loop forever
  loop: =>  
    @world = @travel (cell) =>
      cell = @world[cell.row][cell.column]
      @draw(cell)
      @dead_or_alive(cell)
    setTimeout(@loop, @draw_rate)
  
  # ------- Logic and Patterns of Life ------- #
  
  # make decision for next step, return the whole cell
  dead_or_alive: (cell) ->
    neighbor = @count_neighbor(cell)
    cell = @review_status(cell, neighbor)
    cell
  
  count_neighbor: (cell) ->
    neighbor = 0
    for row in [-1..1]
      for column in [-1..1]
        neighbor_row = cell.row + row
        neighbor_column = cell.column + column
        # (row or column) means except itself
        ++neighbor if (row or column) and @isAlive(neighbor_row, neighbor_column)
    neighbor
  
  # check if that cell is alive (true/false)
  isAlive: (row, column) ->
    # check if coordinates is inside the boundary
    if @world[row] isnt undefined and @world[row][column] isnt undefined
      # check the current status of the neighbor
      result = @world[row][column].status
    else result = false # if coordinates is undefined
    result
  
  # game rules
  review_status: (cell, neighbor) ->
    # input cell and num of neighbor, return the whole cell
    
    # if cell is alive
    #   if neighbor < 2 then die <-- under-population!
    #   if neighbor is 2 or 3 then alive
    #   if neighbor > 3 then die <-- overcrowding!
    # if cell is dead and neighbor is 3 then live <-- reproduction!
    
    # copy the cell object first
    cell = { row: cell.row, column: cell.column, status: cell.status }
    if neighbor < 2 or neighbor > 3 then cell.status = false
    else if neighbor is 3 then cell.status = true
    # all others remain the same status
    cell
  

# implement GOL
gol = new GameOfLife
gol.create()
              
            
!
999px

Console