0

I have kinda weird problem. I need to make a Tic Tac Toe game for my school project. The board is resizable (e.g. 5x5, 7x7, 12x12) and I want to just check if the first row is filled with circles or crosses, but my code always returns false. I really don't know what I'm doing wrong.

for (i = 0; i < size; i++) {
            console.log(pola[i]);
            if (pola[i] == winnerK || pola[i] == winnerR) {
                console.log("gz");
                alert("Win");
            }
        }

where:
size is just an int describing board size (like 5 or so)
pola is a 2 dimensional array containing cells in board
winnerK is an array, which for 5x5 board is [1, 1, 1, 1, 1]
winnerR is pretty much the same as winnerK, but filled with [2, 2, 2, 2, 2]


So every time user clicks a cell, a field in array "pola" is changed to 1 or 2 (1 for circles, 2 for crosses). And when I console.log it everything looks good, but it won't give me "true" no matter what I'm doing.

Please, help me ;_;

  • try with === it checks for values – Krishna Satya May 14 '18 at 08:13
  • @KrishnaSatya: So does `==`, there's no reason to believe it makes a difference in the above. – T.J. Crowder May 14 '18 at 08:15
  • 4
    You can't check arrays this way, ==/=== on an array will check if the object reference is the same. You should check each cell individually – Austin Greco May 14 '18 at 08:16
  • you may use a single loop and just count every type in an array which hase tree items, according to the the player number. then check if one of the players has the wanted amount. – Nina Scholz May 14 '18 at 08:22
  • @AustinGreco any idea how could I actually make it without having billions of if statements? Would be very helpful – Stanisław Zarycki May 15 '18 at 07:13
  • @StanisławZarycki It's hard to provide code examples here, but basically you want a loop inside a loop. You should be able to loop through each row, and then loop through every column in that row and see if they match. My advice would be to break up your problem into small functions (`checkHorizontalWinner`, `checkVerticalWinner`, etc..) and pass the row/column and player id. – Austin Greco May 16 '18 at 00:32
  • @AustinGreco okay, thank you so much :) – Stanisław Zarycki May 16 '18 at 06:44

0 Answers0