Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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:
grid.png

I need to generate all diagonal solutions of length Y. So if Y is 3, then some of the solutions will be:
grid1.png
and
grid2.png
and
grid3.png

but obviously NOT
grid4.png

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.

2. ## Re: TicTacToe problem

Recommended reading: How to Program (stuck on homework? READ THIS BEFORE POSTING)

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.

3. ## 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.

4. ## Re: TicTacToe problem

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!