summaryrefslogtreecommitdiff
path: root/js/wordsearch.min.js
diff options
context:
space:
mode:
authorLi Zhineng <[email protected]>2016-02-11 11:31:23 +0800
committerLi Zhineng <[email protected]>2016-02-11 11:31:23 +0800
commit4e0eb400d47e9ef3ded2b719348c0aa7b56a4530 (patch)
treed094af77cc9a17bbfaa7ac0d68040f5d4c91a7a7 /js/wordsearch.min.js
parente46f1f131101a24910c5073d6cb6ff7189891663 (diff)
parent8c81bfbcdf3451ae634927a1270c40941ba02d5e (diff)
downloadword-search-game-4e0eb400d47e9ef3ded2b719348c0aa7b56a4530.tar.gz
word-search-game-4e0eb400d47e9ef3ded2b719348c0aa7b56a4530.zip
Merge pull request #1 from stvnthomas/master
Functionality
Diffstat (limited to 'js/wordsearch.min.js')
-rw-r--r--js/wordsearch.min.js2
1 files changed, 1 insertions, 1 deletions
diff --git a/js/wordsearch.min.js b/js/wordsearch.min.js
index 76854e3..2511d88 100644
--- a/js/wordsearch.min.js
+++ b/js/wordsearch.min.js
@@ -1 +1 @@
-(function(){Element.prototype.wordSeach=function(b){return new a(this,b)};function a(b,d){this.wrapEl=b;this.wrapEl.classList.add("ws-area");var c={"directions":["W","N","WN","EN"],"gridSize":10,"words":["one","two","three","four","five"],"debug":false};this.settings=Object.merge(d,c);if(this.parseWords(this.settings.gridSize)){var e=false;while(e==false){this.initialize();e=this.addWords()}if(!this.settings.debug){this.fillUpFools()}this.drawmatrix()}}a.prototype.parseWords=function(e){var b=true;for(var c=0;c<this.settings.words.length;c++){this.settings.words[c]=this.settings.words[c].toUpperCase();var d=this.settings.words[c];if(d.length>e){alert("The length of word `"+d+"` is overflow the gridSize.");console.error("The length of word `"+d+"` is overflow the gridSize.");b=false}}return b};a.prototype.addWords=function(){var e=true,c=0,f=true;while(e){var d=this.settings.directions[Math.rangeInt(this.settings.directions.length-1)],b=this.addWord(this.settings.words[c],d),f=true;if(b==false){e=false;f=false}c++;if(c>=this.settings.words.length){e=false}}return f};a.prototype.addWord=function(b,j){var d=true,g={"W":[0,1],"N":[1,0],"WN":[1,1],"EN":[1,-1]},m,e;switch(j){case"W":var m=Math.rangeInt(this.settings.gridSize-1),e=Math.rangeInt(this.settings.gridSize-b.length);break;case"N":var m=Math.rangeInt(this.settings.gridSize-b.length),e=Math.rangeInt(this.settings.gridSize-1);break;case"WN":var m=Math.rangeInt(this.settings.gridSize-b.length),e=Math.rangeInt(this.settings.gridSize-b.length);break;case"EN":var m=Math.rangeInt(this.settings.gridSize-b.length),e=Math.rangeInt(b.length-1,this.settings.gridSize-1);break;default:var h="UNKNOWN DIRECTION "+j+"!";alert(h);console.log(h);break}for(var f=0;f<b.length;f++){var l=m+f*g[j][0],c=e+f*g[j][1];var k=this.matrix[l][c].letter;if(k=="."||k==b[f]){this.matrix[l][c].letter=b[f]}else{d=false}}return d};a.prototype.initialize=function(){this.matrix=[];this.selectFrom=null;this.selected=[];this.initmatrix(this.settings.gridSize)};a.prototype.initmatrix=function(c){for(var e=0;e<c;e++){for(var b=0;b<c;b++){var d={letter:".",row:e,col:b};if(!this.matrix[e]){this.matrix[e]=[]}this.matrix[e][b]=d}}};a.prototype.drawmatrix=function(){for(var g=0;g<this.settings.gridSize;g++){var e=document.createElement("div");e.setAttribute("class","ws-row");this.wrapEl.appendChild(e);for(var d=0;d<this.settings.gridSize;d++){var f=document.createElement("canvas");f.setAttribute("class","ws-col");f.setAttribute("width",40);f.setAttribute("height",40);var b=f.width/2,h=f.height/2;var c=f.getContext("2d");c.font="400 28px Calibri";c.textAlign="center";c.textBaseline="middle";c.fillStyle="#333";c.fillText(this.matrix[g][d].letter,b,h);f.addEventListener("mousedown",this.onMousedown(this.matrix[g][d]));f.addEventListener("mouseover",this.onMouseover(this.matrix[g][d]));f.addEventListener("mouseup",this.onMouseup());e.appendChild(f)}}};a.prototype.fillUpFools=function(){for(var c=0;c<this.settings.gridSize;c++){for(var b=0;b<this.settings.gridSize;b++){if(this.matrix[c][b].letter=="."){this.matrix[c][b].letter=String.fromCharCode(Math.rangeInt(65,90))}}}};a.prototype.getItems=function(e,c,i,h){var d=[];if(e===i||c===h||Math.abs(i-e)==Math.abs(h-c)){var f=(e===i)?0:(i>e)?1:-1,g=(c===h)?0:(h>c)?1:-1,j=e,b=c;d.push(this.getItem(j,b));do{j+=f;b+=g;d.push(this.getItem(j,b))}while(j!==i||b!==h)}return d};a.prototype.getItem=function(c,b){return(this.matrix[c]?this.matrix[c][b]:undefined)};a.prototype.clearHighlight=function(){var b=document.querySelectorAll(".ws-selected");for(var c=0;c<b.length;c++){b[c].classList.remove("ws-selected")}};a.prototype.lookup=function(e){var g=[""];for(var c=0;c<e.length;c++){g[0]+=e[c].letter}g.push(g[0].split("").reverse().join(""));if(this.settings.words.indexOf(g[0])>-1||this.settings.words.indexOf(g[1])>-1){for(var c=0;c<e.length;c++){var f=e[c].row+1,b=e[c].col+1,d=document.querySelector(".ws-area .ws-row:nth-child("+f+") .ws-col:nth-child("+b+")");d.classList.add("ws-found")}}};a.prototype.onMousedown=function(b){var c=this;return function(){c.selectFrom=b}};a.prototype.onMouseover=function(b){var c=this;return function(){if(c.selectFrom){c.selected=c.getItems(c.selectFrom.row,c.selectFrom.col,b.row,b.col);c.clearHighlight();for(var e=0;e<c.selected.length;e++){var g=c.selected[e],h=g.row+1,d=g.col+1,f=document.querySelector(".ws-area .ws-row:nth-child("+h+") .ws-col:nth-child("+d+")");f.className+=" ws-selected"}}}};a.prototype.onMouseup=function(){var b=this;return function(){b.selectFrom=null;b.clearHighlight();b.lookup(b.selected);b.selected=[]}}})(); \ No newline at end of file
+!function(){"use strict";function t(t,e){this.wrapEl=t,this.wrapEl.classList.add("ws-area"),this.solved=0;var r={directions:["W","N","WN","EN"],gridSize:10,words:["one","two","three","four","five"],debug:!1};if(this.settings=Object.merge(e,r),this.parseWords(this.settings.gridSize)){for(var i=!1;0==i;)this.initialize(),i=this.addWords();this.settings.debug||this.fillUpFools(),this.drawmatrix()}}Element.prototype.wordSearch=function(e){return new t(this,e)},t.prototype.parseWords=function(t){for(var e=!0,r=0;r<this.settings.words.length;r++){this.settings.words[r]=this.settings.words[r].toUpperCase();var i=this.settings.words[r];i.length>t&&(alert("The length of word `"+i+"` is overflow the gridSize."),console.error("The length of word `"+i+"` is overflow the gridSize."),e=!1)}return e},t.prototype.addWords=function(){for(var t=!0,e=0,r=!0;t;){var i=this.settings.directions[Math.rangeInt(this.settings.directions.length-1)],s=this.addWord(this.settings.words[e],i),r=!0;0==s&&(t=!1,r=!1),e++,e>=this.settings.words.length&&(t=!1)}return r},t.prototype.addWord=function(t,e){var r,i,s=!0,o={W:[0,1],N:[1,0],WN:[1,1],EN:[1,-1]};switch(e){case"W":var r=Math.rangeInt(this.settings.gridSize-1),i=Math.rangeInt(this.settings.gridSize-t.length);break;case"N":var r=Math.rangeInt(this.settings.gridSize-t.length),i=Math.rangeInt(this.settings.gridSize-1);break;case"WN":var r=Math.rangeInt(this.settings.gridSize-t.length),i=Math.rangeInt(this.settings.gridSize-t.length);break;case"EN":var r=Math.rangeInt(this.settings.gridSize-t.length),i=Math.rangeInt(t.length-1,this.settings.gridSize-1);break;default:var n="UNKNOWN DIRECTION "+e+"!";alert(n),console.log(n)}for(var a=0;a<t.length;a++){var h=r+a*o[e][0],l=i+a*o[e][1],d=this.matrix[h][l].letter;"."==d||d==t[a]?this.matrix[h][l].letter=t[a]:s=!1}return s},t.prototype.initialize=function(){this.matrix=[],this.selectFrom=null,this.selected=[],this.initmatrix(this.settings.gridSize)},t.prototype.initmatrix=function(t){for(var e=0;t>e;e++)for(var r=0;t>r;r++){var i={letter:".",row:e,col:r};this.matrix[e]||(this.matrix[e]=[]),this.matrix[e][r]=i}},t.prototype.drawmatrix=function(){for(var t=0;t<this.settings.gridSize;t++){var e=document.createElement("div");e.setAttribute("class","ws-row"),this.wrapEl.appendChild(e);for(var r=0;r<this.settings.gridSize;r++){var i=document.createElement("canvas");i.setAttribute("class","ws-col"),i.setAttribute("width",40),i.setAttribute("height",40);var s=i.width/2,o=i.height/2,n=i.getContext("2d");n.font="400 28px Calibri",n.textAlign="center",n.textBaseline="middle",n.fillStyle="#333",n.fillText(this.matrix[t][r].letter,s,o),i.addEventListener("mousedown",this.onMousedown(this.matrix[t][r])),i.addEventListener("mouseover",this.onMouseover(this.matrix[t][r])),i.addEventListener("mouseup",this.onMouseup()),e.appendChild(i)}}},t.prototype.fillUpFools=function(){for(var t=0;t<this.settings.gridSize;t++)for(var e=0;e<this.settings.gridSize;e++)"."==this.matrix[t][e].letter&&(this.matrix[t][e].letter=String.fromCharCode(Math.rangeInt(65,90)))},t.prototype.getItems=function(t,e,r,i){var s=[];if(t===r||e===i||Math.abs(r-t)==Math.abs(i-e)){var o=t===r?0:r>t?1:-1,n=e===i?0:i>e?1:-1,a=t,h=e;s.push(this.getItem(a,h));do a+=o,h+=n,s.push(this.getItem(a,h));while(a!==r||h!==i)}return s},t.prototype.getItem=function(t,e){return this.matrix[t]?this.matrix[t][e]:void 0},t.prototype.clearHighlight=function(){for(var t=document.querySelectorAll(".ws-selected"),e=0;e<t.length;e++)t[e].classList.remove("ws-selected")},t.prototype.lookup=function(t){for(var e=[""],r=0;r<t.length;r++)e[0]+=t[r].letter;if(e.push(e[0].split("").reverse().join("")),this.settings.words.indexOf(e[0])>-1||this.settings.words.indexOf(e[1])>-1){for(var r=0;r<t.length;r++){var i=t[r].row+1,s=t[r].col+1,o=document.querySelector(".ws-area .ws-row:nth-child("+i+") .ws-col:nth-child("+s+")");o.classList.add("ws-found")}for(var n=document.querySelector(".ws-words"),a=n.getElementsByTagName("li"),r=0;r<a.length;r++)e[0].toLowerCase()==a[r].innerHTML.toLowerCase()&&(a[r].innerHTML="<del>"+a[r].innerHTML+"</del>");this.solved++,this.solved==this.settings.words.length&&this.gameOver()}},t.prototype.gameOver=function(){var t=document.createElement("div");t.setAttribute("id","ws-game-over-outer"),t.setAttribute("class","ws-game-over-outer"),this.wrapEl.parentNode.appendChild(t);var t=document.getElementById("ws-game-over-outer");t.innerHTML="<div class='ws-game-over-inner' id='ws-game-over-inner'><div class='ws-game-over' id='ws-game-over'><h2>Congratulations!</h2><p>You've found all of the words!</p></div></div>"},t.prototype.onMousedown=function(t){var e=this;return function(){e.selectFrom=t}},t.prototype.onMouseover=function(t){var e=this;return function(){if(e.selectFrom){e.selected=e.getItems(e.selectFrom.row,e.selectFrom.col,t.row,t.col),e.clearHighlight();for(var r=0;r<e.selected.length;r++){var i=e.selected[r],s=i.row+1,o=i.col+1,n=document.querySelector(".ws-area .ws-row:nth-child("+s+") .ws-col:nth-child("+o+")");n.className+=" ws-selected"}}}},t.prototype.onMouseup=function(){var t=this;return function(){t.selectFrom=null,t.clearHighlight(),t.lookup(t.selected),t.selected=[]}}}(); \ No newline at end of file