Guest User

ttt.lua

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