# TicTacToe problem

• March 13th, 2012, 12:15 PM
Kratos
TicTacToe problem
Hi guys, hopefully this is an easy one for you.

Given a grid of nxn squares, where each square has an id, the first(top left) square has id 0 (so a 5x5 grid will have ids 0-24) like below:
Attachment 1102

I need to generate all diagonal solutions of length Y. So if Y is 3, then some of the solutions will be:
Attachment 1103
and
Attachment 1104
and
Attachment 1105

but obviously NOT
Attachment 1106

Any ideas how these solutions can be generated?

This is what Iv got so far (dimension = 5, inARow = 3):

public ArrayList<int[]> getSolutions(int dimension, int inARow) {

ArrayList<int[]> solutions = new ArrayList<int[]>();

//create row solutions
for(int i=0; i<dimension*dimension; i = i+dimension) {
for(int j=i; j<=i+dimension - inARow; j++){
int[] row = new int[inARow];
int counter = 0;
for(int k=j; k<j+inARow; k++){
row[counter++] = k;
}
}
}

//create column solutions
for(int i=0;i<dimension;i++){
for(int j=i; j<(dimension*dimension)-(dimension*inARow)+dimension;j=j+dimension){
int[] col = new int[inARow];
int counter = 0;
for(int k=j;k<j+(dimension*inARow);k=k+dimension){
col[counter++] = k;
}
}
}

//create diagonals
for(int i=0; i<dimension*dimension; i++){
for(int j=i; j<i+(dimension * inARow); j = j+dimension+1){
System.out.println(j);
}
}

return solutions;
This gives me all the diagonal solutions but also gives me the bad ones like 3,9,15. Im having trouble eliminating those.

Anti-diagonals are also solutions so 2,6,10 would also be a solution but if I get normal diagonals working I can probably do the same for anti-diagonals.
• March 13th, 2012, 01:25 PM
KevinWorkman
Re: TicTacToe problem

It's almost impossible to answer "how do I do this" type questions other than to point you to google or the basic tutorials. Break your problem up into smaller pieces, attempt a piece, and ask a specific question when you get stuck.
• March 13th, 2012, 01:54 PM
Norm
Re: TicTacToe problem
How about a pair of methods: (left & right) that given a square, check if the square in their direction is good and returns the good square number or -1 if its not good.
• March 15th, 2012, 12:13 PM
Kratos
Re: TicTacToe problem
Quote:

Originally Posted by Norm
How about a pair of methods: (left & right) that given a square, check if the square in their direction is good and returns the good square number or -1 if its not good.

I think doing that Norm will work but it will change the foundation of my program too much. I think I may have worked it out otherwise, but thanks for your help!