Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Karasuro

OpenLife (Conway's Game of Life)

Recommended Posts

Well, it works! it has a few graphical glitches and is a little slow even with a maxed creative build but it works.
 
Requirements:
 
Touch compatible screen
384K+ RAM
Any graphics card will work.
Tier 2+ CPU or APU recommended.
 
Controls:
 
R = Reset Simulation..
Space = Pause/Play simulation.
Up/Down arrows = Increase/Decrease Speed.
Left/Right arrows = Increase/Decrease Grid Size.

Backspace = Exit Simulation.

 

WARNING!!

 

Due to graphical glitching, rapid screen flashing WILL occur. If you suffer from epilepsy, DO NOT RUN THIS PROGRAM.

Viewer Discretion is Advised.
 
Code:

--[[ OpenLife v0.1.0 by Karasuro]]
 
--[[ Imports ]]
local component = require 'component'
local gpu = component.gpu
local term = require 'term'
local event = require 'event'
local keyboard = require 'keyboard'
 
--[[***************************************
    DECLARATIONS
*******************************************]]
local WIDTH, HEIGHT = gpu.getResolution()
local size = 1
local pause = true
local minDelay = 0.015625
local maxDelay = 0.125
local delay = maxDelay
local timer = minDelay / 2
local cSize = 1
local screenTier = 1
local grid1 = {}
local myEventHandlers = setmetatable({}, {__index = function() return unknownEvent end})
 
 
--[[**********************************************
    INITIALIZATION
**************************************************]]
gpu.setBackground(0x000000)
gpu.setForeground(0xFFFFFF)
term.clear()
term.setCursorBlink(false)
term.setCursor(1, 1)
 
if (HEIGHT == 16) then
  screenTier = 1
elseif (HEIGHT == 25) then
  screenTier = 2
elseif (HEIGHT == 50) then
  screenTier = 3
end
 
--[[*********************************************************
    PROGRAM METHODS
*************************************************************]]
 
-- Unknown Event Handler
function unknownEvent()
  -- Do Nothing
end
 
-- Creating the Grid
function setupGrid()
  
  if (screenTier == 1) then -- 50, 16
    if (size <= 1) then
      cSize = 2
    elseif (size >= 2) then
      cSize = 1
    end
  elseif (screenTier == 2) then -- 80, 25
    if (size <= 1) then
      cSize = 5
    elseif (size >= 2) then
      cSize = 1
    end
  elseif (screenTier == 3) then -- 160, 50 
    if (size <= 1) then
      cSize = 10
    elseif (size == 2) then
      cSize = 5
    elseif (size >= 3) then
      cSize = 2
    end
  end
  
  grid1 = { width = WIDTH / cSize, height = HEIGHT / cSize}
  
  for x = 1, grid1.width do
    grid1[x] = {}
    for y = 1, grid1.height do
      grid1[x][y] = 0
    end
  end
end
 
-- Touch Event Handler
function myEventHandlers.touch(screenAdress, x, y, button, playerName)
 
  if (button == 0) then
    local i = math.ceil(x / cSize)
    local j = math.ceil(y / cSize)
    if (grid1[i][j] == 0) then
      grid1[i][j] = 1
    else
      grid1[i][j] = 0
    end
  end
end
 
-- Keyboard Event Handler
function myEventHandlers.key_up(adress, char, code, playerName)
 
  -- Space Bar (Pause/Play)
  if (code == 0x39) then
    pause = not pause
  end
  
  -- Backspace (Exit Program)
  if (code == 0x0E) then
    gpu.setResolution(WIDTH, HEIGHT)
    gpu.setBackground(0x000000)
    gpu.setForeground(0xFFFFFF)
    term.clear()
    os.exit()
  end
  
  -- R key (Reset)
  if (code == 0x13) then
    pause = true
    delay = minDelay
    setupGrid()
  end
  
  -- Up key
  if (code == 0xC8) then
    if (delay > minDelay) then
      delay = delay / 2
    end
  end
  
  -- Down key
  if (code == 0xD0) then
    if (delay < maxDelay) then
      delay = delay * 2
    end
  end
  
  -- Right key
  if (code == 0xCD) then
    pause = true
    if (size < 5) then
      size = size + 1
    end
    setupGrid()
  end
  
  -- Left key
  if (code == 0xCB) then
    Pause = true
    if (size > 1) then
      size = size - 1
    end
    setupGrid()
  end
end
 
-- Event Handler Method
function handleEvent(eventID, ...)
  if (eventID) then
    myEventHandlers[eventID](...)
  end
end
 
-- Cell Check Method
function checkCells(x, y, g)
 
  local n = 0
  for i = x-1, x+1 do
    for j = y-1, y+1 do
      if not (i < 1 or j < 1 or i > g.width or j > g.height or (i == x and j == y)) then
        n = n + g[i][j]
      end
    end
  end
  return n
end
 
setupGrid()
 
--[[****************************************************************
    MAIN LOOP
********************************************************************]]
while true do
 
  --[[ UPDATE ]]
  if not pause then
    timer = timer * 2
    
    if (timer >= delay) then
      timer = minDelay / 2
      local grid2 = { width = WIDTH / cSize, height = HEIGHT / cSize }
      
      for x = 1, grid1.width do
        grid2[x] = {}
        local r = grid1[x]
        
        for y = 1, grid1.height do
          local n = checkCells(x, y, grid1)
          local l = r[y]
          grid2[x][y] = l
          
          if (n < 2) then
            grid2[x][y] = 0
          elseif (n == 2) then
            grid2[x][y] = grid1[x][y]
          elseif (n == 3) then
            grid2[x][y] = 1
          elseif (n > 3) then
            grid2[x][y] = 0
          end
        end
      end
      
      grid1 = grid2
    end
  end
  
  --[[ DRAW ]]
  gpu.setBackground(0x000000)
  gpu.setForeground(0xFFFFFF)
  term.clear()
  
  for x = 1, grid1.width do
    for y = 1, grid1.height do
      local i = (x*cSize)-(cSize-1)
      local j = (y*cSize)-(cSize-1)
      
      if (grid1[x][y] == 1) then
        gpu.setBackground(0xFFFFFF)
      else
        gpu.setBackground(0x000000)
      end
      
      gpu.fill(i, j, i+cSize, j+cSize, " ")
    end
  end
  
  handleEvent(event.pull(0.001))
end
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.