jweisbeck / Crossword

A javascript crossword puzzle script
http://www.jesseweisbeck.com/crossword/
244 stars 140 forks source link

show stored puzzle attempts #23

Open bradzo opened 3 years ago

bradzo commented 3 years ago

Hi folks Not an issue, but PR's don't seem to be looked at - hope this helps. Tell me if I'm doing this wrong - very new to GH. Just want to share my solution.

Have been working on this over last couple of days, and needed a way to show the attempts made on the puzzle.

If you paste this function into the puzInit var, as the puzzle is generated, it'll fill in the user's attempts.

    showAttempts: function() {

        //console.log("entries",entries);       // co-ords of cell - col,row, ["1,1", "2,1", "3,1", "4,1"]

        for (var x=1, p = entryCount; x <= p; ++x) {
            //var letters = puzz.data[x-1].answer.split('');
            var attempt = puzz.data[x-1].attempt.split('');     // retrieved user's attempt - useful when rebuilding puzzle with user attempts
            console.log(attempt);

            console.log("entries["+(x-1)+"]",entries[x-1]);     // co-ords of cell - col,row, ["1,1", "2,1", "3,1", "4,1"]

            var y = 0;
            for (var i=0; i < entries[x-1].length; ++i) {
                var selector =  '.position-' + (x-1) + ' input';
                //console.log(selector);

                var co = entries[x-1][i].split(",");
                console.log("co:",co);

                var c0 = co[0];
                var c1 = co[1];
                var r = 0;
                if(c0>=c1) {
                    r = c0;
                    // try to normalise what we have
                    // col value appears first, and 
                    // has the column value of the <td> element across the row
                    // to target it properly, if it's at say, col 6 (and its the 1st letter position of the word),
                    // we need to force it back to 1
                    var t1 = r;     // could be 6
                    var t2 = t1 - 1;    // whatever value is one less ie: 5
                    var t3 = (t1 - t2) + y; // ie: 6 - 5 = 1
                    y++;
                    r = t3;

                } else {
                    r = co[1];
                }

                console.log("r:",r,"t1:",t1, "t2:",t2, "t3:",t3,"attempt[r-1]",attempt[r-1],"attempt[t3-1]",attempt[t3-1]); 

                    $(selector)[r-1].value = attempt[r-1];

            }
        }

    },

This bit:

var attempt = puzz.data[x-1].attempt.split('');     // retrieved user's attempt - useful when rebuilding puzzle with user attempts
                        console.log(attempt);

requires that a key in the json puzzleData variable called "attempt" have the users attempt at this clue.

So, for example:

    var puzzleData = [
        {
            clue: "my first name",
            answer: "brad",
            attempt: "blah",
            position: 1,
            orientation: "across",
            startx: 1,
            starty: 1
        },
        {
            clue: "my wife's first name",
            answer: "nottellingyou",
            attempt: "abcdefghijklm",
            position: 2,
            orientation: "across",
            startx: 6,
            starty: 1
        },
        {
            clue: "opposite of above",
            answer: "below",
            attempt: "below",
            position: 1,
            orientation: "down",
            startx: 1,
            starty: 1
        },

    ] 

Oh! One last thing: In var puzInit = {, right at the bottom, add puzInit.showAttempts() as below:

    puzInit.buildTable();
    puzInit.buildEntries();
    puzInit.showAttempts();

If you edit the puzzle data in this repo and add a key called "attempt" with some value in each json entry, give it a whirl! It worked for me.

Hope this helps!