1

I am making a tic tac toe game. Each square on the board has an index from 0 to 8. To check the winner, I have a two-dimensional array winningPlays that contains all potential winning combinations. I have two arrays that contain plays by Xs or by Os - xPlays and oPlays, respectively. After sorting xPlays, I want to compare it to winningPlaysto see if any of the arrays match. If they do, I want to console.log('X wins'). I can't seem to find a way to execute the console.log at the right time to determine the winner. Here is the piece of problem code:

const winningPlays = [
        [0,1,2], //across top
        [3,4,5], //across middle
        [6,7,8], //across bottom
        [0,3,6], //left down
        [1,4,7], //middle down
        [2,5,8], //right down
        [0,4,8], //top left to bottom right
        [2,4,6] // top right to bottom left
    ]; //length == 8

    function checkForWinner() {
        for(let i = 0; i < winningPlays.length; i++){
            for(let j = 0; j < winningPlays[i].length; j++){
                if (xPlays.length < 3) {
                    return;
                } else if (winningPlays[i][j] !== xPlays[j]) {
                    console.log(winningPlays[i][j])
                    console.log(xPlays[j])
                    return;
                }
                console.log('win')  // executes every time that xPlays.length >= 3
            }
        } 
    };

And here is a link to my codepen draft: https://codepen.io/CDLWebDev/pen/gOawjvE

4
  • The xPlays.length check shouldn't be inside the loop. Commented Apr 20, 2020 at 22:00
  • 1
    That way of checking the winner doesn't seem too great to be honest. Commented Apr 20, 2020 at 22:04
  • I would parse your board to look for the lines. Commented Apr 20, 2020 at 22:07
  • You can write a function checking if a player won. Your best friend will be the for loop. Commented Apr 20, 2020 at 22:08

3 Answers 3

2

You have several problems.

First, you return from the function as soon as you find a mismatch, but a later element of winningPlays could match.

Second, you're expecting xPlays to exactly match one of the winningPlays elements. But X could have additional plays. For instance, if xPlays = [2, 3, 4, 5], that should match[3, 4, 5]. What you really want to test is if all the elements of one of thewinningPlayselements are included inxPlays`, they don't have to have the same index.

function checkForWinner() {
  if (xPlays.length < 3) {
    return;
  }
  for (let i = 0; i < winningPlays.length; i++) {
    let win = true;
    for (let j = 0; j < winningPlays[i].length; j++) {
      if (!xPlays.includes(winningPlays[i][j])) {
        win = false;
        break;
      }
    }
    if (win) {
      console.log('win');
      break;
    }
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

This is a wonderful and elegant solution! In actuality, I had realized that I would have the second problem eventually, but I was trying to break them apart and solve them one at a time. But this solves both issues in one fell swoop. Thanks very much, @Barmar
0
  • Go through each sub-array
  • Check if all the items of that subArray are present in players array
function checkWinner() {
    return winningPlays.some(list => {
        return list.every(item => {
            return xPlays.includes(item);
        });
    });
}

Comments

0

I would make it simpler by doing like following

const winningPlays = [
    "0,1,2", //across top
    "3,4,5", //across middle
    "6,7,8", //across bottom
    "0,3,6", //left down
    "1,4,7", //middle down
    "2,5,8", //right down
    "0,4,8", //top left to bottom right
    "2,4,6" // top right to bottom left
]; //length == 8

function checkForWinner(xPlays) {
    var convertXPlays = xPlays.toString(); //if there are spaces in your array, make sure to remove it
    if (winningPlays.indexOf(convertXPlays) > -1)
    {
       console.log('win!');
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.