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;
}
}
}