-2

I'm solving 4 *4 boggle in Android using this piece of code. It's working well but I'm trying to get position of found word in it.

| A | C | E | X |
-----------------
| X | X | X | X |
-----------------
| X | X | X | X |
-----------------
| X | B | A | R |

Lets say I'm trying to find ACE's position based on 0 index. It is [0, 1, 2].

Or BAR, it's [13, 14, 15]

How can I achieve this? I tried a few implementation but it messed so I'm posting code without them.

public class Boggle {

    private NavigableSet<String> dict___ = new TreeSet<>();
    ArrayList<Word> foundWords = new ArrayList<>();

    /* helpers */
    int N = 4;
    char[][] board = new char[N][N];


    /* context obj*/
    private final Context ctx;

    public Boggle(Context ctx) {
        this.ctx = ctx;
        initFile();
    }


    public ArrayList<Word> startSolving(String str) {
        initPuzzle(str);

        Stack<Point> visitedLetters = new Stack<>();


        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                findWordRecursive(i, j, new Word("", null), visitedLetters);
            }
        }

        Log("foundWords = " + foundWords.size());
        Log("=============================");

        return foundWords;
    }

    private void findWordRecursive(int i, int j, Word prefix, Stack<Point> visitedLetters) {

        Point currentLocation = new Point(i, j);
        visitedLetters.push(currentLocation);

        String possibleWord = prefix.getWord() + this.board[i][j];

        if (dict___.contains(possibleWord)) {
            if(!isFoundWordsContains(possibleWord)) {
                foundWords.add(
                        new Word(
                                possibleWord,
                                null)
                );
            }
        }

        NavigableSet<String> child__ = findChildWords(possibleWord);

        if (!child__.isEmpty()) {
            for (int x = Math.max(0, i - 1); x < Math.min(i + 2, 4); x++) {
                for (int y = Math.max(0, j - 1); y < Math.min(j + 2, 4); y++) {
                    if (!visitedLetters.contains(new Point(x,y))) {
                        findWordRecursive(x, y, new Word(possibleWord, null), visitedLetters);
                    }
                }
            }
        }

        visitedLetters.pop();
    }

    private void initFile() {

        Log("=============================");
        Log("init starting");

        //fill dict___ array with words from a TXT file.

        Log("init finished")

    }


    void initPuzzle(String letters) {
        letters = letters.toLowerCase(new Locale("tr", "TR"));
        int in = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {

                board[i][j] = letters.charAt(in);
                in++;
            }
        }
        Log("board letters inited");
    }

    void Log(String e){
         Log.wtf("___Boggle___", e);
    }

    NavigableSet<String> findChildWords(String prefix){
        prefix = prefix.toLowerCase(new Locale("tr", "TR"));
        return dict___.subSet(prefix, false, prefix+"zzzzzzzzzzzzzzz", false);
    }

    boolean isFoundWordsContains(String word) {
        boolean yeah = false;

        for (int i = 0; i < foundWords.size(); i++) {
            if(word.equals(foundWords.get(i).getWord())) {
                yeah = true;
                break;
            }
        }

        return yeah;
    }
}

Word class:

public class Word {

    private String word;
    private ArrayList<Integer> position;

    public Word(String word, ArrayList<Integer> position) {
        this.word = word;
        this.position = position;
    }

    public String getWord() {
        return word;
    }

}
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Lazy
  • 1,807
  • 4
  • 29
  • 49
  • Hi there. If someone downvotes, don't worry about it, don't respond, and don't retaliate. They are just unicorn points and not worth getting worked up about. Especially, please do not edit your post to add abuse - it just creates work for someone else. Thanks! – halfer Sep 01 '15 at 23:58
  • Hey, Of course I know they are fake points, my anger was to downvoter without any comment/help/complaint/question/answer. – Lazy Sep 02 '15 at 00:27
  • People may vote anonymously if they wish, and there is no compulsion to explain them (other than the hover-over text on the down button). However, abusive retaliations may earn you further downvotes - worth bearing in mind. – halfer Sep 02 '15 at 08:09

1 Answers1

2

I think your issue is because of prefix = prefix.toLowerCase(new Locale("tr", "TR"));

So to make your code works, you may need to check these step:

  • all of String in dict___ must be lowerCase.
  • dict___ must contain the word subset (for "ace", must have at least "a", "ac", "ace")
  • in findWordRecursive replace String possibleWord = prefix.getWord() + this.board[i][j]; by
    String possibleWord = prefix.getWord() + this.board[i][j]; possibleWord = possibleWord.toLowerCase();
    Update
    To find position, you could try this:

  • in startSolving, use findWordRecursive(i, j, new Word("", new ArrayList<Integer>()), visitedLetters);

  • in findWordRecursive, add pass currentPosition

        int curPos = i * N + j;
        if (dict___.contains(possibleWord)) {
            if(!isFoundWordsContains(possibleWord)) {
                // add current position to the word
                ArrayList<Integer> posList = new ArrayList<Integer>();
                posList.addAll(prefix.position);
                posList.add(curPos);
                foundWords.add(
                        new Word(
                                possibleWord,
                                posList)
                );
            }
        }
    
        NavigableSet<String> child__ = findChildWords(possibleWord);
    
        if (!child__.isEmpty()) {
            for (int x = Math.max(0, i - 1); x < Math.min(i + 2, 4); x++) {
                for (int y = Math.max(0, j - 1); y < Math.min(j + 2, 4); y++) {
                    if (!visitedLetters.contains(new Point(x,y))) {
                        // add current before find new word
                        ArrayList<Integer> posList = new ArrayList<Integer>(prefix.position);
                        posList.addAll(prefix.position);
                        posList.add(curPos);
    
                        findWordRecursive(x, y, new Word(possibleWord, posList), visitedLetters);
                    }
                }
            }
        }
    
justHooman
  • 3,044
  • 2
  • 17
  • 15
  • Bro, I'm already solving any type of boggle, and finding words in it. If you read the question, I'm trying to get position of found word. – Lazy Aug 14 '15 at 12:10
  • @Lazy, Sorry, I updated the answer, please check. Just don't understand that why you couldn't do that yet after such a complex code :) – justHooman Aug 14 '15 at 15:03