-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.find-a-word.min.js
1 lines (1 loc) · 10.5 KB
/
jquery.find-a-word.min.js
1
var lineMath=function(e,a){function t(e,a){return e[0]==a[0]?null:(a[1]-e[1])/(a[0]-e[0])}function o(e,a){return console.log(e),console.log(a),null===a?e[0]:e[1]-a*e[0]}e=[Number(e[0]),Number(e[1])],a=[Number(a[0]),Number(a[1])],console.log(e),console.log(a);var r=t(e,a),s=o(e,r),n=!1;if(e[0]>a[0]||e[1]>a[1]){var i,d;n=!0,d=[e[0],e[1]],i=[a[0],a[1]],e=i,a=d}var c,l=[];if(e[0]!=a[0])if(e[0]<a[0])for(x=e[0];x<=a[0];x++)c=r*x+s,l.push([x,c]);else for(x=e[0];x>=a[0];x--)c=r*x+s,l.push([x,c]);else for(c=e[1];c<=a[1];c++)l.push([e[0],c]);return n&&l.reverse(),l},data={testgame:{wordlist:["COW","CAT","DOG","PIG","OWL"],gameboard:[["G","O","D","X","X","X"],["X","X","X","X","O","X"],["X","X","C","O","W","X"],["C","X","X","X","L","G"],["X","A","X","X","I","X"],["X","X","T","P","X","X"]]},gameone:{wordlist:["ANNAPOLIS","AUGUSTA","AUSTIN","BISMARCK","CHARLESTON","CHEYENNE","COLUMBUS","HARRISBURG","HARTFORD","LANSING","MADISON","MONTGOMERY","NASHVILLE","OLYMPIA","PROVIDENCE","RICHMOND","SACRAMENTO","SALEM","SPRINGFIELD","TALLAHASSEE"],gameboard:[["D","E","S","P","J","H","I","C","V","A","G","G","O","K","L","W","M"],["H","L","V","Q","F","A","I","P","M","Y","L","O","X","B","U","O","M"],["F","L","E","S","P","T","F","Z","G","C","H","E","Y","E","N","N","E"],["X","I","Z","I","Z","S","A","C","R","A","M","E","N","T","O","N","L"],["J","V","Q","L","F","U","Y","S","U","I","T","B","G","N","S","N","A"],["J","H","U","O","T","G","I","T","B","Q","C","O","I","K","A","H","S"],["D","S","B","P","H","U","N","A","S","A","M","H","Q","P","T","Z","D"],["B","A","H","A","L","A","S","I","I","E","U","D","M","R","V","E","Q"],["U","N","A","N","Y","C","H","A","R","L","E","S","T","O","N","J","K"],["Q","L","R","N","C","O","V","Y","R","P","V","Q","T","V","N","Q","G"],["P","O","T","A","L","L","A","H","A","S","S","E","E","I","F","D","K"],["P","D","F","Y","I","U","B","P","H","O","Z","R","Y","D","N","Q","K"],["O","T","O","T","T","M","A","D","I","S","O","N","W","E","E","Q","Y"],["Y","F","R","A","L","B","W","P","L","A","N","S","I","N","G","Q","M"],["B","F","D","O","O","U","M","B","I","S","M","A","R","C","K","C","Q"],["R","N","H","H","B","S","Q","M","N","F","P","G","G","E","D","Z","E"],["R","O","P","R","G","Y","P","O","L","F","K","K","G","B","I","N","O"]]},gametwo:{wordlist:["ANNAPOLIS","AUGUSTA","AUSTIN","BISMARCK","CHARLESTON","CHEYENNE","COLUMBUS","HARRISBURG","HARTFORD","LANSING","MADISON","MONTGOMERY","NASHVILLE","OLYMPIA","PROVIDENCE","RICHMOND","SACRAMENTO","SALEM","SPRINGFIELD","TALLAHASSEE"],gameboard:[["V","Q","I","T","A","L","L","A","H","A","S","S","E","E","H","E","Q"],["N","I","X","Z","M","A","N","H","E","G","I","U","K","P","A","R","J"],["Q","A","R","Q","R","R","N","T","M","B","F","L","L","G","R","A","O"],["Z","I","I","I","Z","X","J","L","J","W","K","Q","R","D","R","C","J"],["P","H","A","R","S","F","V","R","L","B","J","C","L","N","I","W","C"],["A","F","V","K","X","K","T","M","K","Y","I","I","L","O","S","W","C"],["E","C","V","J","Y","N","X","I","W","Q","Y","I","P","J","B","M","Y"],["K","E","C","I","T","H","X","L","W","T","M","E","L","C","U","K","N"],["M","E","H","Y","V","Y","V","A","S","C","C","C","D","C","R","L","J"],["W","T","E","G","D","C","O","Q","T","G","G","J","J","G","G","D","G"],["D","V","Y","C","Q","T","F","N","Z","D","Y","A","S","D","J","E","O"],["N","X","E","V","D","N","Z","K","Y","L","L","X","S","G","I","M","V"],["P","K","N","C","O","Q","N","O","K","H","G","W","F","H","M","U","A"],["I","I","N","A","E","I","R","V","J","L","C","T","H","Q","O","B","G"],["H","F","E","O","R","I","U","E","Z","C","U","O","U","X","U","I","H"],["A","S","A","Z","V","X","Y","C","M","I","L","H","P","A","V","C","Q"],["U","Z","V","J","D","O","K","U","N","N","E","V","W","Q","V","Z","A"]]}};!function(e,a,t){"use strict";e.fn.findAword=function(o){var r,s,n,i=e(this);r=e.extend({buttons:!0,results:!0,instructions:"Highlight (click-and-drag) as many as you can. Words can be forward, backward, or diagonal"},o),s=a[r.game];var n={state:"ready",init:function(){console.log("init"),n.layout.create(),n.controls.create(),n.resultsboard.create(),n.wordlist.create(),n.gameboard.create(),n.canvas.create()},layout:{create:function(){i.css("margin","20px");var a=[];a.push('<div id="instructions-container"></div>'),a.push('<div id="results-container"></div>'),a.push('<div id="wordlist-container">'),a.push(' <div id="wordlist-subcontainer"></div>'),a.push(' <div class="clear"></div>'),a.push("</div>"),a.push('<div id="gameboard-container" class="noselect">'),a.push(' <div id="gameboard-subcontainer">'),a.push(" </div>"),a.push("</div>"),a.push('<div id="controls-container">'),a.push(' <div id="controls">'),a.push(' <a id="reset" class="btn">RESET</a>'),a.push(' <a id="resign" class="btn">RESIGN</a>'),a.push(" </div>"),a.push("</div>"),i.append(e(a.join("\n"))),e("#instructions-container").html(r.instructions)}},controls:{create:function(){this.enableResign(),e("#reset").off().on("click",n.controls.reset),e("#resign").off().on("click",n.controls.resign),r.buttons||e("#controls-container").hide()},reset:function(){function a(){n.state="ready",n.wordlist.reset(),e("#wordlist-subcontainer").empty(),e("#gameboard-subcontainer").empty(),e("#results-container").empty(),n.init()}if(console.log("reset"),"resigned"==n.state||"completed"==n.state)return void a();var t=confirm("You want to reset the game? All found words will be erased.");t===!0&&a()},resign:function(){console.log("resign");var e=confirm("You want to resign the game?");e===!0&&(n.state="resigned",n.resultsboard.indicateResigned(),n.gameboard.lock())},gameCompleted:function(){console.log("game-completed")},enableResign:function(){e("#resign").css("visibility","visible")},disableResign:function(){e("#resign").css("visibility","hidden")}},resultsboard:{create:function(){console.log("create results board");var a=e("<div></div>").attr("id","results");e("#results-container").append(a),this.clear(),r.results||e("#results-container").hide()},update:function(a){e("#results").html(a)},indicateComplete:function(){n.gameboard.lock(),this.update("You found all the words on this puzzle."),n.state="completed",n.controls.disableResign(),e("#results").addClass("completed")},indicateResigned:function(){var a=n.wordlist.words.length,t=n.wordlist.found.length,o=t+" of "+a;this.update("You resigned. You found "+o+" words."),e("#results").addClass("resigned")},clear:function(){e("#results").removeClass(),this.update("Ready to Play!")}},wordlist:{words:s.wordlist,found:[],create:function(){var a=e("<ul></ul>").attr("id","wordlist");e("#wordlist-subcontainer").append(a);for(var t in s.wordlist){var o=e("<li>"+s.wordlist[t]+"</li>").addClass("word");a.append(o)}},isInList:function(e){var a=this.words.indexOf(e);return a!=-1&&(this.found.push(e),!0)},crossOffList:function(a){e("li.word").each(function(t,o){e(o).text()==a&&e(o).html("<strike>"+a+"</strike>")})},isGameCompleted:function(){return this.words.length==this.found.length},reset:function(){this.found=[]}},gameboard:{state:"startup",board:null,selected_a:[],selected_b:[],create:function(){function a(a,t,o){return e("<td>"+o+"</td>").css({backgroundColor:"transparent"}).data("x",a).data("y",t).on("mousedown",n.gameboard.selectA).on("mouseup",n.gameboard.selectB).on("mouseenter",n.gameboard.isInLine).on("mouseleave",n.gameboard.clearInLine)}this.state="waiting";var t=e("<table></table>").attr("id","gameboard");e("#gameboard-subcontainer").append(t);for(var o in s.gameboard){t.append("<tr></tr>");for(var r in s.gameboard[o]){var i=s.gameboard[o][r],d=a(r,o,i);t.find("tr:eq("+o+")").append(d)}}e("#gameboard").on("mouseleave",n.gameboard.resetBoard),e("#gameboard-subcontainer").css("height",e("#gameboard").height()+"px").css("width",e("#gameboard").width()+"px")},selectA:function(){var a=e(this);a.addClass("selectA"),n.gameboard.state="selectA",n.gameboard.selected_a=[a.data("x"),a.data("y")]},selectB:function(){var a=e(this);if(a.hasClass("outofline")||a.hasClass("selectA"))return void n.gameboard.resetBoard();n.gameboard.state="selectB",n.gameboard.selected_b=[a.data("x"),a.data("y")];var t=n.gameboard.checkWord();t?(n.gameboard.state="waiting",n.wordlist.isGameCompleted()&&n.resultsboard.indicateComplete(),n.gameboard.resetBoard()):n.gameboard.state="waiting"},isInLine:function(){var a=n.gameboard.state;if("waiting"!=a){var t=n.gameboard.selected_a,o=[e(this).data("x"),e(this).data("y")];t[0]==o[0]||t[1]==o[1]||Math.abs(t[0]-o[0])==Math.abs(t[1]-o[1])?n.gameboard.highlightDraggedSelection(t,o):e(this).addClass("outofline")}},clearInLine:function(){n.gameboard.clearDraggedSelection(),e(this).removeClass("outofline")},checkWord:function(){var e=n.gameboard.selected_a,a=n.gameboard.selected_b,t=this.getWord(e,a);return n.gameboard.clearDraggedSelection(),n.wordlist.isInList(t)?(console.log("found a word"),n.canvas.circleWord(e,a),n.wordlist.crossOffList(t),!0):(console.log(t+" is not a word on the list."),n.gameboard.resetBoard(),!1)},getWord:function(a,o){var r=[],s=t(a,o);for(var n in s){var i=s[n],d=e("#gameboard tr:eq("+i[1]+") td:eq("+i[0]+")").text();r.push(d)}var c=r.join("");return c},highlightDraggedSelection:function(a,o){var r=t(a,o);for(var s in r){var n=r[s];e("#gameboard tr:eq("+n[1]+") td:eq("+n[0]+")").addClass("dragged-selection")}},clearDraggedSelection:function(){e("#gameboard td").removeClass("dragged-selection")},resetBoard:function(){console.log("resetBoard"),e("#gameboard td").removeClass(),n.gameboard.clearDraggedSelection(),n.gameboard.state="waiting"},lock:function(){this.resetBoard(),e("#gameboard td").off()}},canvas:{create:function(){var a=e("<canvas></canvas>").attr("id","gamecanvas");e("#gameboard-subcontainer").prepend(a);var t=e("#gameboard").position().top,o=e("#gameboard").css("margin","0 auto"),r=e("#gameboard").height()-2,s=e("#gameboard").width()-2;e("#gamecanvas").css("top",t+"px").css("left",o+"px").css("width",s+"px").css("height",r+"px").attr("width",s).attr("height",r)},circleWord:function(e,a){function t(e){return 26*e+13}console.log("circleword"),console.log(e,a),e=[t(e[0]),t(e[1])],a=[t(a[0]),t(a[1])];var o=document.getElementById("gamecanvas"),r=o.getContext("2d");r.beginPath(),r.lineCap="round",r.lineWidth=20,r.strokeStyle=this.rainbow(20,Math.floor(20*Math.random())+1),r.moveTo(e[0],e[1]),r.lineTo(a[0],a[1]),r.stroke()},rainbow:function(e,a){var t,o,r,s=a/e,n=~~(6*s),i=6*s-n,d=1-i;switch(n%6){case 0:t=1,o=i,r=0;break;case 1:t=d,o=1,r=0;break;case 2:t=0,o=1,r=i;break;case 3:t=0,o=d,r=1;break;case 4:t=i,o=0,r=1;break;case 5:t=1,o=0,r=d}var c="#"+("00"+(~~(255*t)).toString(16)).slice(-2)+("00"+(~~(255*o)).toString(16)).slice(-2)+("00"+(~~(255*r)).toString(16)).slice(-2);return c},clear:function(){e("#gamecanvas").remove(),this.create()}}};n.init()}}(jQuery,data,lineMath);