Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show stored puzzle attempts #23

Open
bradzo opened this issue Feb 28, 2021 · 0 comments
Open

show stored puzzle attempts #23

bradzo opened this issue Feb 28, 2021 · 0 comments

Comments

@bradzo
Copy link

bradzo commented Feb 28, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant