0

I am desperate and I need your help! I am working on a crossword puzzle solver where the puzzle and the words are given to us and we need to format the puzzle so each index in the array has it's own letter. Right now I have the puzzle index split and the words split so I can go in and compare them letter by letter but is there a better way? Is it possible to, lets say, have a string array that contains the word I'm looking for and have an array of chars locate that word?

    My array looks like this: 
    [W, V, E, R, T, I, C, A, L, L]
    [R, O, O, A, F, F, L, S, A, B]
    [A, C, R, I, L, I, A, T, O, A]
    [N, D, O, D, K, O, N, W, D, C]
    [D, R, K, E, S, O, O, D, D, K]
    [O, E, E, P, Z, E, G, L, I, W]
    [M, S, I, I, H, O, A, E, R, A]
    [A, L, R, K, R, R, I, R, E, R]
    [K, O, D, I, D, E, D, R, C, D]
    [H, E, L, W, S, L, E, U, T, H]

And lets say I'm looking for "Vertical". How can I set up a loop to look for the chars that make up the string "Vertical". Or do I have to compare it letter by letter?

This is my code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class WordSearch {

    public static void main(String[] args) throws Exception{
            File file = new File("puzzle.txt"); 
            Scanner sc = new Scanner(file);
        int row = sc.nextInt();
        int col = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the puzzle
        char[][] puzzle = new char[row][col];   


         //Read in the strings from the file into the array.
        for (int i=0; i<row; i++){
            String getChar = (new String(sc.next()));
            for(int j = 0;j<col; j++){
                puzzle[i][j] = getChar.charAt(j);
                }
            }

        //Test Print
         for (int i=0; i<puzzle.length; i++){

               System.out.print(Arrays.toString(puzzle[i]));
               System.out.println("");
         }



        //Read the number of words and move to the next line    
        int numwords = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the words and read in the words from a file
        String[] words = new String[numwords];
        for(int i=0; i<numwords; i++){
            words[i] = sc.nextLine();
        }

        //look for each word
        for(int i=0; i<numwords; i++){
            String currentword = words[i];      

            String[] strings1 = currentword.split("");

            int range = currentword.length()+1;

            String[] strings2 = Arrays.copyOfRange(strings1, 1, range);

            int range2 = strings2.length;
            char[] charwords = new char[range2];    





            }



    //Close the scanner     
    sc.close();
    }

    }

Would this work to test for the diagonals:

private boolean checkDiagonals(int row, int col, String word, char[][] puzzle) {
        //Checking diagonals direction
        for(int letter = 1; letter < word.length(); letter++) {
            if(puzzle[row + letter][col + letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row + letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
                return false;
            }
        }
        return true;
    }
7
  • 1
    Are words horizontal, diagonal, and vertical? Commented Oct 1, 2013 at 14:42
  • Yes, they are in all the cardinal directions. Or so the teacher says. Commented Oct 1, 2013 at 14:44
  • You're going to have to write the code that performs the searching. If you are searching for a specific word, look for the starting letter, then check each direction to see if you can complete the word Commented Oct 1, 2013 at 14:46
  • could they be in reverse order (right to left)? Commented Oct 1, 2013 at 14:47
  • @JohnB Yes they can be in reverse. Commented Oct 1, 2013 at 14:52

3 Answers 3

1

Because the words can be in any direction, it'd make more sense to simply go letter by letter. Search the entire array of characters for the first letter of the word you're looking for. For example, if you were looking for "VERTICAL", you would go through the whole array looking for the letter 'V'.

Then have a function that verifies the word for all directions (up, down, left, right, diagonals). Let's call it check(int row, int col).

for(int row = 0; row < puzzle.length; row++) {
    for(int col = 0; col < puzzle[0].length; col++) {
        if(puzzle[row][col] == word.charAt(0))
            if(check(row, col) == true)
                    //We found it.
    }
}

Then to write check(int row, int col, String word, char[][] puzzle), you'd check each cardinal direction, for example, if you were checking right, you'd keep going right and compare the letters to the word until you match the word, or find an inconsistency. Then if no direction works, return false.

As an example, here's checking to the right (no error checking in the case we're out of the array, you'd want to implement that).

private boolean checkRight(int row, int col, String word, char[][] puzzle) {
    //Checking right direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}

You'd then want one of these for each direction, and then in your check function, it'd look something like

public boolean check(int row, int col, String word, char[][] puzzle) {
    if(checkRight(row, col, word, puzzle)) return true;
    if(checkLeft(row, col, word, puzzle)) return true;
    if(checkUp(row, col, word, puzzle)) return true;
    if(checkDown(row, col, word, puzzle)) return true;
    if(checkDiagonals(row, col, word, puzzle)) return true;
    return false;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! Now it just says that method check(int,int) is undefined. Gonna attempt to define it.
So would I just write a function that adds 1 to [col] until it matches then keep adding 1 to [col] until it finds all the chars/reaches the end of line and then add 1 to [row] and continue through the puzzle?
I'm confused as to what to do with row and col once inside the check method.
So, row and col that are passed in to the method are the position of the first letter. You'd then want to check the cardinal directions (left, right, up, down, diagonals). So for left, you'd subtract 1 from just the columns and compare the next letter to the next letter in the word. Then for right, you'd add one to col and compare the letters. But each time you switch directions you'd want to start from the initial position.
I'm sorry but I am a super noob in java. The check method returns true or false but I don't know how to make it move positions. I can add or subtract from row and col in the check method but how would that return true or false. Do I need to add another if(puzzle[row][col] == word.charAt(0) inside the check method?
|
1

Is it possible to, lets say, have a string array that contains the word I'm looking for and have an array of chars locate that word?

Yes, Simply do

get element from String array like

  String arrayElem = str[i]; // i is index.

which gives you a string.

Then

  boolean result=Arrays.equals(arrayElem.toCharArray(), actualCharArray);

1 Comment

The only problem I am having now is that my array of chars is a 2d array while .equals is only allowing char[] char[].
-1

I would suggest converting each row in the array to a String then use String.indexOf(). This will determine if the string you are looking for exists in the row and at what position it begins. If you get back -1 you know the string does not exist in the row.

String.indexOf()

4 Comments

This will not address diagonals.
true, comment about this was posted after I posted the answer. I think I would still create Strings to represent all directions (horizontal, vertical, top-left to bottom-right, and top-right to bottom-left). You could also reserve the word you are looking for to find if it exists in the opposite direction.
It won't address vertical words either. Java doesn't have a method that I know of to extract a column from a 2-dimensional array, and I didn't see one in a quick look at Apache Commons' ArrayUtils either. Maybe there's one in some library package somewhere.
Didn't suggest that there was a built in mechanism to do so but would be easy to write a method that loops the array in the vertical / diagonal directions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.