honeyphos1234

Tic tac toe computercraft

Jun 24th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. mon=peripheral.find("monitor")
  2. function DrawTTT()
  3.   mon.setBackgroundColor(colors.blue)
  4.   mon.clear()
  5.   mon.setBackgroundColor(colors.white)
  6.   for pos=1,5 do
  7.     mon.setCursorPos(2,pos)
  8.     mon.write("  ")
  9.   end
  10.   mon.setBackgroundColor(colors.black)
  11.   for pos=1,5 do
  12.     mon.setCursorPos(3,pos)
  13.     mon.write(" ")
  14.   end
  15.   for pos=1,5 do
  16.     mon.setCursorPos(5,pos)
  17.     mon.write(" ")
  18.   end
  19.   mon.setCursorPos(2,2)
  20.   mon.write("    ")
  21.   mon.setCursorPos(2,4)
  22.   mon.write("    ")
  23. end
  24. function play()
  25.   Z=true
  26.   mon.setBackgroundColor(colors.white)
  27.   mon.setTextColor(colors.black)
  28.   while Z do
  29.     event, Ts, Tx, Ty=os.pullEvent(monitor_touch)
  30.     if Tx==2 and Ty==1 and L1==" " then
  31.       L1=player
  32.       mon.setCursorPos(2,1)
  33.       mon.write(player)
  34.       Z=false
  35.     elseif Tx==4 and Ty==1 and L2==" " then
  36.       L2=player
  37.       mon.setCursorPos(4,1)
  38.       mon.write(player)
  39.       Z=false
  40.     elseif Tx==6 and Ty==1 and L3==" " then
  41.       L3=player
  42.       mon.setCursorPos(6,1)
  43.       mon.write(player)
  44.       Z=false
  45.     elseif Tx==2 and Ty==3 and L4==" " then
  46.       L4=player
  47.       mon.setCursorPos(2,3)
  48.       mon.write(player)
  49.       Z=false
  50.     elseif Tx==4 and Ty==3 and L5==" " then
  51.       L5=player
  52.       mon.setCursorPos(4,3)
  53.       mon.write(player)
  54.       Z=false
  55.     elseif Tx==6 and Ty==3 and L6==" " then
  56.       L6=player
  57.       mon.setCursorPos(6,3)
  58.       mon.write(player)
  59.       Z=false
  60.     elseif Tx==2 and Ty==5 and L7==" " then
  61.       L7=player
  62.       mon.setCursorPos(2,5)
  63.       mon.write(player)
  64.       Z=false
  65.     elseif Tx==4 and Ty==5 and L8==" " then
  66.       L8=player
  67.       mon.setCursorPos(4,5)
  68.       mon.write(player)
  69.       Z=false
  70.     elseif Tx==6 and Ty==5 and L9==" " then
  71.       L9=player
  72.       mon.setCursorPos(6,5)
  73.       mon.write(player)
  74.       Z=false
  75.     end
  76.     os.sleep(1)
  77.   end
  78. end
  79. function check()
  80.   mon.setBackgroundColor(colors.lime)
  81.   Won=true
  82.   if L1==player and L2==player and L3==player then
  83.     mon.setCursorPos(2,1)
  84.     mon.write("  ")
  85.     playing=false
  86.   elseif L4==player and L5==player and L6==player then
  87.     mon.setCursorPos(2,3)
  88.     mon.write("  ")
  89.     playing=false
  90.   elseif L7==player and L8==player and L9==player then
  91.     mon.setCusorPos(2,5)
  92.     mon.write("  ")
  93.     playing=false
  94.   elseif L1==player and L4==player and L7==player then
  95.     for pos=1,5 do
  96.       mon.setCursorPos(2,pos)
  97.       mon.write(" ")
  98.     end
  99.     playing=false
  100.   elseif L2==player and L5==player and L8==player then
  101.     for pos=1,5 do
  102.       mon.setCursorPos(4,pos)
  103.       mon.write(" ")
  104.     end
  105.     playing=false
  106.   elseif L3==player and L6==player and L9==player then
  107.     for pos=1,5 do
  108.       mon.setCursorPos(6,pos)
  109.       mon.write(" ")
  110.     end
  111.     playing=false
  112.   elseif L1==player and L5==player and L9==player then
  113.     for pos=1,5 do
  114.       mon.setCursorPos(pos+1,pos)
  115.       mon.write(" ")
  116.     end
  117.     playing=false
  118.   elseif L3==player and L5==player and L7==player then
  119.     for pos=0,4 do
  120.       mon.setCursorPos(6-pos,pos+1)
  121.       mon.write(" ")
  122.     end
  123.     playing=false
  124.   else
  125.     Won=false
  126.   end
  127.   if Won then
  128.     os.pullEvent(monitor_touch)
  129.   end
  130. end
  131. CP=true
  132. while CP do
  133.   mon.setTextScale(1)
  134.   DrawTTT()
  135.   player="X"
  136.   L1=" "
  137.   L2=" "
  138.   L3=" "
  139.   L4=" "
  140.   L5=" "
  141.   L6=" "
  142.   L7=" "
  143.   L8=" "
  144.   L9=" "
  145.   playing=true
  146.   while playing do
  147.     play()
  148.     check()
  149.     if player=="X" then
  150.       player="O"
  151.     elseif player=="O" then
  152.       player="X"
  153.     end
  154.     if L1==" " or L2==" " or L3==" " or L4==" " or L5==" " or L6==" " or L7==" " or L8==" " or L9==" " then
  155.     elseif not Won then
  156.       playing=false
  157.     end
  158.   end
  159.   if Won then
  160.     if player=="X" then
  161.       player="O"
  162.     elseif player=="O" then
  163.       player="X"
  164.     end
  165.     mon.setBackgroundColor(colors.black)
  166.     mon.setTextColor(colors.white)
  167.     mon.clear()
  168.     mon.setCursorPos(1,1)
  169.     mon.write("player")
  170.     mon.write(player)
  171.     mon.setCursorPos(3,3)
  172.     mon.write("WON")
  173.     mon.setCursorPos(1,5)
  174.     mon.write("TheGame")
  175.   end
  176.   os.pullEvent(monitor_touch)
  177.   mon.setBackgroundColor(colors.black)
  178.   mon.setTextColor(colors.white)
  179.   mon.clear()
  180.   mon.setCursorPos(2,1)
  181.   mon.write("Play?")
  182.   mon.setCursorPos(1,3)
  183.   mon.write("yes")
  184.   mon.setCursorPos(1,5)
  185.   mon.write("no")
  186.   mon.setBackgroundColor(colors.lime)
  187.   mon.setCursorPos(5,3)
  188.   mon.write("  ")
  189.   mon.setBackgroundColor(colors.red)
  190.   mon.setCursorPos(5,5)
  191.   mon.write("  ")
  192.   click=false
  193.   while not click do
  194.     event, Ts, Tx, Ty=os.pullEvent(monitor_touch)
  195.     if Ty==3 then
  196.       if Tx==5 or Tx==6 then
  197.         click=true
  198.         CP=true
  199.       end
  200.     elseif Ty==5 then
  201.       if Tx==5 or Tx==6 then
  202.         click=true
  203.         CP=false
  204.       end
  205.     end
  206.   end
  207.   if not CP then
  208.     mon.setBackgroundColor(colors.black)
  209.     mon.clear()
  210.   end
  211. end
Add Comment
Please, Sign In to add comment