var w,h,el,my={};function einsteinMain(mode){this.version='0.684' my.mode=typeof mode!=='undefined'?mode:'4Num';w=600;h=600;my.nums=["0","1","2","3","4","5","6","7","8","9"] my.ltrs=["A","B","C","D","E","F"] my.clrs=[["Navy",'#000080'],["Blue",'#0000FF'],["Light Blue",'#ADD8E6'],["Gold",'#ffd700'],["Yellow",'#ffff00'],["Red",'#FF0000'],["Pink",'#FFb6c1'],["Orange",'#FFA500'],["Black",'#000000']] my.games=[{mode:"3x3",title:"3 by 3",n:3,ruleSz:48,tileSz:48,grid:[[0,0],[1,0],[0.5,1]]},{mode:"4x4",title:"4 by 4",n:4,ruleSz:48,tileSz:48,grid:[[0,0],[1,0],[0,1],[1,1]]},{mode:"5x5",title:"5 by 5",n:5,ruleSz:36,tileSz:50,grid:[[0,0],[1,0],[0,1],[1,1],[0.5,0.5]]},{mode:"6x6",title:"6 by 6",n:6,ruleSz:32,tileSz:42,grid:[[0,0],[1,0],[2,0],[0,1],[1,1],[2,1]]},];my.game=my.games[0] my.pics=[{id:"pic1",title:"Sketches",url:'../games/images/einstein-tiles.jpg',max:8},{id:"pic2",title:"Photos",url:'../games/images/einstein-tiles2.jpg',max:6},{id:"pic3",title:"Mathematics",url:'../games/images/einstein-simple.svg',max:8},];my.pic=my.pics[2] my.dummy=[gameChg,popNo,popYes,popShow] my.tilePos={x:204,y:40} my.soundQ=true var s="";s+='' var bg='background: linear-gradient(to bottom, #def 0%,#fed 100%)' s+='
';s+='';s+='
';s+='';s+='';s+='
';s+=soundBtnHTML() s+='
' s+=popHTML();s+='
© 2018 MathsIsFun.com v'+this.version+'
';s+='';s+='';s+='';s+='
';document.write(s) my.ruleMax=120 my.img=new Image() my.img.src=my.pic.url if(my.img.complete){loaded()}else{my.img.addEventListener('load',loaded)} my.drags=[] my.dragNo=0 my.dragQ=false document.addEventListener('mouseup',function(){my.dragQ=false;});el=document.getElementById('cans') el.width=w el.height=h el.addEventListener("mousedown",mouseDown,false);el.addEventListener('touchstart',touchStart,false);el.addEventListener("mousemove",pointerChg,false);} function picChg(n){console.log('picChg',n) my.pic=my.pics[n]} function success(onq){var div=document.getElementById('success') if(onq){div.style.display='block' if(my.soundQ)document.getElementById('successsnd').play();}else{div.style.display='none'}} function loaded(){gameNew()} function touchStart(ev){var touch=ev.targetTouches[0];ev.clientX=touch.clientX;ev.clientY=touch.clientY;ev.touchQ=true;mouseDown(ev) console.log('touchStart')} function touchMove(ev){var touch=ev.targetTouches[0];ev.clientX=touch.clientX;ev.clientY=touch.clientY;ev.touchQ=true;mouseMove(ev);console.log('touchMove')} function touchEnd(){el.addEventListener('touchstart',touchStart,false);window.removeEventListener("touchend",touchEnd,false);if(my.dragQ){my.dragQ=false;window.removeEventListener("touchmove",touchMove,false);}} function pointerChg(ev){var bRect=el.getBoundingClientRect();var mouseX=ev.clientX-bRect.left var mouseY=ev.clientY-bRect.top var overQ=false;for(var i=0;imaxX)?maxX:posX);var posY=mouseY-my.dragHoldY;posY=(posYmaxY)?maxY:posY);my.drags[my.dragNo].move(posX,posY)} function hitTest(shape,mx,my){if(mx(shape.x+shape.wd))return false;if(my>(shape.y+shape.ht))return false;return true} function gameChg(){} function popShow(){var pop=document.getElementById('optpop');pop.style.transitionDuration="0.3s";pop.style.opacity=1;pop.style.zIndex=12;pop.style.left='10px';my.opt={gamemode:my.game.mode,picid:my.pic.id}} function popYes(){var pop=document.getElementById('optpop');pop.style.opacity=0;pop.style.zIndex=1;pop.style.left='-500px';for(var i=0;i';s+='';s+='
'} s+='';return s;} function gameRestart(){success(false) my.userPos.import(my.sttPos) var myNode=document.getElementById("cans");var children=myNode.childNodes console.log('children',children) for(var child in children){console.log('child',child,myNode[child])} rulesShow() drawTiles('gameRestart')} function gameNew(){success(false) my.n=my.game.n my.puzzle=[] my.rules=[] genPuzzle(my.puzzle,my.rules) console.log('gameNew',my.puzzle,my.rules) my.userPos=new Possibilities() my.userPos.reset() var myNode=document.getElementById("cans");while(myNode.firstChild){myNode.removeChild(myNode.firstChild);} rulesShow() drawTiles('gameNew') my.sttPos=new Possibilities() my.sttPos.import(my.userPos)} function genPuzzle(puzzle,rules){var goodq=false var tryCount=0 while(!goodq&&tryCount<3){rules.length=0 goodq=true tryCount++;for(var i=0;i=my.ruleMax){console.log('FAIL',tryCount) goodq=false continue} rulesRemove(puzzle,rules) console.log('after pruning',rules.length,rules)}} function shuffle(arr){var a,b,c;for(var i=0;i<30;i++){a=randomIntExc(0,my.n);b=randomIntExc(0,my.n);c=arr[a];arr[a]=arr[b];arr[b]=c;}} function randomIntExc(min,max){return Math.floor(Math.random()*(max-min))+min;} function rulesMake(puzzle,rules){setTileSize();var rulesDone=false;var count=0;do{var rule=genRule(puzzle);var ruleDupe=false;var s=rule.getAsText();for(var i=0;ih-my.game.ruleSz){currX+=my.game.ruleSz*3+5 currY=0}}}} function drawTiles(id){console.log("drawTiles="+id);my.tiles=[] for(var row=0;row=my.n-2) rightPossible=false;else rightPossible=(pos.isPossible(i+1,this.centerRow,this.centerThing)&&pos.isPossible(i+2,this.row1,this.thing1));if((!leftPossible)&&(!rightPossible)){pos.exclude(i,this.row2,this.thing2);goodLoop=true;}} if(pos.isPossible(i,this.row1,this.thing1)){if(i<2) leftPossible=false;else leftPossible=(pos.isPossible(i-1,this.centerRow,this.centerThing)&&pos.isPossible(i-2,this.row2,this.thing2));if(i>=my.n-2) rightPossible=false;else rightPossible=(pos.isPossible(i+1,this.centerRow,this.centerThing)&&pos.isPossible(i+2,this.row2,this.thing2));if((!leftPossible)&&(!rightPossible)){pos.exclude(i,this.row1,this.thing1);goodLoop=true;}}} if(goodLoop)changed=true}while(goodLoop) break case 'lft':case 'rgt':for(i=0;i=0;i--){if(pos.isPossible(i,this.row1,this.thing1)){pos.exclude(i,this.row1,this.thing1);changed=true;} if(pos.isPossible(i,this.row2,this.thing2))break;} break case 'col':for(i=0;i0){if(my.soundQ)document.getElementById('failsnd').play() console.log('failn')} console.log('solved?',completeQ,failn) if(completeQ){if(failn>0){}else{success(true)}}} function getLoc(locs,mousex,mousey){var loc=null if(locs.length==1){return locs[0]}else{for(var i=locs.length-1;i>=0;i--){var loc=locs[i] if(mousex>loc[0]&&mousexloc[1]&&mousey