forked from Kolatzek/WPdoodlez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordsearch.min.js
1 lines (1 loc) · 5.67 KB
/
wordsearch.min.js
1
function searchLanguage(t){return codefirstLetter=t.charCodeAt(),codefirstLetter>=65&&codefirstLetter<=90||console.log("Letter not detected : "+t+":"+codefirstLetter),[65,90]}"function"!=typeof Math.rangeInt&&(Math.rangeInt=function(t,e){return void 0==e&&(e=t,t=0),Math.floor(Math.random()*(e-t+1))+t}),"function"!=typeof Object.merge&&(Object.merge=function(t,e){for(var r in t)e[r]=t[r];return e}),function(){"use strict";function t(t,e){if(this.wrapEl=t,this.wrapEl.classList.add("ws-area"),this.solved=0,this.settings=Object.merge(e,{directions:["W","N","WN","EN"],gridSize:10,words:["one","two","three","four","five"],clues:["eine","zwei","drei","vier","f\xfcnf"],wordsList:[],debug:!1}),this.parseWords(this.settings.gridSize)){for(var r=!1;!1==r;)this.initialize(),r=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.wordsList[r]=this.settings.words[r].trim(),this.settings.words[r]=this.settings.wordsList[r].trim().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;!1==s&&(t=!1,r=!1),++e>=this.settings.words.length&&(t=!1)}return r},t.prototype.addWord=function(t,e){var r,i,s=!0,n={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 o="UNKNOWN DIRECTION "+e+"!";alert(o),console.log(o)}for(var a=0;a<t.length;a++){var l=r+a*n[e][0],h=i+a*n[e][1],d=this.matrix[l][h].letter;"."==d||d==t[a]?this.matrix[l][h].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;e<t;e++)for(var r=0;r<t;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,n=i.height/2,o=i.getContext("2d");o.font="400 28px Arial",o.textAlign="center",o.textBaseline="middle",o.fillStyle="#333",o.fillText(this.matrix[t][r].letter,s,n),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=searchLanguage(this.settings.words[0].split("")[0]),e=0;e<this.settings.gridSize;e++)for(var r=0;r<this.settings.gridSize;r++)"."==this.matrix[e][r].letter&&(this.matrix[e][r].letter=String.fromCharCode(Math.rangeInt(t[0],t[1])))},t.prototype.getItems=function(t,e,r,i){var s=[];if(t===r||e===i||Math.abs(r-t)==Math.abs(i-e)){var n=t===r?0:r>t?1:-1,o=e===i?0:i>e?1:-1,a=t,l=e;s.push(this.getItem(a,l));do a+=n,l+=o,s.push(this.getItem(a,l));while(a!==r||l!==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;document.querySelector(".ws-area .ws-row:nth-child("+i+") .ws-col:nth-child("+s+")").classList.add("ws-found")}for(var n=document.querySelector(".ws-words").getElementsByTagName("li"),r=0;r<n.length;r++)e[0]==n[r].innerHTML.toUpperCase()&&n[r].innerHTML!="<del>"+n[r].innerHTML+"</del>"&&(n[r].innerHTML="<del>"+n[r].innerHTML+"</del> | "+this.settings.clues[r],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'><h6>Gl\xfcckwunsch</h6><p>Alle W\xf6rter gefunden</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,n=i.col+1,o=document.querySelector(".ws-area .ws-row:nth-child("+s+") .ws-col:nth-child("+n+")");o.className+=" ws-selected"}}}},t.prototype.onMouseup=function(){var t=this;return function(){t.selectFrom=null,t.clearHighlight(),t.lookup(t.selected),t.selected=[]}}}();