Tic tac toe -- turning off used squares


I’m struggling with how to keep track of which tic tac toe squares are occupied, particularly when it comes to the computer player. I need a way to keep the computer from putting an X or O on a space that’s already occupied.

I’ve tried removing occupied squares from an array as a way to keep track of them. I’ve also tried turning the square “off” with


and it works for the user, but the computer can still change it. Now I’m stuck.

Here’s the link to my codepen, if you’d like to look at all of the js. But be warned–I’ve made a bit of a mess.

Thank you in advance.

Instead of trying to track them separately, why not track the board’s squares in an array? You could put ‘p’ for player or ‘c’ for computer to know which one is where.

Thanks for the suggestion. Unfortunately I couldn’t picture it in my head (one of my shortcomings). So I ended up rewriting that section of the code, completely eliminating all arrays, instead relying on a bunch of if/else if statements.

So now I’m moving on to figuring out how to verify the winner.