-----Check winner or tie ---- function board.check_all() local c_turn = board.player[board.turn].st --Save the str of the current player in a variable --Fisrt check winner for i = board.row, board.row do if board.check._2D_horizontal(c_turn,i) or board.check._2D_vertical(c_turn,i) or --2D board.check._2D_diagonal_inc(c_turn,i) or board.check._2D_diagonal_dec(c_turn,i) or --2D DIAGONALS board.check._3D_horizontal_up(c_turn,i) or board.check._3D_vertical_up(c_turn,i) or --3d UP board.check._3D_horizontal_down(c_turn,i) or board.check._3D_vertical_down(c_turn,i) or --3D DOWN board.check._3D_diagonal_inc_up(c_turn,i) or board.check._3D_diagonal_dec_up(c_turn,i) or --3D DIAGONALS UP board.check._3D_diagonal_inc_down(c_turn,i) or board.check._3D_diagonal_dec_down(c_turn,i) or --3D DIAGONALS DOWN board.check._3D_vertical(c_turn,i) --3D VERTICAL then board.win = board.turn else board.win = false end end end ------------------------------------2D FUNCTIONS------------------------------------ ----- 2D-Check vertical ---- function board.check._2D_vertical(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for x = 1, board.width do for y = 1, board.height do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x,y+i) and board.table[d][y+i][x] == str then cont = cont +1 table.insert(tab,{x=x,y=y+i,d=d}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d board.winner_row.vars.finalx = x board.winner_row.vars.finaly = y+i board.winner_row.row = tab board.winner_row.vars.how = "vertical" return true end end end end end end end -----2D-Check horizontal ---- function board.check._2D_horizontal(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y) and board.table[d][y][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y,d=d}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y board.winner_row.row = tab board.winner_row.vars.how = "horizontal" return true end end end end end end end -----2D-Check diagonal with y increasing ---- function board.check._2D_diagonal_inc(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y+i) and board.table[d][y+i][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y+i,d=d}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y+i board.winner_row.row = tab board.winner_row.vars.how = "diagonal_inc" return true end end end end end end end -----2D-Check diagonal with y decreasing ---- function board.check._2D_diagonal_dec(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y-i) and board.table[d][y-i][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y-i,d=d}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y-i board.winner_row.row = tab board.winner_row.vars.how = "diagonal_dec" return true end end end end end end end ------------------------------------3D FUNCTIONS------------------------------------ ---------------------- UP ----- 3D-Check vertical UP---- function board.check._3D_vertical_up(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for x = 1, board.width do for y = 1, board.height do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x,y+i,d+i) and board.table[d+i][y+i][x] == str then cont = cont +1 table.insert(tab,{x=x,y=y+i,d=d+i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d+i board.winner_row.vars.finalx = x board.winner_row.vars.finaly = y+i board.winner_row.row = tab board.winner_row.vars.how = "vertical" return true end end end end end end end -----3D-Check horizontal UP---- function board.check._3D_horizontal_up(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y,d+i) and board.table[d+i][y][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y,d=d+i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d+i board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y board.winner_row.row = tab board.winner_row.vars.how = "horizontal" return true end end end end end end end -----3D-Check diagonal with y increasing UP---- function board.check._3D_diagonal_inc_up(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y+i,d+i) and board.table[d+i][y+i][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y+i,d=d+i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d+i board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y+i board.winner_row.row = tab board.winner_row.vars.how = "diagonal_inc" return true end end end end end end end -----3D-Check diagonal with y decreasing UP ---- function board.check._3D_diagonal_dec_up(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y-i,d+i) and board.table[d+i][y-i][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y-i,d=d+i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d+i board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y-i board.winner_row.row = tab board.winner_row.vars.how = "diagonal_dec" return true end end end end end end end ---------------------- DOWN ----- 3D-Check vertical DOWN---- function board.check._3D_vertical_down(str,n) local cont, tab = 1,{} for d = board.depth,1,-1 do --The depth for x = 1, board.width do for y = 1, board.height do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x,y+i,d-i) and board.table[d-i][y+i][x] == str then cont = cont +1 table.insert(tab,{x=x,y=y+i,d=d-i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d-i board.winner_row.vars.finalx = x board.winner_row.vars.finaly = y+i board.winner_row.row = tab board.winner_row.vars.how = "vertical" return true end end end end end end end -----3D-Check horizontal DOWN---- function board.check._3D_horizontal_down(str,n) local cont, tab = 1,{} for d = board.depth,1,-1 do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y,d-i) and board.table[d-i][y][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y,d=d-i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d-i board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y board.winner_row.row = tab board.winner_row.vars.how = "horizontal" return true end end end end end end end -----3D-Check diagonal with y increasing DOWN---- function board.check._3D_diagonal_inc_down(str,n) local cont, tab = 1,{} for d = board.depth,1,-1 do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y+i,d-i) and board.table[d-i][y+i][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y+i,d=d-i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d-i board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y+i board.winner_row.row = tab board.winner_row.vars.how = "diagonal_inc" return true end end end end end end end -----3D-Check diagonal with y decreasing DOWN ---- function board.check._3D_diagonal_dec_down(str,n) local cont, tab = 1,{} for d = board.depth,1,-1 do --The depth for y = 1, board.height do for x = 1, board.width do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x+i,y-i,d-i) and board.table[d-i][y-i][x+i] == str then cont = cont +1 table.insert(tab,{x=x+i,y=y-i,d=d-i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d-i board.winner_row.vars.finalx = x+i board.winner_row.vars.finaly = y-i board.winner_row.row = tab board.winner_row.vars.how = "diagonal_dec" return true end end end end end end end ----- 3D-Check vertical ---- function board.check._3D_vertical(str,n) local cont, tab = 1,{} for d = 1, board.depth do --The depth for x = 1, board.width do for y = 1, board.height do tab ={} --Empty the tab cont = 1 --Reset the counter if board.table[d][y][x] == str then --If the current chip is the table.insert(tab,{x=x,y=y,d=d}) --Insert the postion in the array for i = 1, n do if board.inside(x,y,d+i) and board.table[d+i][y][x] == str then cont = cont +1 table.insert(tab,{x=x,y=y,d=d+i}) end --If the position+i is the str then increase the cont var and insert the postion in the array if cont >= n then board.winner_row.vars.initx = x board.winner_row.vars.inity = y board.winner_row.vars.initd = d board.winner_row.vars.finald = d+i board.winner_row.vars.finalx = x board.winner_row.vars.finaly = y board.winner_row.row = tab board.winner_row.vars.how = "vertical" return true end end end end end end end