need to detect a diagonal win, have acheived this horizontally with the hScore part of the detwct win function but it can not detect a horizontal winner, ive tried to make the function so it uses the indexs of the arrays to trigger the win when each one has a o or x placed on it, but it doesnt work, is there a better way for me to achieve this ?
The game is o's and x's, with out the function working correctly it wont detect the winner for diagonal wins
players = ['o', 'x'];
currentPlayerIndex = 0;
board = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' '],
];
play = (x, y) => {
let message = 'current player wins';
if (getSquare(x, y) === ' ') {
setSquare(x, y, currentPlayer());
document.getElementById('square' + x + y).innerHTML = currentPlayer();
if (detectWinner()) {
alert(currentPlayer() + message);
console.log('winner');
}
changePlayer();
}
};
detectWinner = () => {
for (let i = 0; i < board.length; i++) {
let hScore = board[i];
let dScore = [...board[i]];
let hScoreString = JSON.stringify(hScore);
console.log(board[i]);
let matches = [
JSON.stringify(['x', 'x', 'x']),
JSON.stringify(['o', 'o', 'o']),
];
if (matches.includes(hScoreString)) {
return true;
} else if ( dScore[(0, 0), (0, 1), (0, 1)].every() === 'o' ) {
return true;
}
}
return false;
};
currentPlayer = () => {
console.log(players[currentPlayerIndex]);
return players[currentPlayerIndex];
};
changePlayer = () => {
currentPlayerIndex = (currentPlayerIndex + 1) % 2;
};
getSquare = (x, y) => {
return board[x][y];
};
setSquare = (x, y, value) => {
board[x][y] = value;
};
reset = () => {
////refresh page
};