World Conquest Chronicles

World Conquest Chronicles

lualife, v1.5

The library that implements Conway's Game of Life.

Adding the placed field model that stores an offset and extending of possibilities of the simple field model.

Change Log

  • models:
    • field:
      • supporting of checking if an other field fits inside;
    • placed field:
      • extends the field model;
      • storing a field offset;
      • working with cells taking into account the field offset;
      • supporting of checking if an other field with offset fits inside;
      • supporting of copying the existing field with setting an offset.

Features

  • models:
    • size:
      • supporting of checking if an other size with offset fits inside;
    • point:
      • supporting of translation;
      • supporting of scaling;
      • supporting of textual representation;
    • field:
      • storing only of set cells;
      • supporting of counting of set cells;
      • supporting of checking if a cell is set;
      • supporting of checking if an other field fits inside;
      • supporting of mapping;
    • placed field:
      • extends the field model;
      • storing a field offset;
      • working with cells taking into account the field offset;
      • supporting of checking if an other field with offset fits inside;
      • supporting of copying the existing field with setting an offset;
  • generating of a random field:
    • customizable filling factor;
    • limiting by a cell count:
      • lower limit;
      • upper limit;
  • operations with fields as with sets:
    • union of fields:
      • supporting an offset for the second operand;
      • restricting of the result size by the size of the first operand;
    • complement of fields:
      • supporting an offset for the second operand;
    • intersection of fields:
      • supporting an offset for the second operand;
  • operations with fields as with matrices:
    • rotating of a field clockwise;
  • populating of a field according to Conway's Game of Life rules:
    • using of the naive algorithm with iterating and copying of a whole field.

Examples

lualife.sets:

local Size = require("lualife.models.size")
local Point = require("lualife.models.point")
local PlacedField = require("lualife.models.placed_field")
local sets = require("lualife.sets")

local function print_field(field)
  assert(field:isInstanceOf(PlacedField))

  field:map(function(point, contains)
    io.write(contains and "O" or ".")

    if point.x - field.offset.x == field.size.width - 1 then
      io.write("\n")
    end
  end)

  io.write("\n")
end

local glider = PlacedField:new(Size:new(3, 3), Point:new(2, 2))
glider:set(Point:new(3, 2))
glider:set(Point:new(4, 3))
glider:set(Point:new(2, 4))
glider:set(Point:new(3, 4))
glider:set(Point:new(4, 4))

local blinker = PlacedField:new(Size:new(3, 3), Point:new(1, 2))
blinker:set(Point:new(2, 2))
blinker:set(Point:new(2, 3))
blinker:set(Point:new(2, 4))

local unioned_field = sets.union(glider, blinker)
print_field(unioned_field)

local complemented_field = sets.complement(glider, blinker)
print_field(complemented_field)

local intersected_field = sets.intersection(glider, blinker)
print_field(intersected_field)

Repository

Link: https://github.com/thewizardplusplus/lualife/tree/v1.5.

Content: code.

License: MIT.