Advertisement
felixmaxwell

Game of Life [1.0.0]

Apr 17th, 2013
1,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. --(c) 2013 Felix Maxwell
  2. --License: CC BY-SA 3.0
  3.  
  4. local fps = 8 --Determines how long the program will wait between each tick
  5. local char = "#" --Live cells will look like this
  6.  
  7. function getMonitors()
  8.     local monitors = {}
  9.     if checkMonitorSide( "top" ) then table.insert( monitors, "top" ) end
  10.     if checkMonitorSide( "bottom" ) then table.insert( monitors, "bottom" ) end
  11.     if checkMonitorSide( "left" ) then table.insert( monitors, "left" ) end
  12.     if checkMonitorSide( "right" ) then table.insert( monitors, "right" ) end
  13.     if checkMonitorSide( "front" ) then table.insert( monitors, "front" ) end
  14.     if checkMonitorSide( "back" ) then table.insert( monitors, "back" ) end
  15.     return monitors
  16. end
  17. function checkMonitorSide( side )
  18.     if peripheral.isPresent( side ) then
  19.         if peripheral.getType(side) == "monitor" then
  20.             return true
  21.         end
  22.     end
  23.     return false
  24. end
  25. function printMonitorStats( side )
  26.     local x, y = peripheral.call(side, "getSize")
  27.     local color = "No"
  28.     if peripheral.call(side, "isColor") then
  29.         color = "Yes"
  30.     end
  31.     print("Side:"..side.." Size:("..x..", "..y..") Color?"..color)
  32. end
  33. function askMonitor()
  34.     local monitors = getMonitors()
  35.     if #monitors == 0 then
  36.         print("No monitors found, add more!")
  37.         return nill
  38.     elseif #monitors == 1 then
  39.         return monitors[1]
  40.     else
  41.         while true do
  42.             print("Multiple monitors found, please pick one.")
  43.             for i,v in ipairs(monitors) do
  44.                 write("["..(i).."] ")
  45.                 printMonitorStats( v )
  46.             end
  47.             write("Selection: ")
  48.             local sel = tonumber(io.read())
  49.             if sel < 1 or sel > #monitors then
  50.                 print("")
  51.                 print("Invalid number.")
  52.             else
  53.                 return monitors[sel]
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. function printCharAt( monitor, x, y, char )
  60.     monitor.setCursorPos( x, y )
  61.     monitor.write( char )
  62. end
  63. function printGrid( monitor, grid )
  64.     monitor.clear()
  65.     for i=1,#grid do
  66.         for o=1,#grid[i] do
  67.             printCharAt( monitor, i, o, grid[i][o] )
  68.         end
  69.     end
  70. end
  71.  
  72. function getNumNeighborhood( grid, x, y )
  73.     local neighbors = 0
  74.     if x > 1 then
  75.         if y > 1 then
  76.             if grid[x-1][y-1] == char then neighbors = neighbors + 1 end
  77.         end
  78.         if grid[x-1][y] == char then neighbors = neighbors + 1 end
  79.         if y < #grid[x] then
  80.             if grid[x-1][y+1] == char then neighbors = neighbors + 1 end
  81.         end
  82.     end
  83.    
  84.     if y > 1 then
  85.         if grid[x][y-1] == char then neighbors = neighbors + 1 end
  86.     end
  87.     if y < #grid[x] then
  88.         if grid[x][y+1] == char then neighbors = neighbors + 1 end
  89.     end
  90.    
  91.     if x < #grid then
  92.         if y > 1 then
  93.             if grid[x+1][y-1] == char then neighbors = neighbors + 1 end
  94.         end
  95.         if grid[x+1][y] == char then neighbors = neighbors + 1 end
  96.         if y < #grid then
  97.             if grid[x+1][y+1] == char then neighbors = neighbors + 1 end
  98.         end
  99.     end
  100.    
  101.     return neighbors
  102. end
  103. function lifeOrDeath( cur, neighbors )
  104.     if neighbors < 2 then
  105.         return " "
  106.     elseif neighbors > 3 then
  107.         return " "
  108.     elseif neighbors == 3 then
  109.         return char
  110.     else
  111.         return cur
  112.     end
  113. end
  114. function tick( grid )
  115.     local retGrid = {}
  116.     for x=1,#grid do
  117.         retGrid[x] = {}
  118.         for y=1,#grid[x] do
  119.             local num = getNumNeighborhood( grid, x, y )
  120.             retGrid[x][y] = lifeOrDeath( grid[x][y], num )
  121.         end
  122.     end
  123.     return retGrid
  124. end
  125.  
  126. function setup( w, h )
  127.     local grid = {}
  128.     for i=1,w do
  129.         grid[i] = {}
  130.         for o=1,h do
  131.             if math.random(1, 5) == 1 then
  132.                 grid[i][o] = char
  133.             else
  134.                 grid[i][o] = " "
  135.             end
  136.         end
  137.     end
  138.     return grid
  139. end
  140. function run()
  141.     local monitor = peripheral.wrap( askMonitor() )
  142.     if monitor.isColor() then
  143.         monitor.setTextColor(colors.lime)
  144.         monitor.setBackgroundColor(colors.black)
  145.     end
  146.     local w, h = monitor.getSize()
  147.     local grid = setup( w, h )
  148.     while true do
  149.         printGrid( monitor, grid )
  150.         grid = tick( grid )
  151.         os.sleep(1/fps)
  152.     end
  153. end
  154.  
  155. run()
  156.  
  157. --This one looks kind of cool
  158. --  ###
  159. --#    #
  160. --#    #
  161. --#    #
  162. --
  163. --  ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement