Logic for Tic-Tac-Toe WIN

Logic for Tic-Tac-Toe WIN
0

#1

Hello! I am working on the Tic-ac-Toe game, and I am trying to write script, using a multi-dimensional array, to tell when I have won the game. Hope this makes sense. Thanks in advance!

My project is below. Here is some background:

Line 18: multidimensional array of possible winning combinations.
Line 43: beginning of the user’s turn.
Line 48 (the problem): calls isGameOver, the function that doesn’t work. (isGameOver begins on line 77).

Here is my project:

Here is the JavaScript code, if that’s easier:

const winCombos = [
     [1, 2, 3],
     [4, 5, 6],
     [7, 8, 9],
     [1, 5, 9],
     [3, 5, 7],
     [1, 4, 7],
     [2, 5, 8],
     [3, 6, 9],
   ]

  **_//One Player game_** 
  function gamePlay(id, availableSpots, me, you){
      if (availableSpots[id]){       **_//if the spot at index of ID is available, AKA TRUE_**
        $("#"+id).text(me);          **_//add my letter when I click_**
        plays.push(id);                **_//and add that id to the plays array_**
        availableSpots[id] = false; **_//and make that same spot FALSE_**
        isGameOver; **_//run the function to see if I won_**
        if (endOfGame === true){  
           alert ("hi"); **_//test. not working_**
       } else { **_//if the game is not over, it's the computer's turn_**
           computerTurn(availableSpots, you) 
    }}
  };

   **_//function to check if I won. NOT Working!_**

  function isGameOver(){
    for (var i = 0; i<winCombos.length; i++){ **_//iterate through the possible winning combinations_**
      if(plays.includes(i.every())){ **_//if every element in one of those combinations is in the play array, then I win_**
          endOfGame === true;
          $("body").css("background-color", red);
      } else{
          endOfGame === false;
      }
   }
}

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

I will point out your first issue out of the many code issues you currently have. The above line does not execute anything. You are missing a very important syntax which is used to call a function. Hint: The extra part goes after the function name.

In the future to help you debug accidental syntax issues like the one above, I suggest putting a few console.log statements into your code to validate certain functions are being called. For example, I put the following as the first line of your isGameOver function and quickly realized it was never been called.

console.log('Running isGameOver function');

The same concept will help you to see what is going on inside a loop such as your for loop inside isGameOver.

BTW - Once you resolve how to call the isGameOver function, you will need to check out your browser’s console (Ctrl+Shft+J in Chrome) and see an error you will get for the i.every reference.