﻿var Tam=21;var Pal=20;var Tema=SieteLetras;var Letras=new Array('A','B','C','D','F','G','H','I','J','K','L','M','N','Ñ','O','P','Q','R','S','T','U','V','W','X','Z');var sentido=new Array(0,1,-1);var Solucion=new Array(Tam);var Docu=window.document;for(var i=0;i<Tam;i++)Solucion[i]=new Array(Tam);for(i=0;i<Tema.length;i++)Tema[i]=Tema[i].toUpperCase();function Construye(){var i,j;var Lista=new Array();var L=Letras.length;Pal=Docu.getElementById("Palabras").value;var Texto="<table align='center' cellpadding='0' cellspacing='0'>";for(i=0;i<Tam;i++)for(j=0;j<Tam;j++)Solucion[i][j]=null;for(i=0;i<Pal;i++)Lista.push(Tema.splice(Math.floor(Tema.length*Math.random()),1));for(i=0;i<Pal;i++){Tema.push(Lista[i]);Anade(Lista[i].toString())}Lista=Lista.sort();for(i=0;i<Tam;i++){Texto+="<tr>";for(j=0;j<Tam;j++)Texto+="<td class='A' id='C"+i+"_"+j+"' onclick='Valida("+i+","+j+");'>"+(Solucion[i][j]!=null?Solucion[i][j]:Letras[Math.floor(L*Math.random())])+"<\/td>";Texto+="<\/tr>"}Texto+="<\/table>";Docu.getElementById('tablero').innerHTML=Texto;Texto='';for(i=0;i<Pal;i++)Texto+="<span class='normal' id='"+Lista[i]+"' onclick='Tacha(this);'>"+Lista[i]+"<\/span><br>";Docu.getElementById('P').innerHTML=Texto}function Anade(palabra){var ox,oy,sx=0,sy=0;var i,j,l=sentido.length;var longitud=palabra.length;var PAL=palabra.split('');var vale=true;while(sx==0&&sy==0){sx=sentido[Math.floor(l*Math.random())];sy=sentido[Math.floor(l*Math.random())]}ox=Math.floor((Tam-longitud)*Math.random())+(sx<0?longitud:0);oy=Math.floor((Tam-longitud)*Math.random())+(sy<0?longitud:0);for(i=ox,j=oy,l=0;l<longitud;i+=sx,j+=sy,l++)if(Solucion[i][j]!=null&&Solucion[i][j]!=PAL[l])vale=false;if(vale==true){for(i=ox,j=oy,l=0;l<longitud;i+=sx,j+=sy,l++)Solucion[i][j]=PAL[l]}else Anade(palabra)}function Valida(x,y){var ide=Docu.getElementById("C"+x+"_"+y);if(/^yellow|^red/.test(ide.style.backgroundColor))ide.style.backgroundColor='transparent';else ide.style.backgroundColor=(Solucion[x][y]!=null?'yellow':'red')}function Comprueba(){var i,j,ide;var estado=true;for(i=0;i<Tam;i++)for(j=0;j<Tam;j++){if(Solucion[i][j]!=null){ide=Docu.getElementById("C"+i+"_"+j);if(!/^yellow/.test(ide.style.backgroundColor)){ide.style.backgroundColor='lime';estado=false}}}alert((estado==true?'Finalizado Correctamente':'No finalizado'))}function Cambia(obj){var i=0;Tema=eval(obj[obj.selectedIndex].value);while(Tema[i])if(Tema[i].length>15)Tema.splice(i,1);else{Tema[i]=Tema[i].toUpperCase().replace(/ |-|,|:/g,'');i++}}function Tacha(obj){obj.className=(obj.className=='normal'?'tachado':'normal')}function Imprime(){var V=window.open('','Impresion','width=100,height=100,resizable=yes,toolbar=no,scrollbars=no');var Texto="<html><head><title>Sopa de Letras<\/title><link rel='stylesheet' type='text\/css' href='SopaLetras.css'\/><\/head><body><table align='center'><tr><td id='tablero' class='sopa'>"+Docu.getElementById('tablero').innerHTML+"<\/td><td id='P' class='borde'>"+Docu.getElementById('P').innerHTML+"<\/td><\/tr><\/table><\/body><\/html>";V.document.write(Texto);V.document.close();V.print();V.close()}

