G-8
G-8

Reputation: 107

Check if an array contains elements from another array

I'm trying to make a tictactoe game using javascript. I have the winning conditions in a nested array

const winConditions = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8],
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8],
            [0, 4, 8],
            [2, 4, 6]
]

and I have my current player's marked boxes indices in an array

let squaresIndices = [0,4,8]

I'm trying to stop the game when any of the winning conditions occur. I have tried each of the following and I can't figure out why they don't work.

winConditions.forEach((array) => {
    if (array === squaresIndices){
        alert("Game Over")
    }
//////

if (winConditions.includes(squaresIndices)){
    alert ("Game Over")
}
//////
        winConditions.some((arr) =>{
            if (arr.every((square) => {
                squaresIndices.includes(square)
            })) {
                alert("Game Over")
            }
        })

Upvotes: 0

Views: 195

Answers (3)

Khant
Khant

Reputation: 1122

First of all Array cannot be compared

Try this example

let a = [0,2,3]
let b = [0,2,3]

alert( a === b )

What you need to understand is that when you save an array. What you actually doing is created a reference to that array in memory.

Then again try this example

let a = [0,2,3]
let b = a

alert( a === b )

You will get true, Why? Because in first case you are trying to two separate addresses in memory. Meaning that a & b got different addresses but same kind of apartment. But in second case you are comparing the same address.

So the easiest way you can do is convert them into string then try to compare them.

JSON.stringify(a)==JSON.stringify(b)

Then you will get your desire result. While this could work if the order of the properties will always the same for those array instances, this leaves the door open for extremely nasty bugs that could be hard to track down.

Upvotes: 1

Tushar Kale
Tushar Kale

Reputation: 169

winConditions.forEach((array) => {
    if(JSON.stringify(array)==JSON.stringify(squaresIndices)){
        alert("Game Over")
    }
})

You cannot compare two arrays directly in javascript. You need to convert it to string for comparison. You can also use toString() instead of JSON.stringify()

Upvotes: 0

Kulvar
Kulvar

Reputation: 1179

Your last example is correct if not for forgetting to return the value and moving the if outside.

const winConditions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
];

const squaresIndices = [0, 4, 8];

const isWin = winConditions.some(
    (arr) =>
    {
        // return if this combination is matching or not
        return arr.every(
            (square) =>
            {
                // return if the value matches or not
                return squaresIndices.includes(square);
            }
        );
    }
);

// Then test the result
if (isWin)
{
    alert("Game Over");
}

Upvotes: 0

Related Questions