Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --(c) 2013 Felix Maxwell
- --License: CC BY-SA 3.0
- local fps = 8 --Determines how long the program will wait between each tick
- local char = "#" --Live cells will look like this
- function getMonitors()
- local monitors = {}
- if checkMonitorSide( "top" ) then table.insert( monitors, "top" ) end
- if checkMonitorSide( "bottom" ) then table.insert( monitors, "bottom" ) end
- if checkMonitorSide( "left" ) then table.insert( monitors, "left" ) end
- if checkMonitorSide( "right" ) then table.insert( monitors, "right" ) end
- if checkMonitorSide( "front" ) then table.insert( monitors, "front" ) end
- if checkMonitorSide( "back" ) then table.insert( monitors, "back" ) end
- return monitors
- end
- function checkMonitorSide( side )
- if peripheral.isPresent( side ) then
- if peripheral.getType(side) == "monitor" then
- return true
- end
- end
- return false
- end
- function printMonitorStats( side )
- local x, y = peripheral.call(side, "getSize")
- local color = "No"
- if peripheral.call(side, "isColor") then
- color = "Yes"
- end
- print("Side:"..side.." Size:("..x..", "..y..") Color?"..color)
- end
- function askMonitor()
- local monitors = getMonitors()
- if #monitors == 0 then
- print("No monitors found, add more!")
- return nill
- elseif #monitors == 1 then
- return monitors[1]
- else
- while true do
- print("Multiple monitors found, please pick one.")
- for i,v in ipairs(monitors) do
- write("["..(i).."] ")
- printMonitorStats( v )
- end
- write("Selection: ")
- local sel = tonumber(io.read())
- if sel < 1 or sel > #monitors then
- print("")
- print("Invalid number.")
- else
- return monitors[sel]
- end
- end
- end
- end
- function printCharAt( monitor, x, y, char )
- monitor.setCursorPos( x, y )
- monitor.write( char )
- end
- function printGrid( monitor, grid )
- monitor.clear()
- for i=1,#grid do
- for o=1,#grid[i] do
- printCharAt( monitor, i, o, grid[i][o] )
- end
- end
- end
- function getNumNeighborhood( grid, x, y )
- local neighbors = 0
- if x > 1 then
- if y > 1 then
- if grid[x-1][y-1] == char then neighbors = neighbors + 1 end
- end
- if grid[x-1][y] == char then neighbors = neighbors + 1 end
- if y < #grid[x] then
- if grid[x-1][y+1] == char then neighbors = neighbors + 1 end
- end
- end
- if y > 1 then
- if grid[x][y-1] == char then neighbors = neighbors + 1 end
- end
- if y < #grid[x] then
- if grid[x][y+1] == char then neighbors = neighbors + 1 end
- end
- if x < #grid then
- if y > 1 then
- if grid[x+1][y-1] == char then neighbors = neighbors + 1 end
- end
- if grid[x+1][y] == char then neighbors = neighbors + 1 end
- if y < #grid then
- if grid[x+1][y+1] == char then neighbors = neighbors + 1 end
- end
- end
- return neighbors
- end
- function lifeOrDeath( cur, neighbors )
- if neighbors < 2 then
- return " "
- elseif neighbors > 3 then
- return " "
- elseif neighbors == 3 then
- return char
- else
- return cur
- end
- end
- function tick( grid )
- local retGrid = {}
- for x=1,#grid do
- retGrid[x] = {}
- for y=1,#grid[x] do
- local num = getNumNeighborhood( grid, x, y )
- retGrid[x][y] = lifeOrDeath( grid[x][y], num )
- end
- end
- return retGrid
- end
- function setup( w, h )
- local grid = {}
- for i=1,w do
- grid[i] = {}
- for o=1,h do
- if math.random(1, 5) == 1 then
- grid[i][o] = char
- else
- grid[i][o] = " "
- end
- end
- end
- return grid
- end
- function run()
- local monitor = peripheral.wrap( askMonitor() )
- if monitor.isColor() then
- monitor.setTextColor(colors.lime)
- monitor.setBackgroundColor(colors.black)
- end
- local w, h = monitor.getSize()
- local grid = setup( w, h )
- while true do
- printGrid( monitor, grid )
- grid = tick( grid )
- os.sleep(1/fps)
- end
- end
- run()
- --This one looks kind of cool
- -- ###
- --# #
- --# #
- --# #
- --
- -- ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement