Tic Tac Toe - Computer not winning when it should

So I have two difficulty levels on my game. In “noob” the computer just chooses a square at random, and you have to be asleep at the wheel to lose. If you do intentionally lose however the correct message comes up showing you lost.

In “medium” I’ve programed far more logic into the computer’s choice. However when it wins in this mode it doesn’t register, it just switches back to the player turn.

I have no idea why this happens as I use the same code to check for a win with both difficulties. Could someone take a look at my code and see if anything pops out? Computer logic starts on line 78, and the check win function on line 166.

Thanks in advance :slight_smile:

hey you are assigning arrays to the squareChosen, that’s why do not match for win.
please don’t use alert are a bit annoying, prefer console.log or html text,

I appreciate your reply, but I’m still failing to see where the problem is. I’ve added console logs and it appears the check function works the exact same for both difficulty levels. However a win is not being registered in medium mode.

If you add [0] after comp2x on line 93 and add it after player2x on line 105 then it works fine. Luiko was trying to say that in these cases you are assigning an array to those comp2x and player2x values when it should just be a single number, and then this leads to a messed up computerTiles array in the checkWin() function for the medium difficulty.

Add console.log(computerTiles); right inside the checkWin() function and you’ll notice the difference between the logs printed in noob mode vs medium mode.

1 Like

Ahh yes, that totally makes sense now. Thank you both for clarifying that for me :slight_smile:

2 Likes