I am building a tic tac toe game similar to the one used in the official tutorial for React. I am using these methods to handle clicks and to check if someone won. Right now my history is always one step behind and I can't figure out why. It won't say I have won until I do one more move.
function didSomeoneWin(){
const squares = history[history.length - 1].squares;
console.log("history: ", history, " squares: ", squares);
const lines = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[6,4,2],
];
lines.forEach((line) => {
const [a,b,c] = line;
if (squares[a] && squares[a] === squares[b] && squares[b] === squares[c]){
console.log("SOMEONE WON!");
setWinner( squares[a]); // return the winner: X or O
}
})
}
function squareClick(i){
if (!winner){
const current = history[history.length - 1];
const squares = current.squares.slice();
if (squares[i]){
return;
}
squares[i] = turnCount;
console.log(i);
console.log("history before: ", history[history.length - 1]);
setHistory(history.concat([{ squares: squares }]));
console.log("history after: ", history[history.length - 1]);
setTurnCount(history.length % 2 === 0 ? "X" : "O");
didSomeoneWin();
}
}