Guest User

ttt.lua

a guest
Oct 8th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. --tic tac toe
  2. ----SETTINGS----
  3. size=9 --size of field
  4. len=5  --length of line which you must solve
  5. ----------------
  6. tex={
  7.  x={"▄   ▄",
  8.     " ▀▄▀ ",
  9.     "▄▀ ▀▄",
  10.  },
  11.  o={"  ▄  ",
  12.     "▄▀ ▀▄",
  13.     " ▀▄▀ "
  14.  }
  15. }
  16. isEnded=false
  17. turn="x"
  18. nextTurn={x="o",o="x"}
  19. local event=require("event")
  20. local gpu=require("component").gpu
  21. map={}
  22.  
  23. for i=1,size*size do
  24.   map[i]=" "
  25. end
  26. function toXY(num)
  27.   local y=math.floor((num-1)/size)+1
  28.   return num-(y-1)*size,y
  29. end
  30. function fromXY(x,y)
  31.   return (y-1)*size+x
  32. end
  33. function win(s)
  34.   gpu.set(90,1,"WIN: "..s)
  35.   isEnded=true
  36. end
  37. function draw(name,x,y)
  38.   if name=="x" then
  39.     gpu.setForeground(0x0000ff)
  40.   elseif name=="o" then
  41.     gpu.setForeground(0xff0000)
  42.   else
  43.     return
  44.   end
  45.   for i=1,3 do
  46.     gpu.set(x,y+i-1,tex[name][i])
  47.   end
  48.   gpu.setForeground(0xffffff)
  49. end
  50. function drawField()
  51.   for y=1,size do
  52.     if y~=size then
  53.       gpu.set(1,y*4,string.rep("▄",8*size-1))
  54. --      gpu.set(y*9,1,string.rep("█",4*size),true)
  55.     end
  56.     for x=1,size do
  57.       draw(map[fromXY(x,y)],(x-1)*8+2,(y-1)*4+1)
  58.     end
  59.   end
  60.   for y=1,size-1 do
  61.       gpu.set(y*8,1,string.rep("█",4*size),true)
  62.   end
  63. end
  64. require("term").clear()
  65. drawField()
  66. --os.exit()
  67.  
  68. function onClick(_,_,x,y,_,name)
  69.   if (turn=="o")and(name~="astral17") then
  70.     return
  71.   end
  72.   if isEnded then
  73.     return false
  74.   end
  75.   xC=math.floor(x/8)+1
  76.   yC=math.floor((y-1)/4)+1
  77. --  gpu.set(1,1,xC.." "..yC)
  78.   if (xC>size)or(yC>size) then
  79.     return
  80.   end
  81.   num=fromXY(xC,yC)
  82.   if map[num]~=" " then
  83.     return
  84.   end
  85.   map[num]=turn
  86.   for i=1,size do
  87.     for j=1,size do
  88.       if (i<=size-len+1)and(j<=size-len+1) then
  89.         bool1,bool2=true,true
  90.         s=map[fromXY(i,j)]
  91.         s2=map[fromXY(i+len-1,j)]
  92.         if s==" " then
  93.           bool1=false
  94.         end
  95.         if s2==" " then
  96.           bool2=false
  97.         end
  98.         for x=0,len-1 do
  99.           if map[fromXY(i+x,j+x)]~=s then
  100.             bool1=false
  101.           end
  102. --          io.write(i+len-x-1,j+x,map[fromXY(i+len-x-1,j+x)]," ")
  103.           if map[fromXY(i+len-x-1,j+x)]~=s2 then
  104.             bool2=false
  105.           end
  106.         end
  107.         if (bool1)or(bool2) then
  108.           win((bool1 and s)or(bool2 and s2))
  109.           break
  110.         end
  111.       end
  112.       if i<=size-len+1 then
  113.         bool=true
  114.         s=map[fromXY(i,j)]
  115.         if s==" " then
  116.           bool=false
  117.         end
  118.         for x=0,len-1 do
  119.           if map[fromXY(i+x,j)]~=s then
  120.             bool=false
  121.             break
  122.           end
  123.         end
  124.         if bool then
  125.           win(s)
  126.           break
  127.         end
  128.       end
  129.       if j<=size-len+1 then
  130.         bool=true
  131.         s=map[fromXY(i,j)]
  132.         if s==" " then
  133.           bool=false
  134.         end
  135.         for x=0,len-1 do
  136.           if map[fromXY(i,j+x)]~=s then
  137.             bool=false
  138.             break
  139.           end
  140.         end
  141.         if bool then
  142.           win(s)
  143.           break
  144.         end
  145.       end
  146.     end
  147.   end
  148.   turn=nextTurn[turn]
  149.   drawField()
  150. --  gpu.set(1,1,turn)
  151. end
  152.  
  153. event.listen("touch",onClick)
  154. while true do
  155.   _,_,_,_,name=event.pull("key_down")
  156.   if name=="astral17" then
  157.     break
  158.   end
  159. end
  160. event.ignore("touch",onClick)
Advertisement
Add Comment
Please, Sign In to add comment