It is necessary that the computer after the person’s move (the call to the click1 function) generates a random number from 1 to 9, sets it to “s” and checks if it is empty, put “Y”, if busy, re-generate the number ... some problem in the code I can not understand:
var s1 = document.getElementById("s1") var s2 = document.getElementById("s2") var s3 = document.getElementById("s3") var s4 = document.getElementById("s4") var s5 = document.getElementById("s5") var s6 = document.getElementById("s6") var s7 = document.getElementById("s7") var s8 = document.getElementById("s8") var s9 = document.getElementById("s9") var comChoice=0; var clickScore=0; function click1(element){ clickScore++ comChoice = Math.floor((Math.random() * 9)+1); console.log(comChoice) if(element.innerText==""){ if(clickScore%2==1 ){ element.style.cursor= "default" element.style.color="white" element.style.fontSize="40px" element.style.textAlign="center" element.innerHTML="X" } else{ element.style.cursor= "default" element.style.color="white" element.style.fontSize="40px" element.style.textAlign="center" element.innerHTML="O" } } else { alert("Ячейка уже занята") } computerTurn() if (proverka()==true ){ alert("end") } } function computerTurn(){ comChoice = Math.floor((Math.random() * 9)+1); if (s[comChoice].innerHTML=="X"||s[comChoice].innerHTML=="Y"){ computerTurn() } else { s[comChoice].innerHTML=="Y" } }
clickScore++
was moved to the beginning of the function - Grundy;
put it everywhere - Grundy