7 Replies - 2994 Views - Last Post: 10 February 2011 - 11:10 AM Rate Topic: -----

#1 Guest_troubledwithjava*


Reputation:

Assistance with locating words diagonally in an array

Posted 09 February 2011 - 09:27 PM

I'm trying to locate words in a 2-d char array. The array outputs letters in rows and columns (like a word search puzzle). I need to find words both horizontally and vertically and diagonally. I have the horizontal way working but am confused how to do the diagonal and vertical part. I know that I need to go through the rows step by step to align the diagonal rows in one to find the word but I don't understand how to put that into code.



Here's what I have so far. I guess I mainly need help figuring out what to put in the solveDirection method. Please help, I'm very lost.

static Point findWord (char[][] puzzle, String[] words)
    {
		for(int h=0; h<words.length; h++){
			String word = words[h];
	char[] letters = word.toCharArray ();

	for (int r=0; r<puzzle.length; r++) {
	    for (int c=0; c<puzzle[r].length; c++) {
	    	 for( int rd = -1; rd <= 1; rd++ )
                 for( int cd = -1; cd <= 1; cd++ )
                     if( rd != 0 || cd != 0 )
                         matches = solveDirection( r, c, rd, cd, puzzle);
		
		boolean found = true;

		for (int k=0; k<letters.length; k++) {
		    if ( (c+k >= puzzle[r].length) || (letters[k] != puzzle[r][c+k]) ) {
		
			found = false;
			break;
		    }
		}

		if (found) {
		    return new Point(r,c);
		}

	    }
	}
		}
	return null;
    }



Is This A Good Question/Topic? 0

Replies To: Assistance with locating words diagonally in an array

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: Assistance with locating words diagonally in an array

Posted 09 February 2011 - 09:45 PM

Four nested for loops?! Surely you can do better than that.

As for vertical iteration, increment the row counter in the array[row][col] setup. For diagonal iteration, you want to increase both row and col, or decrease both of them based on the direction you are travelling. You still want to use nested loops (2) here.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_troubledwithjava*


Reputation:

Re: Assistance with locating words diagonally in an array

Posted 09 February 2011 - 11:17 PM

Clearly I'm missing something. I understand that I should increment the rows up and the columns down in something like puzzle[r++][c--] but I'm not sure where to proceed from there.

Any other explanations? I'm really confused and most of the code I've tried just doesn't do a thing.
Was This Post Helpful? 0

#4 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Assistance with locating words diagonally in an array

Posted 09 February 2011 - 11:30 PM

View Posttroubledwithjava, on 10 February 2011 - 01:17 AM, said:

Clearly I'm missing something. I understand that I should increment the rows up and the columns down in something like puzzle[r++][c--] but I'm not sure where to proceed from there.

Any other explanations? I'm really confused and most of the code I've tried just doesn't do a thing.


The basic way to do is very simple. It looks like you already started doing it.

First, choose a location in your puzzle to start searching from - puzzle[0][0] would work.
Then search for words horizontally - keep the row the same, but increase the column locations.
Then search for words vertically - keep the column the same, but increase the row locations.
Then search for words diagonally - increase both the row and the column locations.
Repeat until either all words are found or you read the end of the puzzle.


Example:
Let's say you are looking for the word "the" in the puzzle.

First you would start at puzzle[0][0].
We know that "the" is 3 characters long, so we need to look for 3 characters in each direction.
Horizontally - search in puzzle[0][0], puzzle[0][1], and puzzle[0][2] for the appropriate letters
Vertically - search in puzzle[0][0], puzzle[1][0], and puzzle[2][0]
Diagonally - search in puzzle[0][0], puzzle[1][1], and puzzle[2][2]
If you found "the" in any of the above directions, then we're done!
Otherwise, go back to the beginning and do the same thing, but start at a new puzzle location - puzzle[0][1]
Was This Post Helpful? 0
  • +
  • -

#5 Guest_troubledwithjava*


Reputation:

Re: Assistance with locating words diagonally in an array

Posted 10 February 2011 - 09:40 AM

That all makes sense, thanks moobler!

The problem is I do understand theoretically what needs to be done but I don't know how to actually write the code for that. Could you show me an example of how i would set up a loop to search at each of those points? I'm still so confused!
Was This Post Helpful? 0

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: Assistance with locating words diagonally in an array

Posted 10 February 2011 - 09:42 AM

To iterate across the main diagonal from (0,0) to (length-1, length-1):
for(int i = 0; i < array.length; i++){
     //access array[i][i]
}


Was This Post Helpful? 0
  • +
  • -

#7 Guest_troubledwithjava*


Reputation:

Re: Assistance with locating words diagonally in an array

Posted 10 February 2011 - 11:08 AM

Doesn't that just find that position (i,j) in an array? It doesn't seem as if it sets that position to anything though.
Was This Post Helpful? 0

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: Assistance with locating words diagonally in an array

Posted 10 February 2011 - 11:10 AM

It just iterates over a diagonal. It's up to you to deal with the array elements.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1