KaoSDlanor

add (for chess game)

Nov 22nd, 2012
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.96 KB | None | 0 0
  1. --[CLASSES]
  2. local tPieces=setmetatable({},{__call=function(t,x,y)
  3.     for num,tPiece in pairs(t) do
  4.         if tPiece.x==x and tPiece.y==y then return tPiece,num end
  5.     end
  6.     return {sym=' '}
  7. end})
  8.  
  9. board=function()
  10.     return tPieces
  11. end
  12.  
  13. R=function(x,y,owner)
  14.     owner=owner or false
  15.     return setmetatable({x=x,y=y,col=owner,sym='R',hasMoved=false},{__call=function(t)
  16.         local tOpts={}
  17.         for a=1,t.x-1 do
  18.             if tPieces(t.x-a,t.y).col~=t.col then table.insert(tOpts,{t.x-a,t.y}) end
  19.             if tPieces(t.x-a,t.y).x then break end
  20.         end
  21.         for a=1,8-t.x do
  22.             if tPieces(t.x+a,t.y).col~=t.col then table.insert(tOpts,{t.x+a,t.y}) end
  23.             if tPieces(t.x+a,t.y).x then break end
  24.         end
  25.         for a=1,t.y-1 do
  26.             if tPieces(t.x,t.y-a).col~=t.col then table.insert(tOpts,{t.x,t.y-a}) end
  27.             if tPieces(t.x,t.y-a).x then break end
  28.         end
  29.         for a=1,8-t.y do
  30.             if tPieces(t.x,t.y+a).col~=t.col then table.insert(tOpts,{t.x,t.y+a}) end
  31.             if tPieces(t.x,t.y+a).x then break end
  32.         end
  33.         return tOpts
  34.     end})
  35. end
  36.  
  37. B=function(x,y,owner)
  38.     owner=owner or false
  39.     return setmetatable({x=x,y=y,col=owner,sym='B',hasMoved=false},{__call=function(t)
  40.         local tOpts={}
  41.         for num=1,math.min(t.x,t.y)-1 do
  42.             if tPieces(t.x-num,t.y-num).col~=t.col then table.insert(tOpts,{t.x-num,t.y-num}) end
  43.             if tPieces(t.x-num,t.y-num).x then break end
  44.         end
  45.         for num=1,math.min(8-t.x,8-t.y) do
  46.             if tPieces(t.x+num,t.y+num).col~=t.col then table.insert(tOpts,{t.x+num,t.y+num}) end
  47.             if tPieces(t.x+num,t.y+num).x then break end
  48.         end
  49.         for num=1,math.min(9-t.x,t.y)-1 do
  50.             if tPieces(t.x+num,t.y-num).col~=t.col then table.insert(tOpts,{t.x+num,t.y-num}) end
  51.             if tPieces(t.x+num,t.y-num).x then break end
  52.         end
  53.         for num=1,math.min(t.x,9-t.y)-1 do
  54.             if tPieces(t.x-num,t.y+num).col~=t.col then table.insert(tOpts,{t.x-num,t.y+num}) end
  55.             if tPieces(t.x-num,t.y+num).x then break end
  56.         end
  57.         return tOpts
  58.     end})
  59. end
  60.  
  61. Q=function(x,y,owner)
  62.     owner=owner or false
  63.     return setmetatable({x=x,y=y,col=owner,sym='Q',hasMoved=false},{__call=function(t)
  64.         local tOpts={}
  65.         for a=1,t.x-1 do
  66.             if tPieces(t.x-a,t.y).col~=t.col then table.insert(tOpts,{t.x-a,t.y}) end
  67.             if tPieces(t.x-a,t.y).x then break end
  68.         end
  69.         for a=1,8-t.x do
  70.             if tPieces(t.x+a,t.y).col~=t.col then table.insert(tOpts,{t.x+a,t.y}) end
  71.             if tPieces(t.x+a,t.y).x then break end
  72.         end
  73.         for a=1,t.y-1 do
  74.             if tPieces(t.x,t.y-a).col~=t.col then table.insert(tOpts,{t.x,t.y-a}) end
  75.             if tPieces(t.x,t.y-a).x then break end
  76.         end
  77.         for a=1,8-t.y do
  78.             if tPieces(t.x,t.y+a).col~=t.col then table.insert(tOpts,{t.x,t.y+a}) end
  79.             if tPieces(t.x,t.y+a).x then break end
  80.         end
  81.         for num=1,math.min(t.x,t.y)-1 do
  82.             if tPieces(t.x-num,t.y-num).col~=t.col then table.insert(tOpts,{t.x-num,t.y-num}) end
  83.             if tPieces(t.x-num,t.y-num).x then break end
  84.         end
  85.         for num=1,math.min(8-t.x,8-t.y) do
  86.             if tPieces(t.x+num,t.y+num).col~=t.col then table.insert(tOpts,{t.x+num,t.y+num}) end
  87.             if tPieces(t.x+num,t.y+num).x then break end
  88.         end
  89.         for num=1,math.min(9-t.x,t.y)-1 do
  90.             if tPieces(t.x+num,t.y-num).col~=t.col then table.insert(tOpts,{t.x+num,t.y-num}) end
  91.             if tPieces(t.x+num,t.y-num).x then break end
  92.         end
  93.         for num=1,math.min(t.x,9-t.y)-1 do
  94.             if tPieces(t.x-num,t.y+num).col~=t.col then table.insert(tOpts,{t.x-num,t.y+num}) end
  95.             if tPieces(t.x-num,t.y+num).x then break end
  96.         end
  97.         return tOpts
  98.     end})
  99. end
  100.  
  101. K=function(x,y,owner)
  102.     owner=owner or false
  103.     return setmetatable({x=x,y=y,col=owner,sym='K',hasMoved=false},{__call=function(t)
  104.         local tOpts={}
  105.         for a=-1,1,1 do
  106.             for b=-1,1,1 do
  107.                 if not (a==0 and b==0) and t.x+a>0 and t.x+a<=8 and t.y+b>0 and t.y+b<=8 and tPieces(t.x+a,t.y+b).col~=t.col then
  108.                     table.insert(tOpts,{t.x+a,t.y+b})
  109.                 end
  110.             end
  111.         end
  112.         return tOpts
  113.     end})
  114. end
  115.  
  116. H=function(x,y,owner)
  117.     owner=owner or false
  118.     return setmetatable({x=x,y=y,col=owner,sym='H',hasMoved=false},{__call=function(t)
  119.         local tOpts={}
  120.         for num,to in pairs({{t.x+1,t.y+2},{t.x+1,t.y-2},{t.x-1,t.y+2},{t.x-1,t.y-2},{t.x+2,t.y+1},{t.x+2,t.y-1},{t.x-2,t.y+1},{t.x-2,t.y-1}}) do
  121.             if tPieces(unpack(to)).col~=t.col and to[1]<=8 and to[1]>=1 and to[2]<=8 and to[2]>=1 then
  122.                 table.insert(tOpts,to)
  123.             end
  124.         end
  125.         return tOpts
  126.     end})
  127. end
  128.  
  129. P=function(x,y,owner)
  130.     owner=owner or false
  131.     return setmetatable({x=x,y=y,col=owner,sym='P',hasMoved=false},{__call=function(t)
  132.         local tOpts={}
  133.         local target=tPieces(t.x,t.y+(t.col and -1 or 1))
  134.         if not target.x and t.y+(t.col and -1 or 1)>0 and t.y+(t.col and -1 or 1)<=8 then table.insert(tOpts,{t.x,t.y+(t.col and -1 or 1)}) end
  135.         for a=-1,1,2 do
  136.             local target=tPieces(t.x+a,t.y+(t.col and -1 or 1))
  137.             if target.col~=t.col and target.x then table.insert(tOpts,{t.x+a,t.y+(t.col and -1 or 1)}) end
  138.         end
  139.         if not t.hasMoved and not tPieces(t.x,t.y+(t.col and -1 or 1)).x and not tPieces(t.x,t.y+(t.col and -2 or 2)).x and t.y+(t.col and -2 or 2)>0 and t.y+(t.col and -2 or 2)<=8 then table.insert(tOpts,{t.x,t.y+(t.col and -2 or 2)}) end
  140.         return tOpts
  141.     end})
  142. end
Advertisement
Add Comment
Please, Sign In to add comment