Hi,
I'm currently doing a word search program for college that needs to find words diagonally,vertically and horizontally. What I have come up with to solve this problem works fine but I found a program that does what I did with much less code and gets the same results. What you see is the "ascendingDiagonal" method which is one of four very similar methods that searches through an array of columns and rows to display the word from the user input. This one clearly searches the diagonals ascending in the table but I don't what exactly it's doing.
My question is can anyone explain what is going on in this method please?
I'm currently doing a word search program for college that needs to find words diagonally,vertically and horizontally. What I have come up with to solve this problem works fine but I found a program that does what I did with much less code and gets the same results. What you see is the "ascendingDiagonal" method which is one of four very similar methods that searches through an array of columns and rows to display the word from the user input. This one clearly searches the diagonals ascending in the table but I don't what exactly it's doing.
My question is can anyone explain what is going on in this method please?
private static void ascendingDiagonal(char[] word) { boolean check = true; for (int i = word.length-1; i < wordsearch.length; i++) { for (int j = 0; j < wordsearch[i].length - (word.length-1); j++) { for (int k = 0; (k < word.length) && (check); k++) { if((wordsearch[i-k][j+k] == word[k]) && (k == word.length-1)) { wordFound = 1; System.out.println("\nWord found!"); System.out.println("Direction: Ascending diagonal"); System.out.println("Location:"); System.out.println("From Row: " + (i+1) + ", Column: " + (j+1) + " to Row: " + (i-(word.length-1)+1) + ", Column: " + (j+(word.length-1)+1)); } else if(wordsearch[i-k][j+k] != word[k]) check = false; } check = true; } } }