Piorjade

ComputerCraft TicTacToe game (randomized starts)

May 16th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. --Basic TicTacToe Game
  2. --Local Multiplayer (=there is no AI)
  3. --Written completely by Piorjade
  4.  
  5.  
  6. --Variablen
  7. _ver = 0.1
  8. _verstr = "0.1"
  9. mainmenu = false
  10. num = math.random(2)
  11. if num == 1 then turn = "O" elseif num == 2 then turn = "X" end
  12. slot1 = ""
  13. slot2 = ""
  14. slot3 = ""
  15. slot4 = ""
  16. slot5 = ""
  17. slot6 = ""
  18. slot7 = ""
  19. slot8 = ""
  20. slot9 = ""
  21.  
  22. --Funktionen
  23. function clear()
  24.     term.setCursorPos(1,1)
  25.     term.setBackgroundColor(colors.black)
  26.     term.setTextColor(colors.white)
  27.     term.clear()
  28. end
  29.  
  30. function drawMenu()
  31.     clear()
  32.     term.setBackgroundColor(colors.lime)
  33.     term.setCursorPos(24,6)
  34.     term.write("Start")
  35.     term.setCursorPos(24,8)
  36.     term.write("Exit")
  37.     mainmenu = true
  38.     while mainmenu do
  39.         local event, button, x, y = os.pullEventRaw("mouse_click")
  40.         if button == 1 and x >= 24 and x <= 28 and y == 6 then
  41.             drawTable()
  42.         elseif button == 1 and x >= 24 and x <= 27 and y == 8 then
  43.             clear()
  44.             print("Thanks for playing!")
  45.             break
  46.         end
  47.     end
  48. end
  49.  
  50. function drawTable()
  51.     clear()
  52.     print(" | | ")
  53.     print("-+-+-")
  54.     print(" | | ")
  55.     print("-+-+-")
  56.     print(" | | ")
  57.     playing = true
  58.     game()
  59. end
  60.  
  61. function finish()
  62. term.setCursorPos(1,9)
  63. term.write(turn.." won!")
  64. sleep(2)
  65. clear()
  66. print("Thanks for playing!")
  67. playing = false
  68. sleep(2)
  69. os.reboot()
  70. end
  71.  
  72. function game()
  73.     while playing do
  74.         term.setCursorPos(1,7)
  75.         term.write("It is "..turn.."'s turn.")
  76.         local event, button, x, y = os.pullEventRaw("mouse_click")
  77.         if button == 1 and x == 1 and y == 1 and slot1 == "" then
  78.             slot1 = turn
  79.             term.setCursorPos(1,1)
  80.             term.write(turn)
  81.             if slot1 == turn and slot2 == turn and slot3 == turn or slot1 == turn and slot4 == turn and slot7 == turn or slot1 == turn and slot5 == turn and slot9 == turn then
  82.                 finish()
  83.                 break
  84.             else
  85.                 if turn == "X" then
  86.                     turn = "O"
  87.                 else
  88.                     turn = "X"
  89.                 end
  90.             end
  91.         elseif button == 1 and x == 3 and y == 1 and slot2 == "" then
  92.             slot2 = turn
  93.             term.setCursorPos(3,1)
  94.             term.write(turn)
  95.             if slot2 == turn and slot1 == turn and slot3 == turn or slot2 == turn and slot5 == turn and slot8 == turn then
  96.                 finish()
  97.                 break
  98.             else
  99.                 if turn == "X" then
  100.                     turn = "O"
  101.                 else
  102.                     turn = "X"
  103.                 end
  104.             end
  105.         elseif button == 1 and x == 5 and y == 1 and slot3 == "" then
  106.             slot3 = turn
  107.             term.setCursorPos(5,1)
  108.             term.write(turn)
  109.             if slot1 == turn and slot2 == turn and slot3 == turn or slot3 == turn and slot5 == turn and slot7 == turn or slot3 == turn and slot6 == turn and slot9 == turn then
  110.                 finish()
  111.                 break
  112.             else
  113.                 if turn == "X" then
  114.                     turn = "O"
  115.                 else
  116.                     turn = "X"
  117.                 end
  118.             end
  119.         elseif button == 1 and x == 1 and y == 3 and slot4 == "" then
  120.             slot4 = turn
  121.             term.setCursorPos(1,3)
  122.             term.write(turn)
  123.             if slot4 == turn and slot1 == turn and slot7 == turn then
  124.                 finish()
  125.                 break
  126.             else
  127.                 if turn == "X" then
  128.                     turn = "O"
  129.                 else
  130.                     turn = "X"
  131.                 end
  132.             end
  133.         elseif button == 1 and x == 3 and y == 3 and slot5 == "" then
  134.             slot5 = turn
  135.             term.setCursorPos(3,3)
  136.             term.write(turn)
  137.             if slot1 == turn and slot5 == turn and slot9 == turn or slot3 == turn and slot5 == turn and slot7 == turn or slot4 == turn and slot5 == turn and slot6 == turn or slot2 == turn and slot5 == turn and slot8 == turn then
  138.                 finish()
  139.                 break
  140.             else
  141.                 if turn == "X" then
  142.                     turn = "O"
  143.                 else
  144.                     turn = "X"
  145.                 end
  146.             end
  147.         elseif button == 1 and x == 5 and y == 3 and slot6 == "" then
  148.             slot6 = turn
  149.             term.setCursorPos(5,3)
  150.             term.write(turn)
  151.             if slot3 == turn and slot6 == turn and slot9 == turn or slot4 == turn and slot5 == turn and slot6 == turn then
  152.                 finish()
  153.                 break
  154.             else
  155.                 if turn == "X" then
  156.                     turn = "O"
  157.                 else
  158.                     turn = "X"
  159.                 end
  160.             end
  161.         elseif button == 1 and x == 1 and y == 5 and slot7 == "" then
  162.             slot7 = turn
  163.             term.setCursorPos(1,5)
  164.             term.write(turn)
  165.             if slot1 == turn and slot4 == turn and slot7 == turn or slot3 == turn and slot5 == turn and slot7 == turn or slot7 == turn and slot8 == turn and slot9 == turn then
  166.                 finish()
  167.                 break
  168.             else
  169.                 if turn == "X" then
  170.                     turn = "O"
  171.                 else
  172.                     turn = "X"
  173.                 end
  174.             end
  175.         elseif button == 1 and x == 3 and y == 5 and slot8 == "" then
  176.             slot8 = turn
  177.             term.setCursorPos(3,5)
  178.             term.write(turn)
  179.             if slot7 == turn and slot8 == turn and slot9 == turn or slot2 == turn and slot5 == turn and slot8 == turn then
  180.                 finish()
  181.                 break
  182.             else
  183.                 if turn == "X" then
  184.                     turn = "O"
  185.                 else
  186.                     turn = "X"
  187.                 end
  188.             end
  189.         elseif button == 1 and x == 5 and y == 5 and slot9 == "" then
  190.             slot9 = turn
  191.             term.setCursorPos(5,5)
  192.             term.write(turn)
  193.             if slot7 == turn and slot8 == turn and slot9 == turn or slot3 == turn and slot6 == turn and slot9 == turn or slot1 == turn and slot5 == turn and slot9 == turn then
  194.                 finish()
  195.                 break
  196.             else
  197.                 if turn == "X" then
  198.                     turn = "O"
  199.                 else
  200.                     turn = "X"
  201.                 end
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. --Code
  208. clear()
  209. drawMenu()
Advertisement
Add Comment
Please, Sign In to add comment