I am working on a basic Javascript Tic Tac Toe game that lets a user play against the computer. One of the requirements for this game is that the user can never beat the computer the most they can do is tie. I'm having trouble figuring out the logic for this and I have not seen any examples I can understand on how to implement this. Right now the computer just chooses a random spot to place it's decision on it's turn. If this random spot is the top left corner(randomChoice==0) or the bottom right corner (randomChoice ==9) it changes it to the box next to it. I have posted the code below any tips on this would be helpful. Also this is a link to all the code I have so far on CodePen. http://codepen.io/Android162010/pen/LGZXQa
function playRandom() {
randomChoice = Math.round(Math.random() * 10);
if (randomChoice == 0) {
randomChoice = 1;
}
if (randomChoice == 10) {
randomChoice = 9;
}
if ($('#' + randomChoice).hasClass('hoverable')) {
makeTic('#' + randomChoice, false);
}
else {
playRandom();
}
}