Advertisement
astral17

tic tac toe(old)

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