-
Notifications
You must be signed in to change notification settings - Fork 5
/
crossword.min.js
1 lines (1 loc) · 6.98 KB
/
crossword.min.js
1
function CrosswordCell(r){this["char"]=r,this.across=null,this.down=null}function CrosswordCellNode(r,e){this.is_start_of_word=r,this.index=e}function WordElement(r,e){this.word=r,this.index=e}function Crossword(r,e){var t,n=50,o=50,l={};this.getSquareGrid=function(r){for(var e=null,t=0,n=0;r>n;n++){var o=this.getGrid(1);if(null!=o){var l=1*Math.min(o.length,o[0].length)/Math.max(o.length,o[0].length);if(l>t&&(e=o,t=l),1==t)break}}return e},this.getGrid=function(r){if(!w[0])return null;for(var e=0;r>e;e++){f();var n=h(),o=Math.floor(g.length/2),l=Math.floor(g[0].length/2),a=w[0];if("across"==n?l-=Math.floor(a.word.length/2):o-=Math.floor(a.word.length/2),u(a.word,o,l,n)===!1)return t=[a],null;i(a.word,a.index,o,l,n);var s=[];s.push(w.slice(1));for(var v=0;v<s.length;v++){word_has_been_added_to_grid=!1;for(var m=0;m<s[v].length;m++){var a=s[v][m],p=c(a.word);if(p){var o=p.row,l=p.col,_=p.direction;i(a.word,a.index,o,l,_),word_has_been_added_to_grid=!0}else s.length-1==v&&s.push([]),s[v+1].push(a)}if(!word_has_been_added_to_grid)break}if(word_has_been_added_to_grid)return d()}return t=s[s.length-1],null},this.getBadWords=function(){return t},this.getLegend=function(t){for(var n={across:[],down:[]},o=1,l=0;l<t.length;l++)for(var d=0;d<t[l].length;d++){var a=t[l][d],i=!1;for(var s in n)if(a&&a[s]&&a[s].is_start_of_word){var u=a[s].index;n[s].push({position:o,index:u,clue:e[u],word:r[u]}),i=!0}i&&o++}return n};for(var d=function(){for(var r=n-1,e=0,t=o-1,l=0,d=0;n>d;d++)for(var a=0;o>a;a++){var i=g[d][a];null!=i&&(r>d&&(r=d),d>e&&(e=d),t>a&&(t=a),a>l&&(l=a))}for(var s=e-r+1,u=l-t+1,h=new Array(s),d=0;s>d;d++)for(var a=0;u>a;a++)h[d]=new Array(u);for(var d=r,c=0;s>c;d++,c++)for(var a=t,f=0;u>f;a++,f++)h[c][f]=g[d][a];return h},a=function(r,e,t,n,o,d){var a=r.charAt(t);null==g[n][o]&&(g[n][o]=new CrosswordCell(a),l[a]||(l[a]=[]),l[a].push({row:n,col:o}));var i=0==t;g[n][o][d]=new CrosswordCellNode(i,e)},i=function(r,e,t,n,o){if("across"==o)for(var l=n,d=0;l<n+r.length;l++,d++)a(r,e,d,t,l,o);else{if("down"!=o)throw"Invalid Direction";for(var i=t,d=0;i<t+r.length;i++,d++)a(r,e,d,i,n,o)}},s=function(r,e,t){return null==g[e][t]?0:g[e][t]["char"]==r?1:!1},u=function(r,e,t,n){if(0>e||e>=g.length||0>t||t>=g[e].length)return!1;if("across"==n){if(t+r.length>g[e].length)return!1;if(t-1>=0&&null!=g[e][t-1])return!1;if(t+r.length<g[e].length&&null!=g[e][t+r.length])return!1;for(var o=e-1,l=t,d=0;o>=0&&l<t+r.length;l++,d++){var a=null==g[o][l],i=null!=g[e][l]&&g[e][l]["char"]==r.charAt(d),u=a||i;if(!u)return!1}for(var o=e+1,l=t,d=0;o<g.length&&l<t+r.length;l++,d++){var a=null==g[o][l],i=null!=g[e][l]&&g[e][l]["char"]==r.charAt(d),u=a||i;if(!u)return!1}for(var h=0,l=t,d=0;l<t+r.length;l++,d++){var c=s(r.charAt(d),e,l);if(c===!1)return!1;h+=c}}else{if("down"!=n)throw"Invalid Direction";if(e+r.length>g.length)return!1;if(e-1>=0&&null!=g[e-1][t])return!1;if(e+r.length<g.length&&null!=g[e+r.length][t])return!1;for(var l=t-1,o=e,d=0;l>=0&&o<e+r.length;o++,d++){var a=null==g[o][l],i=null!=g[o][t]&&g[o][t]["char"]==r.charAt(d),u=a||i;if(!u)return!1}for(var l=t+1,o=e,d=0;o<e+r.length&&l<g[o].length;o++,d++){var a=null==g[o][l],i=null!=g[o][t]&&g[o][t]["char"]==r.charAt(d),u=a||i;if(!u)return!1}for(var h=0,o=e,d=0;o<e+r.length;o++,d++){var c=s(r.charAt(d,1),o,t);if(c===!1)return!1;h+=c}}return h},h=function(){return Math.floor(2*Math.random())?"across":"down"},c=function(r){for(var e=[],t=0;t<r.length;t++){var n=l[r.charAt(t)];if(n)for(var o=0;o<n.length;o++){var d=n[o],a=d.row,i=d.col,s=u(r,a,i-t,"across"),h=u(r,a-t,i,"down");s!==!1&&e.push({intersections:s,row:a,col:i-t,direction:"across"}),h!==!1&&e.push({intersections:h,row:a-t,col:i,direction:"down"})}}if(0==e.length)return!1;var c=e[Math.floor(Math.random()*e.length)];return c},f=function(){for(var r=0;r<g.length;r++)for(var e=0;e<g[r].length;e++)g[r][e]=null;l={}},g=new Array(n),v=0;n>v;v++)g[v]=new Array(o);for(var w=[],v=0;v<r.length;v++)w.push(new WordElement(r[v],v));w.sort(function(r,e){return e.word.length-r.word.length})}function createCrossword(r,e){var t=new Crossword(r,e),n=10,o=t.getSquareGrid(n);if(null==o){for(var l=t.getBadWords(),d=[],a=0;a<l.length;a++)d.push(l[a].word);return document.getElementById("crossword").innerHTML="Add more words - the words <i>"+d.join(",")+"</i> could not be fitted into the crossword.",void(document.getElementById("clues-wrapper").innerHTML='<div id="clues-header"></div><table id="clues"></table>')}var i=!0;document.getElementById("crossword").innerHTML=CrosswordUtils.toHtml(o,i);var s=t.getLegend(o);addLegendToPage(s)}function addLegendToPage(r){document.getElementById("clues-header").innerHTML="<div id='scissors'><div></div></div><h1 class='titles'>Teacher Sheet</h1></br>",document.getElementById("clues").innerHTML='<thead><tr><th>Across</th><th>Down</th></tr></thead><tbody><tr><td><ul id="across"></ul></td><td><ul id="down"></ul></td></tr></tbody>';for(var e in r){for(var t=[],n=0;n<r[e].length;n++)t.push("<li><strong>"+r[e][n].position+".</strong> "+r[e][n].clue+"</li>");document.getElementById(e).innerHTML=t.join("\n")}}function addWord(r){for(var e=0;e<arguments.length;++e){var r=arguments[e],t=document.createElement("li");t.innerHTML="<label>"+r+'</label><button class="destroy"></button>',list.insertBefore(t,list.childNodes[0]),wordArray.push(r),updateCount()}createCrossword(wordArray,wordArray)}function updateCount(){list.children.length>1?count.innerHTML=list.children.length+" words":0==list.children.length?count.innerHTML="hit enter to add word":count.innerHTML=list.children.length+" word"}var CrosswordUtils={PATH_TO_PNGS_OF_NUMBERS:"numbers/",toHtml:function(r,e){if(null!=r){for(var t=[],n=0;2>n;n++){0==n?t.push("<h1 class='titles'>Student A</h1>"):t.push("<div id='scissors'><div></div></div><h1 class='titles'>Student B</h1>"),t.push("<table class='crossword'>");for(var o=1,l=0;l<r.length;l++){t.push("<tr>");for(var d=0;d<r[l].length;d++){var a=r[l][d],i=!1;if(null==a)var s=" ",u="no-border";else{var s=a["char"],u="";1==n?a.across||(s=""):a.down||(s="");var i=a.across&&a.across.is_start_of_word||a.down&&a.down.is_start_of_word}if(i){var h=CrosswordUtils.PATH_TO_PNGS_OF_NUMBERS+o+".png";t.push("<td class='"+u+"' style=\"background-image:url('"+h+"')\">"),o++}else t.push("<td class='"+u+"'>");e?t.push(s):t.push(" ")}t.push("</tr>")}t.push("</table></br></br>")}return t.join("\n")}}},newWord=document.getElementById("word-new"),list=document.getElementById("word-list"),count=document.getElementById("word-count"),wordArray=[],demo=document.getElementById("demo");list.addEventListener("click",function(r){r.target!==r.currentTarget&&"destroy"==r.target.className&&(wordArray.splice(wordArray.indexOf(r.target.parentElement.getElementsByTagName("LABEL")[0].innerHTML),1),r.target.parentElement.parentNode.removeChild(r.target.parentElement),updateCount(),createCrossword(wordArray,wordArray)),r.stopPropagation()}),demo.addEventListener("click",function(r){addWord("learning","english","speaking","doing")}),newWord.addEventListener("keypress",function(r){13==r.keyCode&&""!==newWord.value.trim()&&(addWord(newWord.value),newWord.value="")});