paramus

Tic Tac Toe

Mar 21st, 2025 (edited)
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | Gaming | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local P1 = {}
  3. local P2 = {}
  4. local P1len, P2len = 0,0
  5.  
  6. function Draw()
  7.     term.setBackgroundColour(colors.white)
  8.     term.clear()
  9.    
  10.     -- Player 1
  11.     term.setTextColour(colors.blue)
  12.     local first = true
  13.     P1len = 0
  14.     for i,u in pairs(P1) do
  15.         if (first) then
  16.             term.setTextColour(colors.cyan)
  17.             first = false
  18.         else
  19.             term.setTextColour(colors.blue)
  20.         end
  21.        
  22.         term.setCursorPos(u[1],u[2])
  23.         write("X")
  24.         P1len = P1len + 1
  25.     end
  26.    
  27.     -- Player 2
  28.     term.setTextColour(colors.red)
  29.     first = true
  30.     P2len = 0
  31.     for i,u in pairs(P2) do
  32.         if (first) then
  33.             term.setTextColour(colors.pink)
  34.             first = false
  35.         else
  36.             term.setTextColour(colors.red)
  37.         end
  38.        
  39.         term.setCursorPos(u[1],u[2])
  40.         write("O")
  41.         P2len = P2len + 1
  42.     end
  43.    
  44.     term.setTextColour(colors.black)
  45.     --R1
  46.     term.setCursorPos(3,1)
  47.     write("|")
  48.     term.setCursorPos(5,1)
  49.     write("|")
  50.     --R2
  51.     term.setCursorPos(2,2)
  52.     write("-")
  53.     term.setCursorPos(3,2)
  54.     write("+")
  55.     term.setCursorPos(4,2)
  56.     write("-")
  57.     term.setCursorPos(5,2)
  58.     write("+")
  59.     term.setCursorPos(6,2)
  60.     write("-")
  61.     --R3
  62.     term.setCursorPos(3,3)
  63.     write("|")
  64.     term.setCursorPos(5,3)
  65.     write("|")
  66.     --R4
  67.     term.setCursorPos(2,4)
  68.     write("-")
  69.     term.setCursorPos(3,4)
  70.     write("+")
  71.     term.setCursorPos(4,4)
  72.     write("-")
  73.     term.setCursorPos(5,4)
  74.     write("+")
  75.     term.setCursorPos(6,4)
  76.     write("-")
  77.     --R5
  78.     term.setCursorPos(3,5)
  79.     write("|")
  80.     term.setCursorPos(5,5)
  81.     write("|")
  82. end
  83.  
  84. function ValidateMove(x, y)
  85.     if (x == 1 or x >= 7 or y > 5) then
  86.         return false
  87.     end
  88.    
  89.     if ( (x-1)%2 == 0 or y%2 == 0) then
  90.         return false
  91.     end
  92.    
  93.     for i,u in pairs(P1) do
  94.         if (x == u[1] and y == u[2]) then
  95.             return false
  96.         end
  97.     end
  98.    
  99.     for i,u in pairs(P2) do
  100.         if (x == u[1] and y == u[2]) then
  101.             return false
  102.         end
  103.     end
  104.    
  105.     return true
  106. end
  107.  
  108. local eventclick = "mouse_click"
  109. if (monitor ~= nil) then
  110.     term.redirect(monitor)
  111.     eventclick = "monitor_touch"
  112. end
  113.  
  114. while (true) do
  115.     parallel.waitForAny(function()
  116.         local validMove = false
  117.         local event, button, x, y
  118.  
  119.         Draw()
  120.         while (validMove == false) do
  121.             event, button, x, y = os.pullEvent(eventclick)
  122.             if (ValidateMove(x,y)) then
  123.                 validMove = true
  124.             end
  125.         end
  126.  
  127.         if (P1len >= 3) then
  128.             table.remove(P1, 1)
  129.         end
  130.         table.insert(P1, {x,y})
  131.  
  132.         Draw()
  133.         validMove = false
  134.         while (validMove == false) do
  135.             event, button, x, y = os.pullEvent(eventclick)
  136.             if (ValidateMove(x,y)) then
  137.                 validMove = true
  138.             end
  139.         end
  140.  
  141.         if (P2len >= 3) then
  142.             table.remove(P2, 1)
  143.         end
  144.         table.insert(P2, {x,y})
  145.     end,
  146.     function()
  147.         os.pullEvent("redstone")
  148.         P1 = {}
  149.         P2 = {}
  150.     end)
  151. end
Advertisement
Add Comment
Please, Sign In to add comment