I am relatively new to JavaScript and I am writing some test code to check win lines in a TicTacToe (Noughts and Crosses) game.
After several iterations this is what I have come up with, but I still feel that it is unwieldy and relies too much on global variables.
Is there a better way to write this code?
const wins = [[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 moves = [0,5,2,4,3]; // example list of moves
var winLine = [];
const won = [true, true, true];
wins.forEach(function (win) {
win.forEach((square) => winLine.push(moves.some((move) => square == move)));
if(JSON.stringify(winLine) == JSON.stringify(won)){
logWin();
} else {
winLine =[];
}
});
function logWin() {
console.log("win");
}