Advertisement
Vilsol

Conway's Game Of Life [CC]

Aug 16th, 2013
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. board = {}
  2. tArgs = { ... }
  3. generation = 0
  4. sleeptime = 0.5
  5.  
  6. if(tArgs[1] == "left" or tArgs[1] == "right" or tArgs[1] == "top" or tArgs[1] == "bottom" or tArgs[1] == "front" or tArgs[1] == "back")then
  7.     mon = peripheral.wrap(tArgs[1])
  8. else
  9.     mon = term
  10. end
  11.  
  12. if(mon.isColor() or mon.isColor)then
  13.     colored = true
  14. else
  15.     colored = false
  16. end
  17.  
  18. w, h = mon.getSize()
  19. for x = 1, w do
  20.     board[x] = {}
  21.     for y = 1, h do
  22.         board[x][y] = 0
  23.     end
  24. end
  25.  
  26. function drawScreen()
  27.     w, h = mon.getSize()
  28.     for x = 1, w do
  29.         for y = 1, h do
  30.             nei = getNeighbours(x, y)
  31.             if(board[x][y] == 1)then
  32.                 if colored then
  33.                     if(nei < 2 or nei > 3)then
  34.                         mon.setBackgroundColor(colors.red)
  35.                     else
  36.                         mon.setBackgroundColor(colors.green)
  37.                     end
  38.                 else
  39.                     mon.setBackgroundColor(colors.white)
  40.                 end
  41.             else
  42.                 if colored then
  43.                     if(nei == 3)then
  44.                         mon.setBackgroundColor(colors.yellow)
  45.                     else
  46.                         mon.setBackgroundColor(colors.black)
  47.                     end
  48.                 else
  49.                     mon.setBackgroundColor(colors.black)
  50.                 end
  51.             end
  52.             mon.setCursorPos(x, y)
  53.             mon.write(" ")
  54.         end
  55.     end
  56.     mon.setCursorPos(1,1)
  57.     if colored then
  58.         mon.setTextColor(colors.blue)
  59.     end
  60.     mon.write(generation)
  61. end
  62.  
  63. function getNeighbours(x, y)
  64.     w, h = mon.getSize()
  65.     total = 0
  66.     if(x > 1 and y > 1)then if(board[x-1][y-1] == 1)then total = total + 1 end end
  67.     if(y > 1)then if(board[x][y-1] == 1)then total = total + 1 end end
  68.     if(x < w and y > 1)then if(board[x+1][y-1] == 1)then total = total + 1 end end
  69.     if(x > 1)then if(board[x-1][y] == 1)then total = total + 1 end end
  70.     if(x < w)then if(board[x+1][y] == 1)then total = total + 1 end end
  71.     if(x > 1 and y < h)then if(board[x-1][y+1] == 1)then total = total + 1 end end
  72.     if(y < h)then if(board[x][y+1] == 1)then total = total + 1 end end
  73.     if(x < w and y < h)then if(board[x+1][y+1] == 1)then total = total + 1 end end
  74.     return total
  75. end
  76.  
  77. function compute()
  78.     w, h = mon.getSize()
  79.     while true do
  80.         newBoard = {}
  81.         for x = 1, w do
  82.             newBoard[x] = {}
  83.             for y = 1, h do
  84.                 nei = getNeighbours(x, y)
  85.                 if(board[x][y] == 1)then
  86.                     if(nei < 2)then
  87.                         newBoard[x][y] = 0
  88.                     elseif(nei > 3)then
  89.                         newBoard[x][y] = 0
  90.                     else
  91.                         newBoard[x][y] = 1
  92.                     end
  93.                 else
  94.                     if(nei == 3)then
  95.                         newBoard[x][y] = 1
  96.                     end
  97.                 end
  98.             end
  99.         end
  100.         board = newBoard
  101.         generation = generation + 1
  102.         sleep(sleeptime)
  103.     end
  104. end
  105.  
  106. function loop()
  107.     while true do
  108.         event, variable, xPos, yPos = os.pullEvent()
  109.         if event == "mouse_click" or event == "monitor_touch" or event == "mouse_drag" then
  110.             if variable == 1 then
  111.                 board[xPos][yPos] = 1
  112.             else
  113.                 board[xPos][yPos] = 0
  114.             end
  115.         end
  116.         if event == "key" then
  117.             if tostring(variable) == "28" then
  118.                 return true
  119.             elseif tostring(variable) == "57" then
  120.                 if(mon.isColor() or mon.isColor)then
  121.                     colored = not colored
  122.                 end
  123.             elseif tostring(variable) == "200" then
  124.                 if sleeptime > 0.1 then
  125.                     sleeptime = sleeptime - 0.1
  126.                 end
  127.             elseif tostring(variable) == "208" then
  128.                 if sleeptime < 1 then
  129.                     sleeptime = sleeptime + 0.1
  130.                 end
  131.             end
  132.         end
  133.         drawScreen()
  134.     end
  135. end
  136.  
  137. function intro()
  138.     mon.setBackgroundColor(colors.black)
  139.     mon.clear()
  140.     mon.setCursorPos(1,1)
  141.     mon.write("Conway's Game Of Life")
  142.     mon.setCursorPos(1,2)
  143.     mon.write("It is a game which represents life.")
  144.     mon.setCursorPos(1,3)
  145.     mon.write("The game runs by 4 basic rules:")
  146.     mon.setCursorPos(1,4)
  147.     mon.write("1. If a cell has less than 2 neighbours, it dies.")
  148.     mon.setCursorPos(1,5)
  149.     mon.write("2. If a cell has 2 or 3 neightbours, it lives.")
  150.     mon.setCursorPos(1,6)
  151.     mon.write("3. If a cell has more than 3 neighbours, it dies.")
  152.     mon.setCursorPos(1,7)
  153.     mon.write("4. If a cell has exactly 3 neighbours it is born.")
  154.     mon.setCursorPos(1,9)
  155.     mon.write("At the top left is the generation count.")
  156.     mon.setCursorPos(1,10)
  157.     mon.write("Press spacebar to switch between color modes")
  158.     mon.setCursorPos(1,11)
  159.     mon.write("Press enter to start  the game")
  160.     mon.setCursorPos(1,13)
  161.     mon.write("Colors:")
  162.     mon.setCursorPos(1,14)
  163.     mon.write("Red - Cell will die in next generation")
  164.     mon.setCursorPos(1,15)
  165.     mon.write("Green - Cell will live in next generation")
  166.     mon.setCursorPos(1,16)
  167.     mon.write("Yellow - Cell will be born in next generation")
  168.     mon.setCursorPos(1,18)
  169.     mon.write("Press any key to continue!")
  170.     event, variable, xPos, yPos = os.pullEvent("key")
  171. end
  172.  
  173. intro()
  174. drawScreen()
  175. while true do
  176.     loop()
  177.     parallel.waitForAny(loop, compute)
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement