letrowAndColumnCount=10;lettemplateArr=newArray(rowAndColumnCount*rowAndColumnCount);letgameArr=newArray(rowAndColumnCount*rowAndColumnCount);constcolors=['red','green','lightblue','pink'];letselectedColor=colors[2];letclickCounter=0;letgameBoard=document.getElementById('gamePad');lettemplateBoard=document.getElementById('templateBoard');constbuttons=document.getElementById('buttons');constcheckButton=document.getElementById('checkButton');constchessSizeSelect=document.getElementById('chessSize');constchessSizeSection=document.getElementById('chessSizeSection');constgameSection=document.getElementById('gameSection');fillChessSizes();functionfillChessSizes(){for(leti=2;i<13;i++){constoption=document.createElement('option');option.value=i.toString();option.innerText=i.toString();if(i===rowAndColumnCount){option.selected=true}chessSizeSelect.appendChild(option);}}functionstartGame(){// find the number of rowsrowAndColumnCount=chessSizeSelect.value;templateArr=newArray(rowAndColumnCount*rowAndColumnCount);gameArr=newArray(rowAndColumnCount*rowAndColumnCount);createChessBoard(gameBoard.id,false);createChessBoard(templateBoard.id,true);addButtons(buttons.id);addCheckButton(checkButton.id);// hide thechessSizeSection.style.display='none';gameSection.style.display='inherit';buttons.style.display='inherit';checkButton.style.display='inherit';}functioncreateChessBoard(parentId,prefill){constparentElement=document.getElementById(parentId);// define the max with of the elementparentElement.setAttribute('style','width: '+(((rowAndColumnCount)*32)+10)+'px;')while(parentElement.firstChild)parentElement.removeChild(parentElement.firstChild);for(leti=0;i<(rowAndColumnCount*rowAndColumnCount);i++){constdiv=document.createElement('div');div.onclick=onGameClick;div.className='field';// div.innerText = i.toString();if(prefill){constcolorValue=colors[getRandomValue(0,colors.length-1)];div.setAttribute('style','background-color: '+colorValue+';');// save to the template arrtemplateArr[i]=colorValue;}else{div.id=i.toString();}parentElement.appendChild(div);}}functionaddButtons(parentId){constparentElement=document.getElementById(parentId);while(parentElement.firstChild)parentElement.removeChild(parentElement.firstChild);for(leti=0;i<colors.length;i++){constcolorName=colors[i];constbtn=document.createElement('button');btn.innerText=colorName;btn.onclick=onButtonColorSelectClick;btn.setAttribute('style','background-color: '+colorName+';');// add itparentElement.appendChild(btn);}}functionaddCheckButton(parentId){constparentElement=document.getElementById(parentId);while(parentElement.firstChild)parentElement.removeChild(parentElement.firstChild);constbtn=document.createElement('button');btn.innerText='check';btn.onclick=onButtonCheckClick;// add itparentElement.appendChild(btn);}functiongetRandomValue(min,max){min=Math.ceil(min);max=Math.floor(max);returnMath.floor(Math.random()*(max-min+1))+min;}functiononGameClick(){clickCounter++;document.getElementById(this.id).setAttribute('style','background-color: '+selectedColor+';')gameArr[this.id]=selectedColor;}functiononButtonColorSelectClick(){console.log(this.innerText);selectedColor=this.innerText;}functiononButtonCheckClick(){for(leti=0;i<templateArr.length;i++){if(templateArr[i]!==gameArr[i]){alert('Da ist etwas nicht ganz korrekt!');return;}}alert('Gratuliere! in '+clickCounter+' clicks, gelöst!');if(confirm(' Nochmals spielen?')){templateArr=newArray(rowAndColumnCount*rowAndColumnCount);gameArr=newArray(rowAndColumnCount*rowAndColumnCount);clickCounter=0;createChessBoard(gameBoard.id,false);createChessBoard(templateBoard.id,true);chessSizeSection.style.display='none';gameSection.style.display='inherit';buttons.style.display='inherit';checkButton.style.display='inherit';}else{buttons.style.display='none';checkButton.style.display='none';}}