Guest User

TicTacToe

a guest
Jul 29th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. os.loadAPI("button")
  2. local mainMonitor = "top"
  3. GamePanel = peripheral.wrap(mainMonitor)
  4. GamePanel.clear()
  5. ControlPanel = peripheral.wrap("right")
  6. ControlPanel.clear()
  7. local xc, yc = ControlPanel.getSize()
  8. function writeMessage(message)
  9.   ControlPanel.setCursorPos(math.floor((xc - string.len(message))/2+1),yc/2 + 1)
  10.   ControlPanel.write(message)
  11. end
  12. writeMessage("CrossTurn")
  13. local xo, yo = GamePanel.getSize()
  14. print("Width: "..xo)
  15. print("Height: "..yo)
  16. local width = math.floor((xo-2)/3)
  17. local height = math.floor((yo-2)/3)
  18. print("Box width: "..width)
  19. print("Box height: "..height)
  20. sleep(0)
  21.  
  22. function fx(params)
  23.   local x = params["x"]
  24.   local y = params["y"]
  25.   print("Field at "..x..","..y.." was clicked")
  26. end
  27. --fx setup
  28. --0,0 1,0 2,0
  29. --0,1 1,1 2,1
  30. --0,2 1,2 2,2
  31.  
  32. function fillTable()
  33.   for x = 0,2 do
  34.     for y = 0,2 do
  35.       local xPos = x * width + x + 1
  36.       local yPos = y * height + y + 1
  37.       print("Drawing button "..x..","..y.." at "..xPos..","..yPos.."...")
  38.       button.setTable(x..y, fx, {["x"] = x, ["y"] = y}, xPos, xPos + width - 1, yPos, yPos + height - 1)
  39.     end
  40.   end
  41.   button.screen()
  42. end
  43.  
  44. function getClick()
  45.   event,side,x,y = os.pullEvent("monitor_touch")
  46.   if side == mainMonitor then
  47.     button.checkxy(x,y)
  48.   end
  49. end
  50.  
  51. fillTable()
  52. --button.heading("TicTacToe")
  53.  
  54. while true do
  55.   getClick()
  56. end
Add Comment
Please, Sign In to add comment