Guest User

puzzle

a guest
Jun 10th, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. local lights = {}
  2. local function clear()
  3.   term.clear()
  4.   term.setCursorPos(1,1)
  5. end
  6. local function getNum(num)
  7.   return math.floor((num+1)/2)
  8. end
  9. clear()
  10. write "Enter Difficulty (minimum of 3): "
  11. d = tonumber(read())
  12. if d < 3 then
  13.   error("Please Enter a number greater than 3")
  14. end
  15. for i=1, d*d do
  16.   lights[i] = "false"
  17. end
  18. for i=1, d/3+d%3 do
  19.   lights[math.random(d*d)] = "true"
  20. end
  21. clear()
  22. function drawLights()
  23.   for i=1, d do
  24.     for a=1, d do
  25.       term.setCursorPos((a*2)-1,i)
  26.       if lights[(i*d)-(d-a)] == "false" then
  27.         write("X")
  28.       else
  29.         write"O"
  30.       end
  31.     end
  32.   end
  33. end
  34. drawLights()
  35. local function getCoor()
  36.   return (y*d)-(d-x)
  37. end
  38. local function setLights(lightNum)
  39.   if lights[lightNum] == "true" then
  40.     lights[lightNum] = "false"
  41.   else
  42.     lights[lightNum] = "true"
  43.   end
  44. end
  45. while true do
  46.   event, mouse, x, y = os.pullEvent("mouse_click")
  47.   term.setCursorPos(1,7)
  48.   x = getNum(x)
  49.   setLights(getCoor())
  50.   if y ~= 1 then
  51.     setLights(getCoor()-d)
  52.   end
  53.   if y < d then
  54.     setLights(getCoor()+d)
  55.   end
  56.   if x < d then
  57.     setLights(getCoor()+1)
  58.   end
  59.   if x ~= 1 then
  60.     setLights(getCoor()-1)
  61.   end
  62.   drawLights()
  63.   local test = false
  64.   for i=1, #lights do
  65.     if lights[i] == "false" then
  66.       test = true
  67.     end
  68.   end
  69.   if test == false then
  70.     break
  71.   end
  72. end
  73. term.setTextColor(colors.yellow)
  74. drawLights()
  75. os.pullEvent()
  76. clear()
Advertisement
Add Comment
Please, Sign In to add comment