Advertisement
xerpi

3DnRaya check

Jul 14th, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.62 KB | None | 0 0
  1.     -----Check winner or tie ----
  2.                 function board.check_all()
  3.                     local c_turn = board.player[board.turn].st --Save the str of the current player in a variable
  4.                     --Fisrt check winner
  5.                     for i = board.row, board.row do
  6.                         if board.check._2D_horizontal(c_turn,i) or board.check._2D_vertical(c_turn,i) or --2D
  7.                             board.check._2D_diagonal_inc(c_turn,i) or board.check._2D_diagonal_dec(c_turn,i) or --2D DIAGONALS
  8.                             board.check._3D_horizontal_up(c_turn,i) or board.check._3D_vertical_up(c_turn,i) or --3d UP
  9.                             board.check._3D_horizontal_down(c_turn,i) or board.check._3D_vertical_down(c_turn,i) or --3D DOWN
  10.                             board.check._3D_diagonal_inc_up(c_turn,i) or board.check._3D_diagonal_dec_up(c_turn,i) or --3D DIAGONALS UP
  11.                             board.check._3D_diagonal_inc_down(c_turn,i) or board.check._3D_diagonal_dec_down(c_turn,i) or --3D DIAGONALS DOWN
  12.                             board.check._3D_vertical(c_turn,i) --3D VERTICAL
  13.                            
  14.                             then
  15.                             board.win = board.turn
  16.                         else board.win = false
  17.                         end                    
  18.                     end
  19.                 end
  20.  
  21. ------------------------------------2D FUNCTIONS------------------------------------               
  22.     ----- 2D-Check vertical ----   
  23.                 function board.check._2D_vertical(str,n)
  24.                     local cont, tab = 1,{}
  25.                     for d = 1, board.depth do  --The depth
  26.                         for x = 1, board.width do
  27.                             for y = 1, board.height do
  28.                                 tab ={} --Empty the tab
  29.                                 cont = 1  --Reset the counter
  30.                                 if board.table[d][y][x] == str then  --If the current chip is the
  31.                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  32.                                     for i = 1, n do
  33.                                         if board.inside(x,y+i) and board.table[d][y+i][x] == str then
  34.                                             cont = cont +1
  35.                                             table.insert(tab,{x=x,y=y+i,d=d})
  36.                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  37.                                         if cont >= n then
  38.                                             board.winner_row.vars.initx = x
  39.                                             board.winner_row.vars.inity = y
  40.                                             board.winner_row.vars.initd = d
  41.                                             board.winner_row.vars.finald = d                                           
  42.                                             board.winner_row.vars.finalx = x
  43.                                             board.winner_row.vars.finaly = y+i
  44.                                             board.winner_row.row = tab
  45.                                             board.winner_row.vars.how = "vertical"
  46.                                             return true
  47.                                         end
  48.                                     end                                                        
  49.                                 end            
  50.                             end
  51.                         end
  52.                     end
  53.                 end
  54.            
  55.     -----2D-Check horizontal ----  
  56.                 function board.check._2D_horizontal(str,n)
  57.                     local cont, tab = 1,{}
  58.                     for d = 1, board.depth do  --The depth
  59.                         for y = 1, board.height do
  60.                             for x = 1, board.width do
  61.                                 tab ={} --Empty the tab
  62.                                 cont = 1  --Reset the counter
  63.                                 if board.table[d][y][x] == str then  --If the current chip is the
  64.                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  65.                                     for i = 1, n do
  66.                                         if board.inside(x+i,y) and board.table[d][y][x+i] == str then
  67.                                             cont = cont +1
  68.                                             table.insert(tab,{x=x+i,y=y,d=d})
  69.                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  70.                                         if cont >= n then
  71.                                             board.winner_row.vars.initx = x
  72.                                             board.winner_row.vars.inity = y
  73.                                             board.winner_row.vars.initd = d
  74.                                             board.winner_row.vars.finald = d                                           
  75.                                             board.winner_row.vars.finalx = x+i
  76.                                             board.winner_row.vars.finaly = y
  77.                                             board.winner_row.row = tab
  78.                                             board.winner_row.vars.how = "horizontal"
  79.                                             return true
  80.                                         end
  81.                                     end                                                        
  82.                                 end            
  83.                             end
  84.                         end
  85.                     end
  86.                 end                
  87.                
  88.     -----2D-Check diagonal with y increasing ----  
  89.                 function board.check._2D_diagonal_inc(str,n)
  90.                     local cont, tab = 1,{}
  91.                     for d = 1, board.depth do  --The depth
  92.                         for y = 1, board.height do
  93.                             for x = 1, board.width do
  94.                                 tab ={} --Empty the tab
  95.                                 cont = 1  --Reset the counter
  96.                                 if board.table[d][y][x] == str then  --If the current chip is the
  97.                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  98.                                     for i = 1, n do
  99.                                         if board.inside(x+i,y+i) and board.table[d][y+i][x+i] == str then
  100.                                             cont = cont +1
  101.                                             table.insert(tab,{x=x+i,y=y+i,d=d})
  102.                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  103.                                         if cont >= n then
  104.                                             board.winner_row.vars.initx = x
  105.                                             board.winner_row.vars.inity = y
  106.                                             board.winner_row.vars.initd = d
  107.                                             board.winner_row.vars.finald = d                                           
  108.                                             board.winner_row.vars.finalx = x+i
  109.                                             board.winner_row.vars.finaly = y+i
  110.                                             board.winner_row.row = tab
  111.                                             board.winner_row.vars.how = "diagonal_inc"
  112.                                             return true
  113.                                         end
  114.                                     end                                                        
  115.                                 end            
  116.                             end
  117.                         end
  118.                     end
  119.                 end
  120.                
  121.     -----2D-Check diagonal with y decreasing ----  
  122.                 function board.check._2D_diagonal_dec(str,n)
  123.                     local cont, tab = 1,{}
  124.                     for d = 1, board.depth do  --The depth
  125.                         for y = 1, board.height do
  126.                             for x = 1, board.width do
  127.                                 tab ={} --Empty the tab
  128.                                 cont = 1  --Reset the counter
  129.                                 if board.table[d][y][x] == str then  --If the current chip is the
  130.                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  131.                                     for i = 1, n do
  132.                                         if board.inside(x+i,y-i) and board.table[d][y-i][x+i] == str then
  133.                                             cont = cont +1
  134.                                             table.insert(tab,{x=x+i,y=y-i,d=d})
  135.                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  136.                                         if cont >= n then
  137.                                             board.winner_row.vars.initx = x
  138.                                             board.winner_row.vars.inity = y
  139.                                             board.winner_row.vars.initd = d
  140.                                             board.winner_row.vars.finald = d                                           
  141.                                             board.winner_row.vars.finalx = x+i
  142.                                             board.winner_row.vars.finaly = y-i
  143.                                             board.winner_row.row = tab
  144.                                             board.winner_row.vars.how = "diagonal_dec"
  145.                                             return true
  146.                                         end
  147.                                     end                                                        
  148.                                 end            
  149.                             end
  150.                         end
  151.                     end
  152.                 end                
  153.  
  154. ------------------------------------3D FUNCTIONS------------------------------------   
  155. ----------------------  UP         
  156.                     ----- 3D-Check vertical UP---- 
  157.                                 function board.check._3D_vertical_up(str,n)
  158.                                     local cont, tab = 1,{}
  159.                                     for d = 1, board.depth do  --The depth
  160.                                         for x = 1, board.width do
  161.                                             for y = 1, board.height do
  162.                                                 tab ={} --Empty the tab
  163.                                                 cont = 1  --Reset the counter
  164.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  165.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  166.                                                     for i = 1, n do
  167.                                                         if board.inside(x,y+i,d+i) and board.table[d+i][y+i][x] == str then
  168.                                                             cont = cont +1
  169.                                                             table.insert(tab,{x=x,y=y+i,d=d+i})
  170.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  171.                                                         if cont >= n then
  172.                                                             board.winner_row.vars.initx = x
  173.                                                             board.winner_row.vars.inity = y
  174.                                                             board.winner_row.vars.initd = d
  175.                                                             board.winner_row.vars.finald = d+i
  176.                                                             board.winner_row.vars.finalx = x
  177.                                                             board.winner_row.vars.finaly = y+i
  178.                                                             board.winner_row.row = tab
  179.                                                             board.winner_row.vars.how = "vertical"
  180.                                                             return true
  181.                                                         end
  182.                                                     end                                                        
  183.                                                 end            
  184.                                             end
  185.                                         end
  186.                                     end
  187.                                 end
  188.                                
  189.                     -----3D-Check horizontal UP----
  190.                                 function board.check._3D_horizontal_up(str,n)
  191.                                     local cont, tab = 1,{}
  192.                                     for d = 1, board.depth do  --The depth
  193.                                         for y = 1, board.height do
  194.                                             for x = 1, board.width do
  195.                                                 tab ={} --Empty the tab
  196.                                                 cont = 1  --Reset the counter
  197.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  198.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  199.                                                     for i = 1, n do
  200.                                                         if board.inside(x+i,y,d+i) and board.table[d+i][y][x+i] == str then
  201.                                                             cont = cont +1
  202.                                                             table.insert(tab,{x=x+i,y=y,d=d+i})
  203.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  204.                                                         if cont >= n then
  205.                                                             board.winner_row.vars.initx = x
  206.                                                             board.winner_row.vars.inity = y
  207.                                                             board.winner_row.vars.initd = d
  208.                                                             board.winner_row.vars.finald = d+i                                         
  209.                                                             board.winner_row.vars.finalx = x+i
  210.                                                             board.winner_row.vars.finaly = y
  211.                                                             board.winner_row.row = tab
  212.                                                             board.winner_row.vars.how = "horizontal"
  213.                                                             return true
  214.                                                         end
  215.                                                     end                                                        
  216.                                                 end            
  217.                                             end
  218.                                         end
  219.                                     end
  220.                                 end
  221.                                
  222.                     -----3D-Check diagonal with y increasing UP----
  223.                                 function board.check._3D_diagonal_inc_up(str,n)
  224.                                     local cont, tab = 1,{}
  225.                                     for d = 1, board.depth do  --The depth
  226.                                         for y = 1, board.height do
  227.                                             for x = 1, board.width do
  228.                                                 tab ={} --Empty the tab
  229.                                                 cont = 1  --Reset the counter
  230.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  231.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  232.                                                     for i = 1, n do
  233.                                                         if board.inside(x+i,y+i,d+i) and board.table[d+i][y+i][x+i] == str then
  234.                                                             cont = cont +1
  235.                                                             table.insert(tab,{x=x+i,y=y+i,d=d+i})
  236.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  237.                                                         if cont >= n then
  238.                                                             board.winner_row.vars.initx = x
  239.                                                             board.winner_row.vars.inity = y
  240.                                                             board.winner_row.vars.initd = d
  241.                                                             board.winner_row.vars.finald = d+i                                         
  242.                                                             board.winner_row.vars.finalx = x+i
  243.                                                             board.winner_row.vars.finaly = y+i
  244.                                                             board.winner_row.row = tab
  245.                                                             board.winner_row.vars.how = "diagonal_inc"
  246.                                                             return true
  247.                                                         end
  248.                                                     end                                                        
  249.                                                 end            
  250.                                             end
  251.                                         end
  252.                                     end
  253.                                 end
  254.  
  255.                     -----3D-Check diagonal with y decreasing UP ----   
  256.                                 function board.check._3D_diagonal_dec_up(str,n)
  257.                                     local cont, tab = 1,{}
  258.                                     for d = 1, board.depth do  --The depth
  259.                                         for y = 1, board.height do
  260.                                             for x = 1, board.width do
  261.                                                 tab ={} --Empty the tab
  262.                                                 cont = 1  --Reset the counter
  263.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  264.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  265.                                                     for i = 1, n do
  266.                                                         if board.inside(x+i,y-i,d+i) and board.table[d+i][y-i][x+i] == str then
  267.                                                             cont = cont +1
  268.                                                             table.insert(tab,{x=x+i,y=y-i,d=d+i})
  269.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  270.                                                         if cont >= n then
  271.                                                             board.winner_row.vars.initx = x
  272.                                                             board.winner_row.vars.inity = y
  273.                                                             board.winner_row.vars.initd = d
  274.                                                             board.winner_row.vars.finald = d+i                                         
  275.                                                             board.winner_row.vars.finalx = x+i
  276.                                                             board.winner_row.vars.finaly = y-i
  277.                                                             board.winner_row.row = tab
  278.                                                             board.winner_row.vars.how = "diagonal_dec"
  279.                                                             return true
  280.                                                         end
  281.                                                     end                                                        
  282.                                                 end            
  283.                                             end
  284.                                         end
  285.                                     end
  286.                                 end
  287.  
  288.                            
  289. ----------------------  DOWN               
  290.                     ----- 3D-Check vertical DOWN----   
  291.                                 function board.check._3D_vertical_down(str,n)
  292.                                     local cont, tab = 1,{}
  293.                                     for d = board.depth,1,-1 do  --The depth
  294.                                         for x = 1, board.width do
  295.                                             for y = 1, board.height do
  296.                                                 tab ={} --Empty the tab
  297.                                                 cont = 1  --Reset the counter
  298.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  299.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  300.                                                     for i = 1, n do
  301.                                                         if board.inside(x,y+i,d-i) and board.table[d-i][y+i][x] == str then
  302.                                                             cont = cont +1
  303.                                                             table.insert(tab,{x=x,y=y+i,d=d-i})
  304.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  305.                                                         if cont >= n then
  306.                                                             board.winner_row.vars.initx = x
  307.                                                             board.winner_row.vars.inity = y
  308.                                                             board.winner_row.vars.initd = d
  309.                                                             board.winner_row.vars.finald = d-i
  310.                                                             board.winner_row.vars.finalx = x
  311.                                                             board.winner_row.vars.finaly = y+i
  312.                                                             board.winner_row.row = tab
  313.                                                             board.winner_row.vars.how = "vertical"
  314.                                                             return true
  315.                                                         end
  316.                                                     end                                                        
  317.                                                 end            
  318.                                             end
  319.                                         end
  320.                                     end
  321.                                 end
  322.                                
  323.                     -----3D-Check horizontal DOWN----  
  324.                                 function board.check._3D_horizontal_down(str,n)
  325.                                     local cont, tab = 1,{}
  326.                                     for d = board.depth,1,-1 do  --The depth
  327.                                         for y = 1, board.height do
  328.                                             for x = 1, board.width do
  329.                                                 tab ={} --Empty the tab
  330.                                                 cont = 1  --Reset the counter
  331.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  332.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  333.                                                     for i = 1, n do
  334.                                                         if board.inside(x+i,y,d-i) and board.table[d-i][y][x+i] == str then
  335.                                                             cont = cont +1
  336.                                                             table.insert(tab,{x=x+i,y=y,d=d-i})
  337.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  338.                                                         if cont >= n then
  339.                                                             board.winner_row.vars.initx = x
  340.                                                             board.winner_row.vars.inity = y
  341.                                                             board.winner_row.vars.initd = d
  342.                                                             board.winner_row.vars.finald = d-i                                         
  343.                                                             board.winner_row.vars.finalx = x+i
  344.                                                             board.winner_row.vars.finaly = y
  345.                                                             board.winner_row.row = tab
  346.                                                             board.winner_row.vars.how = "horizontal"
  347.                                                             return true
  348.                                                         end
  349.                                                     end                                                        
  350.                                                 end            
  351.                                             end
  352.                                         end
  353.                                     end
  354.                                 end                
  355.    
  356.                     -----3D-Check diagonal with y increasing DOWN----  
  357.                                 function board.check._3D_diagonal_inc_down(str,n)
  358.                                     local cont, tab = 1,{}
  359.                                     for d = board.depth,1,-1 do  --The depth
  360.                                         for y = 1, board.height do
  361.                                             for x = 1, board.width do
  362.                                                 tab ={} --Empty the tab
  363.                                                 cont = 1  --Reset the counter
  364.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  365.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  366.                                                     for i = 1, n do
  367.                                                         if board.inside(x+i,y+i,d-i) and board.table[d-i][y+i][x+i] == str then
  368.                                                             cont = cont +1
  369.                                                             table.insert(tab,{x=x+i,y=y+i,d=d-i})
  370.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  371.                                                         if cont >= n then
  372.                                                             board.winner_row.vars.initx = x
  373.                                                             board.winner_row.vars.inity = y
  374.                                                             board.winner_row.vars.initd = d
  375.                                                             board.winner_row.vars.finald = d-i                                         
  376.                                                             board.winner_row.vars.finalx = x+i
  377.                                                             board.winner_row.vars.finaly = y+i
  378.                                                             board.winner_row.row = tab
  379.                                                             board.winner_row.vars.how = "diagonal_inc"
  380.                                                             return true
  381.                                                         end
  382.                                                     end                                                        
  383.                                                 end            
  384.                                             end
  385.                                         end
  386.                                     end
  387.                                 end
  388.  
  389.                     -----3D-Check diagonal with y decreasing DOWN ---- 
  390.                                 function board.check._3D_diagonal_dec_down(str,n)
  391.                                     local cont, tab = 1,{}
  392.                                     for d = board.depth,1,-1 do  --The depth
  393.                                         for y = 1, board.height do
  394.                                             for x = 1, board.width do
  395.                                                 tab ={} --Empty the tab
  396.                                                 cont = 1  --Reset the counter
  397.                                                 if board.table[d][y][x] == str then  --If the current chip is the
  398.                                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  399.                                                     for i = 1, n do
  400.                                                         if board.inside(x+i,y-i,d-i) and board.table[d-i][y-i][x+i] == str then
  401.                                                             cont = cont +1
  402.                                                             table.insert(tab,{x=x+i,y=y-i,d=d-i})
  403.                                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  404.                                                         if cont >= n then
  405.                                                             board.winner_row.vars.initx = x
  406.                                                             board.winner_row.vars.inity = y
  407.                                                             board.winner_row.vars.initd = d
  408.                                                             board.winner_row.vars.finald = d-i                                         
  409.                                                             board.winner_row.vars.finalx = x+i
  410.                                                             board.winner_row.vars.finaly = y-i
  411.                                                             board.winner_row.row = tab
  412.                                                             board.winner_row.vars.how = "diagonal_dec"
  413.                                                             return true
  414.                                                         end
  415.                                                     end                                                        
  416.                                                 end            
  417.                                             end
  418.                                         end
  419.                                     end
  420.                                 end
  421.  
  422.  
  423.    
  424.     ----- 3D-Check vertical ----   
  425.                 function board.check._3D_vertical(str,n)
  426.                     local cont, tab = 1,{}
  427.                     for d = 1, board.depth do  --The depth
  428.                         for x = 1, board.width do
  429.                             for y = 1, board.height do
  430.                                 tab ={} --Empty the tab
  431.                                 cont = 1  --Reset the counter
  432.                                 if board.table[d][y][x] == str then  --If the current chip is the
  433.                                     table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array
  434.                                     for i = 1, n do
  435.                                         if board.inside(x,y,d+i) and board.table[d+i][y][x] == str then
  436.                                             cont = cont +1
  437.                                             table.insert(tab,{x=x,y=y,d=d+i})
  438.                                         end --If the position+i is the str then increase the cont var and insert the postion in the array
  439.                                         if cont >= n then
  440.                                             board.winner_row.vars.initx = x
  441.                                             board.winner_row.vars.inity = y
  442.                                             board.winner_row.vars.initd = d
  443.                                             board.winner_row.vars.finald = d+i
  444.                                             board.winner_row.vars.finalx = x
  445.                                             board.winner_row.vars.finaly = y
  446.                                             board.winner_row.row = tab
  447.                                             board.winner_row.vars.how = "vertical"
  448.                                             return true
  449.                                         end
  450.                                     end                                                        
  451.                                 end            
  452.                             end
  453.                         end
  454.                     end
  455.                 end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement