bunkat / wordfind

A small javascript library for generating and solving word find (also known as word search) puzzles.
MIT License
194 stars 110 forks source link

Solve display does not work if your html page contains some classes labelled as '.' + word to find #17

Open clodion opened 2 years ago

clodion commented 2 years ago

The solve display need to find all the buttons created with an automated created class with the text to find. So, if your page uses this class to display some elements that are not in the puzzle, thoses elements will get the "wordFound" class.

The first time you solves the puzzle everything is good. but if you want to dynamically rgenerate another puzzle, without reloading your html page, then the solve script does not word anymore on the texte to find, which correspond to the className.

I've changed in wordfindgame.js the solve script in order to set the class "wordFound" only into the div containing te puzzle, whatever this div id can be. here is the code :

` solve: function(puzzle, words) {

            var solution = wordfind.solve(puzzle, words).found;

            // recherche l'element qui contient le puzzle
            var elem_puzzle = $('[x="0"][y="0"]').parent().parent().attr('id')

            for (var i = 0, len = solution.length; i < len; i++) {

                var word = solution[i].word,
                    orientation = solution[i].orientation,
                    x = solution[i].x,
                    y = solution[i].y,
                    next = wordfind.orientations[orientation];

                if (!$('#'+elem_puzzle + '.' + word).hasClass('wordFound')) {

                    for (var j = 0, size = word.length; j < size; j++) {
                        var nextPos = next(x, y, j);
                        $('[x="' + nextPos.x + '"][y="' + nextPos.y + '"]').addClass('solved');
                    }

                    $('#'+ elem_puzzle +' .' + word).addClass('wordFound');

                }
            }

        },

Hope it helps`everybody