# Help with TicTacToe logic

Help with TicTacToe logic
0

#1

Hey guys. I would really appreciate some help with this problem. I am trying to calculate a “win” for the player in tic tac toe. Basically, I am comparing the players moves( which are stored in an array) to a list of win combinations.

``````///playerOneList is whatever moves the player has made up to this point

//any of these arrays will mean he has three xs in a row.
var winList = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]
];

function player1Won() {

var count = 0;
for (i = 0; i < playerOneList.length; i++) {
count = 0;
for (y = 0; y < winList.length; y++) {
for (x = 0; x < 3; x++) {
if (playerOneList[i] == winList[y][x]) {
console.log(playerOneList[i]);
console.log(winList[y][x]);
count++;
console.log(count);
if (count == 3) {
console.log("player 1 wins!!!");
}
}
}
}
}
}
``````

Thanks so much!

#2

after checking on each array on `winList`, you should’ve emptied `count` or else it’ll will keep adding from the previous value. And you might consider using this trick to comparing array content on javascript:
`playerOneList.toString() == winList[i].toString()`

It’ll work faster in your case.

#3

I am pretty sure that I am emptying the count variable after the first for loop. If I move the “count = 0;” line down to the next for loop then it won’t count any higher than 1 and then empties on the next iteration.

Your “.toString” solution looks neat but won’t it only work if I am comparing a whole list to another list? My problem is that the playerOneList won’t be just three characters like the winList. Chances are, it will be four or more characters. Hence my need to search the playerOneList for the items contained in winList.

I appreciate the help. Do you think I am misunderstanding you?

#4

Whew! I did it. As it turned out, I should have been iterating through the constant win list first and then checking its values against the ones in the actual moves list. Thanks for the help!

#5

nice to see you get this sorted … had rewrote your code late last night and was going to point out a few things to you to day … but now i see your sorted .

will point out one thing though

your not using … for (var i … for(var y … for (var x … this creates global variables for each of these and could cause you problems later or in other things you code if you keep doing this.

A way of reducing slightly your nested for loops … (for loops are great but when they get nested can be a pain)
you are checking each move from player against each move in each hand from winList … which will work but requires 3 for loops … this is what i used

``````        for(let z = moves.length;z>0;z--){
if(hands[y-1].includes(moves[z-1])){
count++; ```

The important line is the if .... and the important method is **includes** ..... using includes means you only have to check if player moves is included in winList rather than having to check each item in winList against each player move

anyway hope the rest goes well .... this was a challenging project ... and not easy to get unbeatable code ... i finished the code part ... fairly sure i got it unbeatable but have put it aside for the time being as i hate designing the board and such lol``````

#6

Oh yeah, I like your technique in the if statement. Thanks for the advice on my for loops. I keep accidentally forgetting to write var before the letter. Oops! Thanks a bunch for the advice.