Need help on Tic Tac Toe please!

edit: I got it to work. I am not really happy with it because I feel like I had to change it because I couldn’t understand what was wrong. I think I will come back to it later after I have learned more stuff and hopefully it will make more sense.

I am very close to being finished but keep getting stuck!
When it is the ai’s turn, it goes several times in a row instead of just one time.
I have tried to make the ai go only when I do a click, but it goes several times after one click.

Any help is appreciated!

update: made clicking already occupied squares impossible.

I cannot get x to go first everytime :frowning:

http://s.codepen.io/Jesse989/debug/begzYg

I also notice that you can select a square that has already been selected. I’m just learning so I don’t know much about best practice or anything but I notice in your turn function you have a few other functions being created. I would think that you should create all functions in the main Program and then call them if needed inside of other functions.

Yeah I noticed that as well, that is something I can fix so I am not as worried about it. The ai going crazy is what I really am getting frustrated with…I keep trying everything but it wont fix it.

hint 1: Objects are passed by reference (check the state of gameBoard during execution).
hint 2: on() method stacks when called multiple times (i.e. when you run element.on('click', handler) three times, then handler code will be executed 3 times after each click event);

Wow okay that helps so much!! Thank you!

So when I try to run this, every object that has a link with gameBoard changes when any other link to it changes??

Okay I still am not getting it.
I changed my events to .one(“click”, function but that did not work, and I tried to see what was making the extra numbers pop out of the miniMax function but I could not find it. To me it looks like the function is being called several times, but there is nothing that I can see that would do that.