const callback = (event) => {
(중략)
turn = turn === 'X' ? 'O' : 'X';
if (turn === 'X') {
const emptyCells = rows.flat().filter((v) => !v.textContent);
const randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)];
randomCell.textContent = 'X';
const hasWinner = checkWinner(randomCell);
if (hasWinner) { // 승자가 있으면
$result.textContent = `${turn}님이승리!`;
return;
}
// 승자가 없으면
const draw = rows.flat().every((cell) => cell.textContent);
if (draw) {
$result.textContent = '무승부';
return;
}
turn = turn === 'X' ? 'O' : 'X';
}
};