How to resolve the algorithm Conway's Game of Life step by step in the Ol programming language

Published on 12 May 2024 09:40 PM
#Ol

How to resolve the algorithm Conway's Game of Life step by step in the Ol programming language

Table of Contents

Problem Statement

The Game of Life is a   cellular automaton   devised by the British mathematician   John Horton Conway   in 1970.   It is the best-known example of a cellular automaton. Conway's game of life is described   here: A cell   C   is represented by a   1   when alive,   or   0   when dead,   in an   m-by-m   (or m×m)   square array of cells. We calculate   N   - the sum of live cells in C's   eight-location neighbourhood,   then cell   C   is alive or dead in the next generation based on the following table: Assume cells beyond the boundary are always dead. The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players.   One interacts with the Game of Life by creating an initial configuration and observing how it evolves.

Although you should test your implementation on more complex examples such as the   glider   in a larger universe,   show the action of the blinker   (three adjoining cells in a row all alive),   over three generations, in a 3 by 3 grid.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Conway's Game of Life step by step in the Ol programming language

Source code in the ol programming language

#!/usr/bin/ol
(import (otus random!))

(define MAX 65536)  ; should be power of two
; size of game board (should be less than MAX)
(define WIDTH 170)
(define HEIGHT 96)

; helper function
(define (hash x y)
   (let ((x (mod (+ x WIDTH) WIDTH))
         (y (mod (+ y HEIGHT) HEIGHT)))
   (+ (* y MAX) x)))

; helper function
(define neighbors '(
   (-1 . -1) ( 0 . -1) ( 1 . -1)
   (-1 .  0)           ( 1 .  0)
   (-1 .  1) ( 0 .  1) ( 1 .  1)
))

; dead-or-alive cell test
(define (alive gen x y)
   (case (fold (lambda (f xy)
                     (+ f (get gen (hash (+ x (car xy)) (+ y (cdr xy))) 0)))
               0 neighbors)
      (2
         (get gen (hash x y) #false))
      (3
         #true)))

; ---------------
(import (lib gl2))
(gl:set-window-title "Convey's The game of Life")

(glShadeModel GL_SMOOTH)
(glClearColor 0.11 0.11 0.11 1)
(glOrtho 0 WIDTH 0 HEIGHT 0 1)

(glPointSize (/ 854 WIDTH))

; generate random field
(gl:set-userdata
   (list->ff (map (lambda (i) (let ((x (rand! WIDTH)) (y (rand! HEIGHT)))
                                 (cons (hash x y) 1))) (iota 2000))))
; main game loop
(gl:set-renderer (lambda (mouse)
(let ((generation (gl:get-userdata)))
   (glClear GL_COLOR_BUFFER_BIT)

   ; draw the cells
   (glColor3f 0.2 0.5 0.2)
   (glBegin GL_POINTS)
      (ff-fold (lambda (st key value)
         (glVertex2f (mod key MAX)
                     (div key MAX))
      ) #f generation)
   (glEnd)

   (gl:set-userdata
      ; next cells generation
      (ff-fold (lambda (st key value)
         (let ((x (mod key MAX))
               (y (div key MAX)))
            (fold (lambda (st key)
                     (let ((x (+ x (car key)))
                           (y (+ y (cdr key))))
                        (if (alive generation x y) (put st (hash x y) 1) st)))
               (if (alive generation x y) (put st (hash x y) 1) st) ; the cell
               neighbors)))
         #empty generation)))))


  

You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the Java programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Ruby programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the D programming language
You may also check:How to resolve the algorithm Repunit primes step by step in the Go programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the MATLAB programming language