# Need help with Tic Tac Toe project - pushing arrays

Need help with Tic Tac Toe project - pushing arrays
0

#1

Hi all,

I’m getting stuck on my Tic Tac Toe project. Specifically, I couldn’t push the elements on an existing array(array A) to a new array (array B), on the condition that the elements in array A exist in the third array (array C).

In the example below, duX is array A, allBtn is array C, and randomArr is array B. In my testing, even when in cases where an element from arrayA exists in arrayC, it doesn’t get pushed to array B for example. In other words, array B remains to be an empty array.

for (i=0; i<duX.length; i++){
if(allBtn.indexOf(duX[i] == -1 )) {
randomArr.push();
}
else {
randomArr.push(duX[i]);
};
};

Could someone help me debug this? That’d be much appreciated. My original codepen is here.

#2

Hi,
What does your pseudo logic look like?

I’m trying to understand how you are choosing the random array and index number. Are you counting each box as it’s numeral? Instead of:
top row [[0,1,2],
center row [3,4,5],
bottom row [6,7,8]];

• or 1-9.
Are you saying box 1 = 2, box 2 = 9?

Is the third array a “space available” array, or a “space taken” array?

What logic are you using to determine if the next move by the player should be blocked, or it the computer should take advantage of a possible immediate win?

I have also found that when my variable name matches what I’m trying to do then it makes it easier to find the issue.

This is what Chrome inspector says:
pen.js:120 [13]
pen.js:121 [ ]
pen.js:233 [8]
pen.js:234 [ ]
pen.js:351 (3) [8, 5, 2]
pen.js:352 [ ]

I totally use Console log to make sure I’m on track, follow the logic and find the line that is causing the problem.
It is helpful when you console.log to say what is happening. For instance: 120 : console.log("allBtn is spliced at (allBtn.indexOf(3), 1) " + allBtn);

I think 351 should console.log("Winning spaces are " + duX + randArray);

I know it is a lot of questions, but if you can answer them, then you can fix it, or at least find how it can be fixed.
I hope this helps.

#4

In this conditional, you are searching whether `allBtn` includes true or false values (`duX[i] == -1` is Boolean). Is that what you want? It seem not to be the case. Maybe `allBtn.indexOf(duX[i])===-1`?

#5

seems like you need to actually push an item onto randomArr.

in your code you aren’t pushing anything to your randomArr arry if duX[i] doesn’t exist in allBtn. Did you want to push something onto the array here? if you dont you should write something like:

``````if (allBtn.indexOf(duX[i] > -1 )) {
randomArr.push(duX[i]);
}
``````

In the code above -1 indicates that the item doesnt exist in the array, however if the index if a positive number it does exist and thus you should add it to randomArr.

hope this helps

#7

Yes, that’s exactly what I did wrong … ! I changed the code and it worked, thanks so much! When I get stuck, it always tends to be these small things that I tend to overlook … thanks again!

#8

Yes, correct, only push if duX[i] exist in allBtn. I like your suggestion of the code, much simpler! Plus Dan_Cio’s point that it should have been “allBtn.indexOf(duX[i]) == -1” (I misplaced the parenthesis), that should solve my problem! Thank you!

#9

Hi KoniKodes, thank you for your help! I like your suggestion of naming the variables according to their exact functions. I did find that when I didn’t do it, I got more confused about my own code …

Basically I was trying to assign value to each button so that, if they are in a straight line, they add up to 15. allBtn is all the buttons still available for players to choose from, duX are all the buttons a player need to block - as the opponent already put two buttons in a straight line, and the third button to help them win is still available, and randomArr is the array of the buttons a player needs to randomly place – except when duX is pushed to randomArr, where they need to randomly choose from the duX.

I know the logic is a bit long … but at least now I figured out where my code went wrong - I misplaced the parenthesis in the indexOf statement …

Thanks again for your help! I"ll also keep using Console Log the way you do. Cheers!

#10

Since arrays are indexed with 0 as the first of the sequence, I find that keeping the board numbers from 0-8 makes it easier to loop through when checking possible moves, wins and comparisons.

Something I found a lot in my research was to create a winning Board Array showing the possible wins. Something like:
boardWins=[
// Horizontal Wins
[0,1,2],[3,4,5],[6,7,8],
// Vertical Wins
[0,3,6],[1,4,7],[2,5,8],
// Diagonal Wins
[0,4,8],[2,4,6]
] ;

I never did get Min-Max algorithm to work, so I relied on my own logic. However, the Board Wins matrix helped put my logic into an order that I could work with.

I must admit that Tic-Tac-Toe took longer time and more research than the other projects (although Simon Says is pushing to beat that record).

Happy Coding - You are so close!

#11

Thank you Koni! I thought of using the winning array logic too, but ended up coming up with my own way – I arranged the numbers in a way that, when three buttons are on a sraight line, they add up to 15.

Don’t know why that was what I thought of … having the values of the buttons arranged in this particular way definitely created more headache when I was doing the testing (hard to keep track of what buttons I clicked on … ).

But thanks to the help on the FCC forum, I think tonight I’ve finally made the code all work … so that the machine is smart enough to keep me from winning! Now I’ll have to create the “choose X/O” function, and to keep scores etc.

But as you said, yes, I can’t believe I’m this close! Thanks again for your kind help.