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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Conway's Game of Life step by step in the Lua 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 Lua programming language

Source code in the lua programming language

local function T2D(w,h) local t={} for y=1,h do t[y]={} for x=1,w do t[y][x]=0 end end return t end

local Life = {
  new = function(self,w,h)
    return setmetatable({ w=w, h=h, gen=1, curr=T2D(w,h), next=T2D(w,h)}, {__index=self})
  end,
  set = function(self, coords)
    for i = 1, #coords, 2 do
      self.curr[coords[i+1]][coords[i]] = 1
    end
  end,
  evolve = function(self)
    local curr, next = self.curr, self.next
    local ym1, y, yp1 = self.h-1, self.h, 1
    for i = 1, self.h do
      local xm1, x, xp1 = self.w-1, self.w, 1
      for j = 1, self.w do
        local sum = curr[ym1][xm1] + curr[ym1][x] + curr[ym1][xp1] +
                    curr[y][xm1] + curr[y][xp1] +
                    curr[yp1][xm1] + curr[yp1][x] + curr[yp1][xp1]
        next[y][x] = ((sum==2) and curr[y][x]) or ((sum==3) and 1) or 0
        xm1, x, xp1 = x, xp1, xp1+1
      end
      ym1, y, yp1 = y, yp1, yp1+1
    end
    self.curr, self.next, self.gen = self.next, self.curr, self.gen+1
  end,
  render = function(self)
    print("Generation "..self.gen..":")
    for y = 1, self.h do
      for x = 1, self.w do
        io.write(self.curr[y][x]==0 and "□ " or "■ ")
      end
      print()
    end
  end
}


print("GLIDER:")
local life = Life:new(5,5)
life:set({ 2,1, 3,2, 1,3, 2,3, 3,3 })
for i = 1, 5 do
  life:render()
  life:evolve()
end
for i = 6,20 do life:evolve() end
life:render()

print()

print("LWSS:")
life = Life:new(10,7)
life:set({ 2,2, 5,2, 6,3, 2,4, 6,4, 3,5, 4,5, 5,5, 6,5 })
for i = 1, 5 do
  life:render()
  life:evolve()
end
for i = 6,20 do life:evolve() end
life:render()


  

You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the SimpleCode programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Fusc sequence step by step in the Processing programming language