let my={} function init(){let version='0.68' my.nums=['0','1','2','3','4','5','6','7','8','9'] my.ltrs=['A','B','C','D','E','F'] my.clrs=[['Med Blue','#88aaff'],['Orange','#FFA500'],['Gold','#ffd700'],['Yellow','#ffff00'],['Light Blue','#ADD8ff'],['Blue','#0000cc'],['Red','#FF0000'],['Pink','#FFaaaa'],['Dark Blue','#000066'],] my.games=[{mode:'3Num',title:'3 Codes: 0 to 9',type:'alpha',codeLen:3,codes:my.nums},{mode:'4Num',title:'4 Codes: 0 to 9',type:'alpha',codeLen:4,codes:my.nums},{mode:'5Num',title:'5 Codes: 0 to 9',type:'alpha',codeLen:5,codes:my.nums},{mode:'3A2F',title:'3 Codes: A to F',type:'alpha',codeLen:3,codes:my.ltrs},{mode:'4A2F',title:'4 Codes: A to F',type:'alpha',codeLen:4,codes:my.ltrs},{mode:'5A2F',title:'5 Codes: A to F',type:'alpha',codeLen:5,codes:my.ltrs},{mode:'3Clr',title:'3 out of 9 Colors',type:'clr',codeLen:3,codes:my.clrs},{mode:'4Clr',title:'4 out of 9 Colors',type:'clr',codeLen:4,codes:my.clrs},{mode:'5Clr',title:'5 out of 9 Colors',type:'clr',codeLen:5,codes:my.clrs},] my.keybdQ=false my.imgHome=(document.domain=='localhost'?'/mathsisfun':'')+'/images/style/' let w=380 let h=300 let s='' s+='' s+='
' s+='
' s+='' s+='' s+='' s+='
' s+=popHTML() s+=wrap({cls:'copyrt',style:'margin-top:3px;'},`© 2022 Rod Pierce v${version}`) s=wrap({cls:'js',style:'width:'+w+'px; min-height:'+h+'px;'},s) docInsert(s) window.addEventListener('keydown',key,false) my.inGameQ=false gameChg(1)} function key(ev){if(!my.inGameQ)return let keyCode=ev.keyCode switch(keyCode){case 37:rowCurr().moveSel(-1) update() ev.preventDefault() break case 39:rowCurr().moveSel(1) update() ev.preventDefault() break case 38:rowCurr().incr(1) update() ev.preventDefault() break case 40:rowCurr().incr(-1) update() ev.preventDefault() break default:} if(keyCode==9||keyCode==13||keyCode==32){} if(keyCode>=96&&keyCode<=105){rowCurr().setVal(keyCode-96)} if(keyCode>=48&&keyCode<=57){rowCurr().setVal(keyCode-48)}} function options(){optpop()} function optpop(){let pop=document.getElementById('optpop') pop.style.transitionDuration='0.3s' pop.style.opacity=1 pop.style.zIndex=12 pop.style.left='10px'} function popYes(){let pop=document.getElementById('optpop') pop.style.opacity=0 pop.style.zIndex=1 pop.style.left='-500px'} function popNo(){let pop=document.getElementById('optpop') pop.style.opacity=0 pop.style.zIndex=1 pop.style.left='-500px'} function popHTML(){let s='' s+='
' s+=radioHTML('','game',my.games,'gameChg',1) s+='
' s+='' s+='' s+='
' s+='
' return s} function radioHTML(prompt,id,lbls,func,n){let s='' s+='
' s+=prompt for(let i=0;i' s+='' s+='
'} s+='
' return s} function gameChg(n){my.game=my.games[n] gameNew()} function gameNew(){let attempt=0 let count do{my.codes=shuffle(my.game.codes.slice()).slice(0,my.game.codeLen) my.rows=[] my.rows.push(new Row('hidden',my.game.codeLen,my.codes)) rowAdd() count=countBovine(my.codes,rowCurr().codes)}while(count.bulls*2+count.cows>=my.game.codeLen&&attempt++<10) document.getElementById('keybd').innerHTML=keybdHTML() update() my.inGameQ=true} function update(){let s='' for(let i=0;i0){let index=Math.floor(Math.random()*counter) counter-- let temp=array[counter] array[counter]=array[index] array[index]=temp} return array} function chg(col){rowCurr().sel(col) if(my.keybdQ){}else{rowCurr().incr(1)} update()} function hasDups(a){let t=a.concat().sort() for(let i=1;i1)my.rows.pop() update()} function guess(){let row=rowCurr() if(hasDups(row.codes)){document.getElementById('info').innerHTML='Duplicates' return} let count=row.guess() if(count==my.game.codeLen){console.log('success!') success()}else{row.type='done' rowAdd()} update()} function success(){my.inGameQ=false my.rows[0].hideQ=false rowCurr().type='done' my.rows.push(new Row('success',my.game.codeLen,[])) update()} function keybdHTML(){let s='' let anss=my.game.codes let rowN=parseInt(anss.length/3+0.99) let n=0 s+='
' for(let j=0;j' s+=' '}else{s+='
' let ans=anss[n] if(ans=='?')ans=' ' s+=ans} s+='
' n++} s+='
' return s} function keybdClick(n){console.log('keybdClick',n,rowCurr()) rowCurr().setVal(n)} function rowAdd(){console.log('rowAdd') let codes=[] if(my.rows.length<2){codes=my.game.codes.slice()}else{codes=rowCurr().codes} my.rows.push(new Row('input',my.game.codeLen,codes))} function rowCurr(){return my.rows[my.rows.length-1]} class Row{constructor(type,n,codes){this.type=type this.n=n this.bulls=0 this.cows=0 this.col=0 this.hideQ=false switch(type){case 'hidden':this.codes=codes.slice(0,n) this.bulls='Bulls' this.cows='Cows' this.hideQ=true break case 'input':this.codes=codes.slice(0,n) break default:}} setVal(n){this.codes[this.col]=my.game.codes[n] this.col++ if(this.col>=this.n)this.col=0 update()} sel(n){this.col=n if(this.col>=this.n)this.col=0 if(this.col<0)this.col=this.n-1} moveSel(n){this.col+=n if(this.col>=this.n)this.col=0 if(this.col<0)this.col=this.n-1} incr(n){if(!my.inGameQ)return let found=-1 for(let i=0;imy.game.codes.length-1)found=0 if(found<0)found=my.game.codes.length-1 this.codes[this.col]=my.game.codes[found]} guess(){let count=countBovine(my.codes,this.codes) this.bulls=count.bulls this.cows=count.cows return count.bulls} html(){let s='' if(this.type=='success'){s+='
' s+='
' s+='Well Done !' s+='
' s+='
' s+='' s+='
' s+='
' return s} s+='
' s+='
' for(let i=0;i=0){let clr=n==this.col?my.cellHiClr:my.cellBDrClr s+='
'}else{s+='
'} s+=this.hideQ?'?':code s+='
' break case 'clr':if(n>=0){let clr=n==this.col?my.cellHiClr:my.cellBDrClr s+='
'}else{if(this.hideQ){s+='
'}else{s+='
'}} s+=' ' s+='
' break default:} return s}} function countBovine(master,guess){let count={bulls:0,cows:0} let g=guess.join('') for(let i=0;i{if(cls.length==0)cls='btn' return '
' return s},sel:()=>{let s='' s+='>\n' for(let i=0;i'+opt.descr+'\n'} s+='' if(lbl.length>0)s+='' return s},sld:()=>'>',}[tag]()||'' s+='\n' return s.trim()} init()