Brain freeze - loop for tic tac toe game that takes click input

Hi there,

I am working on the tic-tac-toe game. I have the 2-human-player game up and running. That runs without creating a loop, more or less. Clicks drive the 2-player game progress forward.

Now I’m wanting to build in the computer-as-player section. I think I need to loop in order to enable the computer to take its turns. But I can’t figure out how to loop and also allow an open-ended period for human-player click input. In codepen, I can’t create a loop with open-ended breaks for input - codepen thinks it has detected an infinite loop.

I’m completely drawing a blank on how to approach the game flow portion of the project. I would be grateful for some guidance. Thank you!!

My pen is

Not sure why you think you need a loop to do the back and forth between you and the computer. The only looping would be if you have a set of winning moves you want to check in an array or object which would require iterating over for the check.

Basically, the player (you) clicks on a square and after that, the computer makes a move. This back and forth continues until one player has a winning board or all squares have been filled. If all filled with no winning board, then it is a tie. You just need click event handlers on the squares and have computer select a square based on some kind of logic considering what you have already clicked and what is left to click.