Here is my current code: http://codepen.io/lisacope/pen/woNLge
Right now the computer just picks a random empty space and it seems to mostly work. Except! whichever space is last picked before either the user or the computer wins doesn’t display. What am I doing wrong?
I think it might be that you’re using an alert() in the js where you check for a winner and this happens before you update the gameboard?
Does the screen not update until after I return from the click function? Is there any way I can force the board to update right after I update the dom element? Or is there a better way to do this?
I’m not sure if this addresses your question, but I when made my game, once a player moved, the move was added to the array that held the game board and then the div with the clicked id had its value changed to ‘X’ or ‘O’. After the update, a function was called to either have the computer move or allow the human to move, depending on who moved last.
I couldn’t get yours to work properly when I checked it, so you might be in the middle of some updates. Here is a link to mine:
It is a little verbose in the script because I hadn’t quite mastered how to break up my code into chunks of the appropriate size yet when I did this project. However, I had every click from the user be the trigger for the computer to go. There is a small delay before the CPU goes because it seemed a little too creepy to me to have the computer instantly move right after you do.