Conway's Game of Life Glimmer Sample
Tags: ruby, gui, dsl, desktop
Published: 2021-08-02 18:58:32

[Glimmer DSL for SWT](https://github.com/AndyObtiva/glimmer-dsl-swt) 4.20.13.11 just shipped with an implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life). A simpler version was [blogged about many years ago](https://andymaleh.blogspot.com/2009/11/conways-game-of-life-glimmer-edition.html), but it has been enhanced to be canvas-based instead of button-based.

The models [Grid](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/samples/elaborate/game_of_life/model/grid.rb) and [Cell](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/samples/elaborate/game_of_life/model/cell.rb) were written test first.

Otherwise, the GUI simply binds to the grid cells (the same way the [Tic Tac Toe elaborate sample](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/docs/reference/GLIMMER_SAMPLES.md#tic-tac-toe) was implemented)

```ruby
# From: https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/docs/reference/GLIMMER_SAMPLES.md#game-of-life

require 'glimmer-dsl-swt'

require_relative 'game_of_life/model/grid'

class GameOfLife
  include Glimmer::UI::CustomShell
  
  WIDTH = 20
  HEIGHT = 20
  CELL_WIDTH = 25
  CELL_HEIGHT = 25
  
  before_body do
    @grid = GameOfLife::Model::Grid.new(WIDTH, HEIGHT)
  end
  
  body {
    shell {
      row_layout :vertical
      text "Conway's Game of Life (Glimmer Edition)"
    
      canvas {
        layout_data {
          width WIDTH*CELL_WIDTH
          height HEIGHT*CELL_HEIGHT
        }
        (0...HEIGHT).each do |row_index|
          (0...WIDTH).each do |column_index|
            rectangle(column_index*CELL_WIDTH, row_index*CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT) {
              background <= [@grid.cell_rows[row_index][column_index], "alive", on_read: ->(a) {a ? :black : :white}]
              
              on_mouse_down {
                @grid.cell_rows[row_index][column_index].toggle_aliveness!
              }
            }
          end
        end
      }
        
      composite {
        row_layout(:horizontal) {
          margin_width 0
        }
                
        button {
          text 'Step'
          enabled <= [@grid, :playing, on_read: :! ]
          
          on_widget_selected {
            @grid.step!
          }
        }
        
        button {
          text 'Clear'
          enabled <= [@grid, :playing, on_read: :! ]
          
          on_widget_selected {
            @grid.clear!
          }
        }
                
        button {
          text <= [@grid, :playing, on_read: ->(p) { p ? 'Stop' : 'Play' }]
          
          on_widget_selected {
            @grid.toggle_playback!
          }
        }
  
        label {
          text 'Slower'
        }
              
        scale {
          minimum 1
          maximum 100
          selection <=> [@grid, :speed]
        }
          
        label {
          text 'Faster'
        }
          
      }
    }
  }
end

GameOfLife.launch
```

Animated Screenshot:

![Game of Life](https://raw.githubusercontent.com/AndyObtiva/glimmer-dsl-swt/master/images/glimmer-game-of-life.gif)

[Glimmer DSL for SWT](https://github.com/AndyObtiva/glimmer-dsl-swt) enables writing desktop applications in Ruby (JRuby) in a fraction of the time they normally take in other languages (think hours and days for an MVP instead of weeks and months)

Happy [Glimmering](https://github.com/AndyObtiva/glimmer-dsl-swt)!