Running Boggle Solver takes over an hour to run. What is wrong with my code?

Posted by user1872912 on Stack Overflow See other posts from Stack Overflow or by user1872912
Published on 2012-12-03T16:54:59Z Indexed on 2012/12/03 17:03 UTC
Read the original article Hit count: 151

Filed under:
|
|

So I am running a Boggle Solver in java on the NetBeans IDE. When I run it, i have to quit after 10 minutes or so because it will end up taking about 2 hour to run completely. Is there something wrong with my code or a way that will make is substantially faster?

public void findWords(String word, int iLoc, int jLoc, ArrayList<JLabel> labelsUsed){

    if(iLoc < 0 || iLoc >= 4 || jLoc < 0 || jLoc >= 4){
        return;
    }

    if(labelsUsed.contains(jLabels[iLoc][jLoc])){
        return;
    }

    word += jLabels[iLoc][jLoc].getText();
    labelsUsed.add(jLabels[iLoc][jLoc]);

    if(word.length() >= 3 && wordsPossible.contains(word)){
        wordsMade.add(word);
    }

    findWords(word, iLoc-1, jLoc, labelsUsed);
    findWords(word, iLoc+1, jLoc, labelsUsed);
    findWords(word, iLoc, jLoc-1, labelsUsed);
    findWords(word, iLoc, jLoc+1, labelsUsed);
    findWords(word, iLoc-1, jLoc+1, labelsUsed);
    findWords(word, iLoc-1, jLoc-1, labelsUsed);
    findWords(word, iLoc+1, jLoc-1, labelsUsed);
    findWords(word, iLoc+1, jLoc+1, labelsUsed);

    labelsUsed.remove(jLabels[iLoc][jLoc]);
}

here is where I call this method from:

public void findWords(){
    ArrayList <JLabel> labelsUsed = new ArrayList<JLabel>();
    for(int i=0; i<jLabels.length; i++){
        for(int j=0; j<jLabels[i].length; j++){
            findWords(jLabels[i][j].getText(), i, j, labelsUsed);
            //System.out.println("Done");
        }
    }
}

edit: BTW I am using a GUI and the letters on the board are displayed by using a JLabel.

© Stack Overflow or respective owner

Related posts about java

Related posts about Performance