I'am creating a tik tak toe game with javascript. So right now i'am making it that you can play vs the computer. And i got it to work but right now it does fill in a field on the board, and i also have a array with the gameState to track which fields on the board are already filled in. But what it does right now is that in that array the move of the computer gets in a wrong position SOMETIMES. Not always but like 50/50 and sometimes it doesn't even showup in the array. Ill put my code for the computer down below. The fields variable is just a querySelectorAll for the fiels of the board.
function handleComputerMove() {
const emptyFields = [];
let randomField;
if (!gameActive || !gameState.includes("")) {
return;
}
fields.forEach(function(cell){
if (cell.textContent == '') {
emptyFields.push(cell);
}
});
randomField = Math.ceil(Math.random() * emptyFields.length -1);
emptyFields[randomField].innerHTML = currentPlayer;
handleCellPlayed(randomField);
handleResultValidation();
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/Coqgp5D
Comments
Post a Comment