Here, this is lua code which should make it clear, the board has been squished from a '2d' table (array) into a 1d array of 'X', 'O' and 'EMPTY' strings. so if we had
XXO
OXO
OOX
it would turn into XXOOXOOX from here we just check wether the tokens are in a ceartin order. continuing on with the above example testing for a X win, it is obvious from the board that X will win on a diagonal, the squares involved in this win have the location in a 2d array of 1,1 + 2, 2 + 3, 3. these dimensions equate to 1,5,9 in the 1d representation XXOOXOOX. if you looks below at the wins table inside it there in another table with these exact numbers. so we therefore loop through each of these win tables checking the 1d representtion which itself is an array in this case called 'self' . so when index reaches 1 in the for loop we do
wins element 1 = { 1, 5, 9 } -> elements 1, 5, 9 of XXOOXOOX must be equal
i.e
if XXOOXOOX element 1 == XXOOXOOX element 5 == XXOOXOOX element 9
they are equal and we therefore have a winner!
from this concept you can compress the 3d grid into a 1d one then check for wins by changing the tables in wins
I hope that made sense, good luck & have fun
function board:win()
local wins = {
{1,5,9},
{3,5,7},
{1,4,7},
{2,5,8},
{3,6,9},
{1,2,3},
{4,5,6},
{7,8,9},
};
for index = 1, 8, 1 do
if self[wins[index][1]] == self[wins[index][2]] and self[wins[index][2]] == self[wins[index][3]] then
return self[wins[index][1]];
end
end
return false;
end