PrinzJuliano

Lights off puzzle 3x3

Aug 30th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. if not term.isColor() then
  2.  error("You need an advanced Computer to run this",2)
  3. end
  4. local field = {}
  5. field[1] = {"X","O","X"};
  6. field[2] = {"O","X","O"};
  7. field[3] = {"X","O","X"};
  8. local running = true
  9. local clicks = 0
  10. local slc = 1
  11. --[[
  12. y
  13. ^
  14. |
  15. |
  16. |
  17. +----->x
  18. ]]
  19.  
  20. function clear()
  21. term.setBackgroundColor(colors.black)
  22. term.clear()
  23. term.setCursorPos(1,1)
  24. end
  25.  
  26. function generate()
  27. for i=1,3 do
  28.  for j=1,3 do
  29.   field[i][j] = (math.random(1,2)==1 and "X" or "O")
  30.  end
  31. end
  32. end
  33.  
  34. function draw()
  35.  clear()
  36.  print("New | Exit | Clicks: ",clicks)
  37.  print()
  38.  if slc == 1 then
  39.   for y=1,3 do
  40.    term.write(" ")
  41.    for x=1,3 do
  42.    term.setBackgroundColor(field[y][x] == "X" and colors.blue or colors.red)
  43.     term.write(field[y][x])
  44.    term.setBackgroundColor(colors.black)
  45.     term.write(" ")
  46.    end
  47.    print()
  48.   end
  49.  else
  50.   print("You won! And it took you "..clicks.." clicks")
  51.  end
  52. end
  53.  
  54. function check()
  55.  if field[1][1] == field[1][2] and field[1][2] == field[1][3] and
  56.     field[1][3] == field[2][1] and field[2][1] == field[2][2] and
  57.     field[2][2] == field[2][3] and field[2][3] == field[3][1] and
  58.     field[3][1] == field[3][2] and field[3][2] == field[3][3] then
  59.   return true
  60.  else
  61.   return false
  62.  end
  63. end
  64.  
  65. function update(e)
  66.  if e[1] == "char" then
  67.   if e[2] == "q" then
  68.    running = false
  69.    return
  70.   end
  71.  elseif e[1] == "mouse_click" then
  72.   button = e[2]
  73.   x = e[3]
  74.   y = e[4]
  75.   --click new
  76.   if x >=1 and x <= 3 and y==1 then
  77.    generate()
  78.    clicks = 0
  79.    slc = 1
  80.    return
  81.   --exit
  82.   elseif x >= 7 and x <= 10 and y == 1 then
  83.    running = false
  84.    return
  85.   --click the buttons
  86.   elseif x == 2 then
  87.    if y == 3 then
  88.     field[1][1] = (field[1][1] == "O" and "X" or "O")
  89.     field[1][2] = (field[1][2] == "O" and "X" or "O")
  90.     field[2][1] = (field[2][1] == "O" and "X" or "O")
  91.     clicks = clicks+1
  92.    
  93.    elseif y == 4 then
  94.     field[1][1] = (field[1][1] == "O" and "X" or "O")
  95.     field[2][1] = (field[2][1] == "O" and "X" or "O")
  96.     field[2][2] = (field[2][2] == "O" and "X" or "O")
  97.     field[3][1] = (field[3][1] == "O" and "X" or "O")
  98.     clicks = clicks+1
  99.    
  100.    elseif y == 5 then
  101.     field[2][1] = (field[2][1] == "O" and "X" or "O")
  102.     field[3][1] = (field[3][1] == "O" and "X" or "O")
  103.     field[3][2] = (field[3][2] == "O" and "X" or "O")
  104.     clicks = clicks+1
  105.    end
  106.   elseif x == 4 then
  107.    if y == 3 then
  108.     field[1][1] = (field[1][1] == "O" and "X" or "O")
  109.     field[1][2] = (field[1][2] == "O" and "X" or "O")
  110.     field[1][3] = (field[1][3] == "O" and "X" or "O")
  111.     field[2][2] = (field[2][2] == "O" and "X" or "O")
  112.     clicks = clicks+1
  113.    
  114.    elseif y == 4 then
  115.     field[1][2] = (field[1][2] == "O" and "X" or "O")
  116.     field[2][1] = (field[2][1] == "O" and "X" or "O")
  117.     field[2][2] = (field[2][2] == "O" and "X" or "O")
  118.     field[2][3] = (field[2][3] == "O" and "X" or "O")
  119.     field[3][2] = (field[3][2] == "O" and "X" or "O")
  120.     clicks = clicks+1
  121.    
  122.    elseif y == 5 then
  123.     field[2][2] = (field[2][2] == "O" and "X" or "O")
  124.     field[3][1] = (field[3][1] == "O" and "X" or "O")
  125.     field[3][2] = (field[3][2] == "O" and "X" or "O")
  126.     field[3][3] = (field[3][3] == "O" and "X" or "O")
  127.     clicks = clicks+1
  128.    end
  129.   elseif x == 6 then
  130.    if y == 3 then
  131.     field[1][2] = (field[1][2] == "O" and "X" or "O")
  132.     field[1][3] = (field[1][2] == "O" and "X" or "O")
  133.     field[2][3] = (field[2][3] == "O" and "X" or "O")
  134.     clicks = clicks+1
  135.    
  136.    elseif y == 4 then
  137.     field[1][3] = (field[1][3] == "O" and "X" or "O")
  138.     field[2][2] = (field[2][2] == "O" and "X" or "O")
  139.     field[2][3] = (field[2][3] == "O" and "X" or "O")
  140.     field[3][3] = (field[3][3] == "O" and "X" or "O")
  141.     clicks = clicks+1
  142.    
  143.    elseif y == 5 then
  144.     field[2][3] = (field[2][3] == "O" and "X" or "O")
  145.     field[3][2] = (field[3][2] == "O" and "X" or "O")
  146.     field[3][3] = (field[3][3] == "O" and "X" or "O")
  147.     clicks = clicks+1
  148.    end
  149.   end
  150.  end
  151. end
  152.  
  153. function mainLoop()
  154. while running do
  155.  e = {os.pullEvent()}
  156.  update(e)
  157.  if check() == true then
  158.   slc = 2
  159.  end
  160.  draw()
  161. end
  162. end
  163.  
  164. generate()
  165. draw()
  166. mainLoop()
  167. clear()
Advertisement
Add Comment
Please, Sign In to add comment