Checking diagonals in an array

I need to find all diagonals rows, and columns, in a multi dimensional dynamic array that share a common charector. tic tac toe essentially

the user inputs the size of the array [5][5]

the tricky part is the user also inputs how many elements in the array to find that match. !!!

int numInaRow;

the most elements to find that match in a 5x5 is 5...

so look for 3 to give us more possibilities..

int numInaRow = 3;

[0,0]
[1,1]
[2,2].....this is simple they are all equal




[2,0] [2,3]
[1,1] [3,4]
[0,2] [4,5]

this one is [0++,2--] this one is [0++,1++]

so what if one function checked everything, rows and columns too.

iterate through the array.. and each number would check all the numbers around it.
For example
Q= [5][2]
___________
_____xxx___
_____xQx___
_____xxx___ Q is the number the iteration is currently checking and X is the spaces it's checking, if no characters are the same, it moves on..


___________
_____Qxx___
_____xQx___
_____xxx___
but if Q found a charector match diagonally, it can only continue diagonally or in the same direction

so the next sequence would only need to check one space.

___Q______
_____Q--___
_____-Q-___
_____---___

iterate top to bottom so it would check [0][1], then [0][2] [0][3]
and next could iterate + numInaRow??? so it doesn't need to double check...

I haven't tried to program it myself yet but if anyone has any tips or ideas that will help that would be greatly appreciated! Bedtime..











closed account (48T7M4Gy)
So you are looking for three in a row.

Take any particular cell [x][y] and just write all the possibilities.

Take advantage of the fact that you don't have to go right up to the perimeter and 3 in one direction is the same as 3 starting from the same cell going in the opposite direction.

You can probably get away with iterating horizontally as well as vertically in 3 cell increments. Once you have the check routines worked out you can double check this by seeing if it covers the grid completely.

If it's ordinary tic-tac-toe then you only have to find 1 trio.
And a miss on any one of a particular trio means you dump that test and move to the next.

Some of these you already have. It won't take long to program it.
Topic archived. No new replies allowed.