let my={}
function crossnumberMain(mode){let version='0.79';this.mode=typeof mode!=='undefined'?mode:'1';w=350;h=350;my.opts={gameN:0,bdSz:5}
my.gameN=optGet('gameN')
my.games=[{name:'easy',dict:'nums',hard:1,xn:8,yn:5,clueN:8,blankPct:50},{name:'medium',dict:'nums',hard:1,xn:10,yn:7,clueN:20,blankPct:40},{name:'hard',dict:'nums',hard:1,xn:10,yn:9,clueN:26,blankPct:20},]
my.game=my.games[my.gameN]
my.tileLoClr='#ffff00';my.tileHiClr='#aaaaff';my.soundQ=true
my.activeQ=true
let s="";my.sndHome=(document.domain=='localhost')?'/mathsisfun/images/sounds/':'/images/sounds/'
s+='';s+='';s+='';my.snds=[];s+=arrowBoxHTML()
s+='
';document.write(s);my.bd=[]
my.boxWd=20
gameNew()}
function gameWindoneQ(){var n=cluesRemaining()
console.log('gameWinCheck',n)
if(n<15){msg(n+' left to go')}else{msg('')}
if(n==0){msg('You solved it!','gold')
soundPlay('sndWin')}}
function msg(s,clr){clr=typeof clr!=='undefined'?clr:'black'
var div=document.getElementById('msg')
div.innerHTML=s
div.style.color=clr}
function gameNew(){my.clues=[]
let dict=my.game.dict;switch(dict){case "nums":loadDictNums(dict);afterDictLoaded();break;default:}}
function loadDictNums(type){let maxLen=my.game.xn*my.game.yn
console.log("maxLen="+maxLen);my.dict=[]
for(let i=0;i<50;i++){let len=randomInt(4,maxLen);my.dict.push(randomNumber(len));}}
function randomNumber(len){let s=''
for(let i=0;i{if(clue.getClueStr()==myclue.getClueStr())oldQ=true})
if(!oldQ)addWord(clue,false);attempts++}while(attempts<300&&my.clues.length'
s+='
'
s+=dirn
s+='
'
for(let i=0;i'
s+='
'
s+=clue.clueNo.toString()
s+=": ";s+='
'
let clr=clue.doneQ()?'blue':'red'
s+='
'
s+=clue.getClueStr();s+='
'
s+=''}}
s+=''}
return s}
function cluesList(){let s="";for(let i=0;i=my.game.xn||row<0||row>=my.game.yn){badTouchQ=true;break;}
let tile=my.bd[col][row]
if(tile.blankQ){badTouchQ=true;break;}
if(tile.str.length==1){if(tile.str==letter){if(tile.dirnStr.includes(dirnName)){badTouchQ=true;break;}else{score+=1;touches+=1;goodTouchQ=true;}}else{badTouchQ=true;break;}}}
let col=xn+word.length*colInc;let row=yn+word.length*rowInc;if(validTileQ(col,row)){if(my.bd[col][row].str.length>0)badTouchQ=true
if(my.bd[col][row].blankQ)score+=0.5}else{}
if((!mustTouchQ||goodTouchQ)&&!badTouchQ){if(score>bestScore){best={col:xn,row:yn,colInc:colInc,rowInc:rowInc,dirnName:dirnName}
bestScore=score;}}}}}
if(best==null){}else{clue.col=best.col
clue.row=best.row
clue.colInc=best.colInc
clue.rowInc=best.rowInc
my.clues.push(clue);for(let k=0;k=my.game.xn||row<0||row>=my.game.yn)return false
return true}
function cluesNumber(){my.clues.sort(function(a,b){if(a.row{if(clue.row==prevRow&&clue.col==prevCol){}else{clueNo++;}
prevRow=clue.row;prevCol=clue.col;clue.clueNo=clueNo;let tile=my.bd[clue.col][clue.row];tile.setCornerText(clue.clueNo.toString());clue.setDirn();})}
class Clue{constructor(){this.typ='?'
this.num=0
this.clueStr="";this.clueNo=my.clueNo++}
makeRandom(){let fns=[[this.makeTimes,2],[this.makeNaturalSequence,1],[this.makeAddNum,2],[this.makeAddClues,2],[this.makeShuffleDigits,1],[this.makeAddDigits,2]];let chanceTot=0;for(let i=0;ichance)break;}
let choice=i;switch(choice){case 0:this.makeTimes();break;case 1:this.makeNaturalSequence();break;case 2:this.makeAddNum();break;case 3:this.makeAddClues();break;case 4:this.makeShuffleDigits();break;case 5:this.makeAddDigits();break;default:}}
makeNum(n){this.typ="num";this.num=n;this.clueStr=this.num.toString();}
makeShuffleDigits(){this.fromClue1=this.getRandomClue();let sFrom=this.fromClue1.num.toString()
let chars=sFrom.split('');let s="";let attempts=0;do{chars.sort(function(){return 0.5-Math.random();});s=chars.join('');attempts++;}while((s==sFrom||s.charAt(0)=="0")&&attempts<9);if(attempts>=9){this.makeAddNum();return;}
this.typ="shuffle";this.num=Number(s);this.len=s.length
this.clueStr="Shuffled digits of $1";}
make2Digit(){this.typ="2dig";this.num=randomInt(10,99);this.len=2
this.clueStr="Two digits";}
make3Digit(){this.typ="3dig";this.num=randomInt(100,999);this.len=2
this.clueStr="Three digits";}
makeNaturalSequence(){this.len=randomInt(2,4);this.upQ=(Math.random()>0.5)
let sttVal=this.upQ?randomInt(1,9-this.len):randomInt(this.len,9);let s="";for(let i=0;i=9){console.log("makeAddDigits too many attempts");this.makeAddNum();return;}
this.typ="add-digits";this.num=nSum;this.len=this.num.toString().length
this.clueStr="Add digits of $1";}
makeAddClues(){if(my.clues.length<2){this.makeAddNum();return;}
this.fromClue1=this.getRandomClue();do{this.fromClue2=this.getRandomClue();}while(this.fromClue2==this.fromClue1);this.typ="add-clues";this.num=this.fromClue1.num+this.fromClue2.num;this.len=this.num.toString().length
this.clueStr="$1 plus $2";}
makeNumTimesNum(){let num1=randomInt(2,11);let num2=randomInt(2,11);this.typ="nxn";this.num=num1*num2;this.len=this.num.toString().length
this.clueStr=num1.toString()+" times "+num2.toString();}
setDirn(){if(this.colInc>0){this.dirn="across";}
if(this.rowInc>0){this.dirn="down";}}
getStr(){return this.num.toString();}
getClueStr(){let s=this.clueStr;if(this.fromClue1!=null)s=s.split("$1").join(''+this.fromClue1.getLocationStr()+'');if(this.fromClue2!=null)s=s.split("$2").join(''+this.fromClue2.getLocationStr()+'');return s;}
getLocationStr(){let s=this.clueNo.toString();s+=" ";s+=this.getDirnStr()
return s;}
getDirnStr(){if(this.colInc>0)return "across"
if(this.rowInc>0)return "down"
return ''}
getRandomClue(){return my.clues[randomInt(0,my.clues.length-1)];}
bdStr(){let s=''
for(let i=0;i0){if(clueNewQ("across"))news.push('a')}}}
console.log('bdExtra',news)}
function clueNewQ(dirnStr){return true}
function bdBlank(){let shows=[]
for(var xn=0;xn0)shows.push(tile)}}
shows.sort(function(){return 0.5-Math.random();});let blankN=(shows.length*my.game.blankPct/100)<<0
for(let i=0;i=blankN)show.str=''}}
function bdDraw(){if(my.bd.length==0)return
for(var xn=0;xn';s+='';s+=radioHTML('Difficulty:','game',my.games,my.gameN,'radioClick');s+='
';s+='New game? '
s+='';s+=' '
s+='';s+='
';s+='';return s;}
function optPop(){console.log("optpop");var pop=document.getElementById('optpop');pop.style.transitionDuration="0.3s";pop.style.opacity=1;pop.style.zIndex=102;pop.style.left=(w-340)/2+'px';}
function optYes(){var pop=document.getElementById('optpop');pop.style.opacity=0;pop.style.zIndex=1;pop.style.left='-999px';my.gameN=radioNGet('game')
console.log('optYes',my.gameN)
optSet('gameN',my.gameN)
my.game=my.games[my.gameN]
gameNew()}
function optNo(){var pop=document.getElementById('optpop');pop.style.opacity=0;pop.style.zIndex=1;pop.style.left='-999px';}
function randomInt(min,max){return Math.floor(Math.random()*(max-min+1))+min;}
function radioHTML(prompt,id,lbls,checkN,func){let s='';s+='
';s+='
';s+=prompt;s+='
';s+='
';for(let i=0;i';s+='';s+=' ';}
s+='
';s+='
';return s;}
function radioNGet(name){var div=document.querySelector('input[name="'+name+'"]:checked')
var id=div.id
var n=(id.match(/\d+$/)||[]).pop();return n}
function radioClick(n){}
function arrowBoxHTML(){var s='';var bgClr='#cdf'
var borderClr='darkblue'
s+='';return s;}
function showOpts(me){console.log('showOpts',me)
var div=document.getElementById('ansBox')
if(me.origQ){div.style.visibility='hidden';return}
var dadDiv=div.parentElement
let dadRect=dadDiv.getBoundingClientRect();var bdDiv=document.getElementById('board')
let bdRect=bdDiv.getBoundingClientRect();console.log('dadRect,bdRect',dadRect,bdRect)
div.style.left=((bdRect.left-dadRect.left)+boxLeft(me.xn-0.67))+'px'
if(me.yn<3){div.style.top=((bdRect.top-dadRect.top)+boxTop(me.yn+1.5))+'px'
div.classList.remove("arrowBot")
div.classList.add("arrowTop")}else{div.style.top=((bdRect.top-dadRect.top)+boxTop(me.yn-2.9))+'px'
div.classList.remove("arrowTop")
div.classList.add("arrowBot")}
div.style.visibility='visible';var s='';var anss=['-',0,'X',1,2,3,4,5,6,7,8,9];var n=0;for(var i=0;i<4;i++){s+='