As a teaching exercise for my son, I am working on a tictactoe game tutorial. I have it working, but my method of finding a winner is very basic. Good for him but I wonder if there is a better way.
As it is I just check the whole board for a win after every move by running it through a stack of 'if' statements,
function CheckWinner()
Winner = 0
if Board(1,1,2) = Name(4) and Board(1,2,2) = Name(4) and Board(1,3,2) = Name(4) then Winner = Name(4)
if Board(2,1,2) = Name(4) and Board(2,2,2) = Name(4) and Board(2,3,2) = Name(4) then Winner = Name(4)
if Board(3,1,2) = Name(4) and Board(3,2,2) = Name(4) and Board(3,3,2) = Name(4) then Winner = Name(4)
if Board(1,1,2) = Name(4) and Board(2,1,2) = Name(4) and Board(3,1,2) = Name(4) then Winner = Name(4)
if Board(1,2,2) = Name(4) and Board(2,2,2) = Name(4) and Board(3,2,2) = Name(4) then Winner = Name(4)
if Board(1,3,2) = Name(4) and Board(2,3,2) = Name(4) and Board(3,3,2) = Name(4) then Winner = Name(4)
if Board(1,1,2) = Name(4) and Board(2,2,2) = Name(4) and Board(3,3,2) = Name(4) then Winner = Name(4)
if Board(1,3,2) = Name(4) and Board(2,2,2) = Name(4) and Board(3,1,2) = Name(4) then Winner = Name(4)
endfunction Winner
This works, but later if we want to change the number of squares in the board, we would have to rewrite the whole function.
What I was hoping for was a way to just check the cubes around the currently selected cube for any possible wins. What I am using, if it's not obvious, is an array "Board(3,3,3)" where the first two dimensions are the position of the cubes and the third dimension contains object names, ownership and color information etc.
So later, if I make it so you can choose the size of the grid, the array will be bigger and the algorithm will have to check larger areas.
So far, I have not been able to get a clear image of what I would have to do, any hints in the direction to go would be appreciated. I was thinking about searching larger and larger circles around the selected cube, but I run into problems at the edges of the grid.
I'm guessing there is a simple way of doing it that I just don't get. My talents are more artistic than mathematical.
Thanks.
Making a cool sig is just too much pressure for me right now.