Snowdingerr

TicTacToe

Feb 8th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. local screen = peripheral.wrap("right")
  2. local color_table = {colors.black,colors.red,colors.green,colors.blue,colors.brown,colors.gray,colors.yellow,colors.pink,colors.white}
  3. local mX, mY = screen.getSize()
  4. local board = {0,0,0,0,0,0,0,0,0}
  5. local currentP = 1
  6. local running = true
  7. local button = {}
  8. local quitClick = 0
  9.  
  10. function newgrid(colorr)
  11.     screen.setBackgroundColor(color_table[3])
  12.     screen.clear()
  13.     local c1 = mX/3
  14.     local c2 = c1*2+1
  15.     local l1 = mY/3
  16.     local l2 = l1*2+1
  17.     drawVertical(c1,mY,colorr)
  18.     drawVertical(c2,mY,colorr)
  19.     drawHorizontal(l1,mX,colorr)
  20.     drawHorizontal(l2,mX,colorr)
  21. end
  22.  
  23. function addButton(name, nodex,nodey, xmin, xmax, ymin, ymax)
  24.     button[name] = {}
  25.     button[name]["nodex"] = nodex
  26.     button[name]["nodey"] = nodey
  27.     button[name]["active"] = true
  28.     button[name]["xmin"] = xmin
  29.     button[name]["ymin"] = ymin
  30.     button[name]["xmax"] = xmax
  31.     button[name]["ymax"] = ymax
  32. end
  33. function ClearButtons()
  34.     button = {}
  35. end
  36. function toggleButton(name)
  37.     button[name]["active"] = not button[name]["active"]
  38. end
  39. function buttonxy(x, y)
  40.     for name, data in pairs(button) do
  41.         if x>=data["xmin"] and x<= data["xmax"] then
  42.             if y>=data["ymin"] and y<= data["ymax"] then
  43.                 return data["nodex"],data["nodey"]
  44.             end
  45.         end
  46.     end
  47.     print("not found")
  48.     return 1,1
  49. end
  50.  
  51. function drawVertical(startX,SizeY,colorr)
  52.     screen.setBackgroundColor(colorr)
  53.     for yy = 1, SizeY, 1 do
  54.         screen.setCursorPos(startX,yy)
  55.         screen.write(" ")
  56.     end
  57. end
  58. function drawHorizontal(startY,SizeX,colorr)
  59.     screen.setBackgroundColor(colorr)
  60.     for xx = 1, SizeX, 1 do
  61.         screen.setCursorPos(xx,startY)
  62.         screen.write(" ")
  63.     end
  64. end
  65. function update(team,x,y)
  66.     board[x+(y-1)*3] = team
  67.     squareX(x,y,team)
  68. end
  69. function squareX(x,y,team)
  70.     screen.setBackgroundColor(color_table[team+3])
  71.     local szX = x*(mX/3)
  72.     local szY = y*(mY/3)
  73.     for xx = (x-1)*(mX/3) + (x-1), szX + (x-1)-1, 1 do
  74.         for yy = (y-1)*(mY/3) + (y-1), szY + (y-1)-1, 1 do
  75.             screen.setCursorPos(xx,yy)
  76.             screen.write(" ")
  77.         end
  78.     end
  79. end
  80. function played(x,y)
  81.     if currentP == 1 then
  82.         currentP = 2
  83.         update(1,x,y)
  84.     else do
  85.         currentP = 1
  86.         update(2,x,y)
  87.         end
  88.     end
  89. end
  90. function setupButtons()
  91.     for xnxx = 1, 3, 1 do
  92.         for ynyy = 1, 3, 1 do
  93.             addButton("Node".. xnxx .. " " .. ynyy,xnxx,ynyy,(xnxx-1)*(mX/3),xnxx*(mX/3) + (xnxx-1)-1, (ynyy-1)*(mY/3),ynyy*(mY/3) + (ynyy-1)-1)
  94.             print("Node".. xnxx .. " " .. ynyy,xnxx,ynyy,(xnxx-1)*(mX/3),xnxx*(mX/3) + (xnxx-1)-1, (ynyy-1)*(mY/3),ynyy*(mY/3)+ (ynyy-1)-1)
  95.         end
  96.     end
  97. end
  98.  
  99. newgrid(color_table[1])
  100. term.clear()
  101. term.setCursorPos(1,1)
  102. term.write("=== Game Started ===")
  103. setupButtons()
  104. while running == true do
  105.     local event, button, px, py = os.pullEvent("monitor_touch")
  106.     local nodex,nodey = buttonxy(px,py)
  107.     if board[nodex + (nodey-1)*3] == 1 or board[nodex + (nodey-1)*3] == 2 then
  108.         quitClick = quitClick + 1
  109.         if quitClick == 5 then
  110.             running = false
  111.         end
  112.     else do
  113.         played(nodex, nodey)
  114.         end
  115.     end
  116. end
Add Comment
Please, Sign In to add comment