Ok, so I am beginning to think that I'm hopeless with data structures.

I am writing a knight's tour program that begins in the top left of an mxn board and tries to travel to each square by making moves that are legal for knights in chess. The process is not my problem, its the data structures that are killing me.

I would like to hold all the legal moves that can be made from a square in a data object. Each data object would hold and row and column position, so a 1x2 array. My problem has been in creating and adding to a list of arrays. I'll post a couple key lines of my code then the all of it at the bottom.

I CAN'T TELL YOU HOW MUCH I WOULD APPRECIATE ANY HELP!

Whether its with this issue or just critique in general of my program.

public List getMoves(int i, int j, int rows, int cols, boolean[][] isTaken)

{

List<int[]> moves = new ArrayList< int[] >();

if (this.isValid(i-2, j+1, rows, cols, isTaken))

moves.add({i-2, j-1});

......

List<int[]> possMoves = this.getMoves(currMove[0], currMove[1], rows, cols, isTaken);

if (possMoves.size() == 0)

moreMoves = false;

else

{

int randomIndex = generator.nextInt( possMoves.size() );

currMove = possMoves.get(randomIndex);

moveCount++;

}

}

....

HERE IS ALL OF THE CODE

// Knights Tour app

import java.io.*;

import java.util.*;

public class kTour

{

public boolean isValid(int i, int j, int rows, int cols, boolean[][] isTaken)

{

if (i < 0 || j < 0 || i >= rows || j >= cols || isTaken[i][j])

return false;

else

return true;

}

public List getMoves(int i, int j, int rows, int cols, boolean[][] isTaken)

{

List<int[]> moves = new ArrayList< int[] >();

if (this.isValid(i-2, j+1, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i-1, j+2, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i+1, j+2, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i+2, j+1, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i+2, j-1, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i+1, j-2, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i-1, j-2, rows, cols, isTaken))

moves.add({i-2, j-1});

if (this.isValid(i-2, j-1, rows, cols, isTaken))

moves.add({i-2, j-1});

return moves;

}

public static void main(

String argv[])

{

int rows = Integer.parseInt(argv[0]);

int cols = Integer.parseInt(argv[1]);

int trys = Integer.parseInt(argv[2]);

int moveCount;

boolean moreMoves;

boolean[][] isTaken = new boolean[rows][cols];

int[] startPos = {0,0}

int[] currMove = startPos;

for ( int count = 0; count < trys; ++count)

{

moreMoves = true;

moveCount = 0;

for(int i = 0; i < rows; ++i)

{

for(int j = 0; j < cols; ++j)

isTaken[i][j] = false;

}

while(moreMoves)

{

isTaken[currMove[0]][currMove[1]] = true;

List<int[]> possMoves = this.getMoves(currMove[0], currMove[1], rows, cols, isTaken);

if (possMoves.size() == 0)

moreMoves = false;

else

{

int randomIndex = generator.nextInt( possMoves.size() );

currMove = possMoves.get(randomIndex);

moveCount++;

}

}

if (moveCount == rows*cols)

System.out.println("Victory");

else

System.out.println("Failure");

}

}

}