Advertisement
Good_Pudge

Noughts'n'Crosses

Sep 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.92 KB | None | 0 0
  1. local com = require("component")
  2. local gpu = com.gpu
  3. local term = require("term")
  4. local color = require("colors")
  5. local comp = require("computer")
  6. local kb = require("keyboard")
  7.  
  8. local OldW,OldH = gpu.getResolution()
  9. local OldB = gpu.getBackground()
  10. local OldF = gpu.getForeground()
  11.  
  12. term.clear()
  13.  
  14. gpu.setBackground(color.black,true)
  15. gpu.setForeground(color.white,true)
  16. gpu.setResolution(10,5)
  17.  
  18. gpu.fill(3,2,6,3," ")
  19.  
  20.  
  21. function textbox(xPos,yPos,text)
  22.     term.setCursor(xPos,yPos)
  23.     term.write(text)
  24. end
  25.  
  26. ::Again::
  27. --Переменные--
  28. local Steps = {}
  29. local PS = "X"
  30. local BS = "O"
  31. local SD = false
  32.  
  33. gpu.setBackground(0x000000)
  34. gpu.setForeground(0xFFFFFF)
  35. term.clear()
  36. gpu.setBackground(0x008705)
  37. gpu.fill(1,5,2,1," ")
  38. gpu.setBackground(0xa31800)
  39. gpu.fill(9,5,2,1," ")
  40. gpu.setBackground(0xFFFFFF)
  41. gpu.setForeground(0x000000)
  42. gpu.fill(3,2,6,3," ")
  43.  
  44. ::Single::
  45. while true do
  46.     local typeT,_,x,y,button,nick = comp.pullSignal(0,"touch")
  47.  
  48.     --ВЫИГРЫШ ИГРОКА--
  49.  
  50.     --Диагональ--
  51.     if Steps[1] == PS and Steps[5] == PS and Steps[9] == PS then
  52.         gpu.setBackground(0x008705)
  53.         gpu.fill(3,2,2,1," ")
  54.         gpu.fill(5,3,2,1," ")
  55.         gpu.fill(7,4,2,1," ")
  56.         goto Wait
  57.     end
  58.  
  59.     if Steps[3] == PS and Steps[5] == PS and Steps[7] == PS then
  60.         gpu.setBackground(0x008705)
  61.         gpu.fill(7,2,2,1," ")
  62.         gpu.fill(5,3,2,1," ")
  63.         gpu.fill(3,4,2,1," ")
  64.         goto Wait
  65.     end
  66.  
  67.     --Горизонталь--
  68.     if Steps[1] == PS and Steps[2] == PS and Steps[3] == PS then
  69.         gpu.setBackground(0x008705)
  70.         gpu.fill(3,2,2,1," ")
  71.         gpu.fill(5,2,2,1," ")
  72.         gpu.fill(7,2,2,1," ")
  73.         goto Wait
  74.     end
  75.  
  76.     if Steps[4] == PS and Steps[5] == PS and Steps[6] == PS then
  77.         gpu.setBackground(0x008705)
  78.         gpu.fill(3,3,2,1," ")
  79.         gpu.fill(5,3,2,1," ")
  80.         gpu.fill(7,3,2,1," ")
  81.         goto Wait
  82.     end
  83.  
  84.     if Steps[7] == PS and Steps[8] == PS and Steps[9] == PS then
  85.         gpu.setBackground(0x008705)
  86.         gpu.fill(3,4,2,1," ")
  87.         gpu.fill(5,4,2,1," ")
  88.         gpu.fill(7,4,2,1," ")
  89.         goto Wait
  90.     end
  91.  
  92.     --Вертикаль--
  93.     if Steps[1] == PS and Steps[4] == PS and Steps[7] == PS then
  94.         gpu.setBackground(0x008705)
  95.         gpu.fill(3,2,2,1," ")
  96.         gpu.fill(3,3,2,1," ")
  97.         gpu.fill(3,4,2,1," ")
  98.         goto Wait
  99.     end
  100.  
  101.     if Steps[2] == PS and Steps[5] == PS and Steps[8] == PS then
  102.         gpu.setBackground(0x008705)
  103.         gpu.fill(5,2,2,1," ")
  104.         gpu.fill(5,3,2,1," ")
  105.         gpu.fill(5,4,2,1," ")
  106.         goto Wait
  107.     end
  108.  
  109.     if Steps[3] == PS and Steps[6] == PS and Steps[9] == PS then
  110.         gpu.setBackground(0x008705)
  111.         gpu.fill(7,2,2,1," ")
  112.         gpu.fill(7,3,2,1," ")
  113.         gpu.fill(7,4,2,1," ")
  114.         goto Wait
  115.     end
  116.  
  117.     --ВЫИГРЫШ БОТА--
  118.  
  119.     --Диагональ--
  120.     if Steps[1] == BS and Steps[5] == BS and Steps[9] == BS then
  121.         gpu.setBackground(0xa31800)
  122.         gpu.fill(3,2,2,1," ")
  123.         gpu.fill(5,3,2,1," ")
  124.         gpu.fill(7,4,2,1," ")
  125.         goto Wait
  126.     end
  127.  
  128.     if Steps[3] == BS and Steps[5] == BS and Steps[7] == BS then
  129.         gpu.setBackground(0xa31800)
  130.         gpu.fill(7,2,2,1," ")
  131.         gpu.fill(5,3,2,1," ")
  132.         gpu.fill(3,4,2,1," ")
  133.         goto Wait
  134.     end
  135.  
  136.     --Горизонталь--
  137.     if Steps[1] == BS and Steps[2] == BS and Steps[3] == BS then
  138.         gpu.setBackground(0xa31800)
  139.         gpu.fill(3,2,2,1," ")
  140.         gpu.fill(5,2,2,1," ")
  141.         gpu.fill(7,2,2,1," ")
  142.         goto Wait
  143.     end
  144.  
  145.     if Steps[4] == BS and Steps[5] == BS and Steps[6] == BS then
  146.         gpu.setBackground(0xa31800)
  147.         gpu.fill(3,3,2,1," ")
  148.         gpu.fill(5,3,2,1," ")
  149.         gpu.fill(7,3,2,1," ")
  150.         goto Wait
  151.     end
  152.  
  153.     if Steps[7] == BS and Steps[8] == BS and Steps[9] == BS then
  154.         gpu.setBackground(0xa31800)
  155.         gpu.fill(3,4,2,1," ")
  156.         gpu.fill(5,4,2,1," ")
  157.         gpu.fill(7,4,2,1," ")
  158.         goto Wait
  159.     end
  160.  
  161.     --Вертикаль--
  162.     if Steps[1] == BS and Steps[4] == BS and Steps[7] == BS then
  163.         gpu.setBackground(0xa31800)
  164.         gpu.fill(3,2,2,1," ")
  165.         gpu.fill(3,3,2,1," ")
  166.         gpu.fill(3,4,2,1," ")
  167.         goto Wait
  168.     end
  169.  
  170.     if Steps[2] == BS and Steps[5] == BS and Steps[8] == BS then
  171.         gpu.setBackground(0xa31800)
  172.         gpu.fill(5,2,2,1," ")
  173.         gpu.fill(5,3,2,1," ")
  174.         gpu.fill(5,4,2,1," ")
  175.         goto Wait
  176.     end
  177.  
  178.     if Steps[3] == BS and Steps[6] == BS and Steps[9] == BS then
  179.         gpu.setBackground(0xa31800)
  180.         gpu.fill(7,2,2,1," ")
  181.         gpu.fill(7,3,2,1," ")
  182.         gpu.fill(7,4,2,1," ")
  183.         goto Wait
  184.     end
  185.  
  186.     if typeT == "touch" then
  187.         Steps[1] = gpu.get(3,2) --Первая строка
  188.         Steps[2] = gpu.get(5,2)
  189.         Steps[3] = gpu.get(7,2)
  190.  
  191.         Steps[4] = gpu.get(3,3) --Вторая строка
  192.         Steps[5] = gpu.get(5,3)
  193.         Steps[6] = gpu.get(7,3)
  194.  
  195.         Steps[7] = gpu.get(3,4) --Третья строка
  196.         Steps[8] = gpu.get(5,4)
  197.         Steps[9] = gpu.get(7,4)
  198.  
  199.         --Выход--
  200.         if x >= 9 and x < 11 and y == 5 then
  201.             goto Exit
  202.         end
  203.  
  204.         --Заного--
  205.         if x >= 1 and x < 3 and y == 5 then
  206.             goto Again
  207.         end
  208.  
  209.         --Ход игрока--
  210.         if x >= 3 and x < 5 and y == 2 then
  211.             if Steps[1] ~= BS then
  212.                 textbox(3,2,PS)
  213.                 SD = true
  214.             end
  215.         elseif x >= 5 and x < 7 and y == 2 then
  216.             if Steps[2] ~= BS then
  217.                 textbox(5,2,PS)
  218.                 SD = true
  219.             end
  220.         elseif x >= 7 and x < 9 and y == 2 then
  221.             if Steps[3] ~= BS then
  222.                 textbox(7,2,PS)
  223.                 SD = true
  224.             end
  225.         elseif x >= 3 and x < 5 and y == 3 then --Вторая строка
  226.             if Steps[4] ~= BS then
  227.                 textbox(3,3,PS)
  228.                 SD = true
  229.             end
  230.         elseif x >= 5 and x < 7 and y == 3 then
  231.             if Steps[5] ~= BS then
  232.                 textbox(5,3,PS)
  233.                 SD = true
  234.             end
  235.         elseif x >= 7 and x < 9 and y == 3 then
  236.             if Steps[6] ~= BS then
  237.                 textbox(7,3,PS)
  238.                 SD = true
  239.             end
  240.         elseif x >= 3 and x < 5 and y == 4 then --Третья строка
  241.             if Steps[7] ~= BS then
  242.                 textbox(3,4,PS)
  243.                 SD = true
  244.             end
  245.         elseif x >= 5 and x < 7 and y == 4 then
  246.             if Steps[8] ~= BS then
  247.                 textbox(5,4,PS)
  248.                 SD = true
  249.             end
  250.         elseif x >= 7 and x < 9 and y == 4 then
  251.             if Steps[9] ~= BS then
  252.                 textbox(7,4,PS)
  253.                 SD = true
  254.             end
  255.         end
  256.  
  257.         --Ход бота--
  258.         local botStep = math.random(1,9)
  259.        
  260.         if SD then
  261.             SD = false
  262.  
  263.             for i = 1,9 do
  264.                 if Steps[botStep] == " " then
  265.                     if botStep == 1 then
  266.                         textbox(3,2,BS)
  267.                         break
  268.                     elseif botStep == 2 then
  269.                         textbox(5,2,BS)
  270.                         break
  271.                     elseif botStep == 3 then
  272.                         textbox(7,2,BS)
  273.                         break
  274.                     elseif botStep == 4 then
  275.                         textbox(3,3,BS)
  276.                         break
  277.                     elseif botStep == 5 then
  278.                         textbox(5,3,BS)
  279.                         break
  280.                     elseif botStep == 6 then
  281.                         textbox(7,3,BS)
  282.                         break
  283.                     elseif botStep == 7 then
  284.                         textbox(3,4,BS)
  285.                         break
  286.                     elseif botStep == 8 then
  287.                         textbox(5,4,BS)
  288.                         break
  289.                     elseif botStep == 9 then
  290.                         textbox(7,4,BS)
  291.                         break
  292.                     end
  293.                 else
  294.                     botStep = math.random(1,9)
  295.                 end
  296.             end
  297.         end
  298.     end
  299. end
  300.  
  301. ::Wait::
  302. while true do
  303.     local typeT,_,x,y,button,nick = comp.pullSignal(0,"touch")
  304.  
  305.     if typeT == "touch" then
  306.         if x >= 1 and x < 3 and y == 5 then
  307.             goto Again
  308.         end
  309.  
  310.         if x >= 9 and x < 11 and y == 5 then
  311.             break
  312.         end
  313.     end
  314. end
  315.  
  316. ::Exit::
  317. gpu.setBackground(OldB,false)
  318. gpu.setForeground(OldF,false)
  319. gpu.setResolution(OldW,OldH)
  320. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement