Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / TicTacToe algorithm?

Author
Message
Radio Check
20
Years of Service
User Offline
Joined: 18th Aug 2004
Location:
Posted: 3rd Oct 2004 00:49
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,


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.
the_winch
21
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 3rd Oct 2004 01:27
One way would be to have a global variable that tells you the size of the game area. Since the grid will allways be square you just need one varable for the width and height.

The to check all you would have to do is.
Check the two diagonals.
Check for vertical lines.
Check for horizontal lines.

it's cool to hate

Login to post a reply

Server time is: 2024-09-23 01:25:31
Your offset time is: 2024-09-23 01:25:31