lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Saturday 21 September 2002 21:02, Dean wrote:
> How would you have written this in Lua?  

local write = io.write

function drawRow(row)
  for i = 1, table.getn(row) do
    write( row[i] and 'O' or '.' )
  end
  write'\n'
end

local push = table.insert

function applyRuleOnRow(rule, row)
  local new_row = { false }
  for i = 2, table.getn(row)-1 do
    local think_binary = 1 + (row[i-1] and 4 or 0)
                           + (row[i]   and 2 or 0)
                           + (row[i+1] and 1 or 0)
    push(new_row, rule[think_binary])
  end
  push(new_row, false)
  return new_row
end

local RULE_100     = { false, true, true, true, false, true, true, false }
local STARTING_ROW = { n=75, [73]=true }

if arg then
  local row = STARTING_ROW
  for i = 1, 70 do
    drawRow(row)
    row = applyRuleOnRow(RULE_100, row)
  end
end

don't need tuples after all. ;-)

- Peter