Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local screen = peripheral.wrap("right")
- local color_table = {colors.black,colors.red,colors.green,colors.blue,colors.brown,colors.gray,colors.yellow,colors.pink,colors.white}
- local mX, mY = screen.getSize()
- local board = {0,0,0,0,0,0,0,0,0}
- local currentP = 1
- local running = true
- local button = {}
- local quitClick = 0
- function newgrid(colorr)
- screen.setBackgroundColor(color_table[3])
- screen.clear()
- local c1 = mX/3
- local c2 = c1*2+1
- local l1 = mY/3
- local l2 = l1*2+1
- drawVertical(c1,mY,colorr)
- drawVertical(c2,mY,colorr)
- drawHorizontal(l1,mX,colorr)
- drawHorizontal(l2,mX,colorr)
- end
- function addButton(name, nodex,nodey, xmin, xmax, ymin, ymax)
- button[name] = {}
- button[name]["nodex"] = nodex
- button[name]["nodey"] = nodey
- button[name]["active"] = true
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["xmax"] = xmax
- button[name]["ymax"] = ymax
- end
- function ClearButtons()
- button = {}
- end
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"]
- end
- function buttonxy(x, y)
- for name, data in pairs(button) do
- if x>=data["xmin"] and x<= data["xmax"] then
- if y>=data["ymin"] and y<= data["ymax"] then
- return data["nodex"],data["nodey"]
- end
- end
- end
- print("not found")
- return 1,1
- end
- function drawVertical(startX,SizeY,colorr)
- screen.setBackgroundColor(colorr)
- for yy = 1, SizeY, 1 do
- screen.setCursorPos(startX,yy)
- screen.write(" ")
- end
- end
- function drawHorizontal(startY,SizeX,colorr)
- screen.setBackgroundColor(colorr)
- for xx = 1, SizeX, 1 do
- screen.setCursorPos(xx,startY)
- screen.write(" ")
- end
- end
- function update(team,x,y)
- board[x+(y-1)*3] = team
- squareX(x,y,team)
- end
- function squareX(x,y,team)
- screen.setBackgroundColor(color_table[team+3])
- local szX = x*(mX/3)
- local szY = y*(mY/3)
- for xx = (x-1)*(mX/3) + (x-1), szX + (x-1)-1, 1 do
- for yy = (y-1)*(mY/3) + (y-1), szY + (y-1)-1, 1 do
- screen.setCursorPos(xx,yy)
- screen.write(" ")
- end
- end
- end
- function played(x,y)
- if currentP == 1 then
- currentP = 2
- update(1,x,y)
- else do
- currentP = 1
- update(2,x,y)
- end
- end
- end
- function setupButtons()
- for xnxx = 1, 3, 1 do
- for ynyy = 1, 3, 1 do
- 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)
- 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)
- end
- end
- end
- newgrid(color_table[1])
- term.clear()
- term.setCursorPos(1,1)
- term.write("=== Game Started ===")
- setupButtons()
- while running == true do
- local event, button, px, py = os.pullEvent("monitor_touch")
- local nodex,nodey = buttonxy(px,py)
- if board[nodex + (nodey-1)*3] == 1 or board[nodex + (nodey-1)*3] == 2 then
- quitClick = quitClick + 1
- if quitClick == 5 then
- running = false
- end
- else do
- played(nodex, nodey)
- end
- end
- end
Add Comment
Please, Sign In to add comment