# Tic Tac Toe - Computer not winning when it should

**URL:** <https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647>\
**Category:** General\
**Created:** [March 2, 2017, 4:55am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647 "2017-03-02T04:55:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug20000](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/doug20000/32/41299_2.png) [@doug20000](https://forum.freecodecamp.org/u/doug20000)\
**Post date:** [March 2, 2017, 4:55am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647/1 "2017-03-02T04:55:16Z")

</div>

> **[Tic Tac Toe](https://codepen.io/doug20000/full/MpYEOM)**
>
> ...

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 🙂

---

<div class="post-metadata">

**Author:** ![Luiko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/luiko/32/72800_2.png) [@Luiko](https://forum.freecodecamp.org/u/Luiko)\
**Post date:** [March 2, 2017, 5:49am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647/2 "2017-03-02T05:49:43Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![doug20000](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/doug20000/32/41299_2.png) [@doug20000](https://forum.freecodecamp.org/u/doug20000)\
**Post date:** [March 3, 2017, 4:15am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647/3 "2017-03-03T04:15:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Twinbird24](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/twinbird24/32/163114_2.png) [@Twinbird24](https://forum.freecodecamp.org/u/Twinbird24)\
**Post date:** [March 3, 2017, 7:46am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647/4 "2017-03-03T07:46:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![doug20000](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/doug20000/32/41299_2.png) [@doug20000](https://forum.freecodecamp.org/u/doug20000)\
**Post date:** [March 4, 2017, 7:17am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647/5 "2017-03-04T07:17:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 9:39am UTC](https://forum.freecodecamp.org/t/tic-tac-toe-computer-not-winning-when-it-should/92647/6 "2021-01-17T09:39:56Z")

</div>

Closed on January 17, 2021, 9:39am UTC


