Simple cellular automata doesn't seem right

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
leNoah
Prole
Posts: 14
Joined: Mon Feb 08, 2016 5:00 pm

Simple cellular automata doesn't seem right

Post by leNoah »

Hello,
I was experimenting with cellular automata and read some stuff here, here, and a bunch of other sites. Currently, my createMap() function looks like this:

Code: Select all

function createMap(width, height, iterations)
  -- empty table
  map = {}
  for h = 1, height do
    -- create another empty table inside each table address
    map[h] = {}
    -- in the second table , 45% chance is land.
    for w = 1, width do
      if love.math.random(1, 100) <= 45 then
        map[h][w] = 3
      else
        map[h][w] = 1
      end
    end
    
    for i = 1, iterations do
      for h, value1 in pairs(map) do
        for w, value2 in pairs(map[h]) do
          neighbours = 0
          
          -- check left and right
          neighbours = neighbours + check(map[h][w - 1])
          neighbours = neighbours + check(map[h][w + 1])
          
          -- if it's the top row all the above count as land
          if h == 1 then
            neighbours = neighbours + 3
          else
            -- check above row
            neighbours = neighbours + check(map[h - 1][w - 1])
            neighbours = neighbours + check(map[h - 1][w])
            neighbours = neighbours + check(map[h - 1][w + 1])
          end
          -- if it's the bottom row sim.
          if h == table.getn(map) then
            neighbours = neighbours + 3
          else
            -- check below row
            neighbours = neighbours + check(map[h + 1][w - 1])
            neighbours = neighbours + check(map[h + 1][w])
            neighbours = neighbours + check(map[h + 1][w + 1])
          end
          
          if neighbours > 5 then
            map[h][w] = 3
          elseif neighbours < 3 then
            map[h][w] = 1
          end
        end
      end
    end
  end
  
And, when iterations is set to anything except 0, it outputs something near-identical to the screenshot I attached at the bottom.

Not sure what I'm doing wrong, whether it's some dumb typo (I did check) or something else, but any help would be appreciated.
Thanks in advance,
Noah ^^
Attachments
love2d-cellular-automata.png
love2d-cellular-automata.png (8.26 KiB) Viewed 2359 times
--Home is where the heart is. Home is the ribcage.--
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Simple cellular automata doesn't seem right

Post by airstruck »

Try doing it in two passes. In the first pass, store the next value for each cell, but don't update the actual value yet. In the second pass, update the actual value from the "next value" you stored on the previous pass.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests