eniallator

Lights Out

Mar 30th, 2016
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. local grid = {}
  2. -- Defining the 2D table as grid
  3.  
  4. local maxX,maxY = term.getSize()
  5. -- Getting the dimensions of the screen for the game
  6.  
  7. local colours = {
  8.     on = colors.green,
  9.     off = colors.red
  10. }
  11. -- Defining the colours used below
  12.  
  13. for x=1,maxX do
  14.  
  15.   grid[x] = {}
  16.  
  17.   for y=1,maxY do
  18.  
  19.     grid[x][y] = "on"
  20.   end
  21. end
  22. -- Making the 2D table with "on" as all of the values
  23.  
  24. local function checkWin()
  25.  
  26.   for i=1,#grid do
  27.     for j=1,#grid[i] do
  28.       if grid[i][j] == "on" then
  29.  
  30.         return false
  31.         -- If there is a value that is "on" it will immediately return false and prevent further unecessary iterations
  32.       end
  33.     end
  34.   end
  35.   -- Iterating over the 2D table
  36.  
  37.   return true
  38.   -- If the function hasn't already returned false, it will return true meaning that the user has won
  39. end
  40. -- Seeing if the user has won the game or not
  41.  
  42. local function toggle(x,y)
  43.   if grid[x][y] == "on" then
  44.  
  45.     grid[x][y] = "off"
  46.   else
  47.  
  48.     grid[x][y] = "on"
  49.   end
  50. end
  51. -- Toggling the status of a single tile in the 2D table
  52.  
  53. local function toggleAdjacent(x,y)
  54.  
  55.   toggle(x,y)
  56.   -- Toggling the tile that the user has clicked. I don't need an if to see if it exists since i already know it exists
  57.  
  58.   if grid[x+1] then
  59.  
  60.     toggle(x+1,y)
  61.   end
  62.  
  63.   if grid[x-1] then
  64.  
  65.     toggle(x-1,y)
  66.   end
  67.  
  68.   if grid[x][y+1] then
  69.  
  70.     toggle(x,y+1)
  71.   end
  72.  
  73.   if grid[x][y-1] then
  74.  
  75.     toggle(x,y-1)
  76.   end
  77.   -- First seeing if the 4 adjacent tiles exist and then if they do, it will toggle them
  78. end
  79.  
  80. local function displayGrid()
  81.   for y=1,#grid do
  82.     for x=1,#grid[y] do
  83.  
  84.       term.setCursorPos(x,y)
  85.       -- Setting the cursor's position to what the for loop's are up to
  86.  
  87.       if grid[x][y] == "on" then
  88.  
  89.         term.setBackgroundColor(colours.on)
  90.         -- Displaying the on colour if the tile is on
  91.       else
  92.  
  93.         term.setBackgroundColor(colours.off)
  94.         -- Displaying the off colour if the tile is off
  95.       end
  96.  
  97.       term.write(" ")
  98.       -- displaying the background without text
  99.     end
  100.   end
  101. end
  102. -- Displaying the 2D table so that the user can see whats been turned on and off
  103.  
  104. while not checkWin() do
  105.   -- Only iterating while the user hasn't won the game
  106.  
  107.   displayGrid()
  108.  
  109.   local event, button, clickX, clickY = os.pullEvent("mouse_click")
  110.   -- Getting where the user has clicked
  111.  
  112.   toggleAdjacent(clickX,clickY)
  113.   -- toggling the adjacent tiles aswell as the tile the user has clicked on
  114.  
  115. end
  116.  
  117. term.setBackgroundColor(colors.black)
  118. shell.run("clear")
  119. print("You Win!")
  120. sleep(2)
  121. -- Displaying the win message
Advertisement
Add Comment
Please, Sign In to add comment