i want the reset
function to change the turn
value to "X"
but it doesnt and after every game it changes the starting turn so game 1 x would start, game 2 o starts, game 3 x…,anyway i put a turn = "X"
at the first line of the reset
function but it doesnt do anything
let board = new Array(9).fill();
const squares = document.getElementsByClassName("square");
let turn = "X";
let X = 0;
let O = 0;
for (let i = 0; i < squares.length; i++) {
let element = squares[i];
element.addEventListener("click", function (e) {
if (!element.textContent) {
element.textContent = turn;
console.log(e.target.id);
board[e.target.id] = e.target.textContent;
console.log(board);
findWinningCombination();
checkDraw();
turn = turn === "X" ? "O" : "X";
}
});
}
function findWinningCombination() {
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (const combination of winningCombinations) {
const [a, b, c] = combination;
if (
board[a] === board[b] &&
board[a] === board[c] &&
board[(a, b, c)] != undefined
) {
switch (turn) {
case "X":
X++;
xText.textContent = `X: ${X}`;
break;
case "O":
O++;
oText.textContent = `O: ${O}`;
break;
}
reset()
}
}
}
function checkDraw() {
if (!(findWinningCombination() || board.includes(undefined))) {
//if there is a win or any empty squares it returns false if the board is full and there is no win it returns true
reset();
}
}
function reset() {
turn = "X";
board = new Array(9).fill();
for (let i = 0; i < squares.length; i++) {
squares[i].textContent = "";
}
}
#board {
display: grid;
grid-template-columns: auto auto auto;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 30vw;
height: 30vw;
gap: 1vmin;
}
.square {
font-family: "Nunito", sans-serif;
background-color: #252525;
color: white;
display: flex;
justify-content: center;
align-items: center;
min-width: 10vw;
min-height: 10vw;
font-size: 10vw;
user-select: none;
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>TicTacToe</title>
</head>
<body>
<p id="xText">X: 0</p>
<p id="oText">O: 0</p>
<div id="board">
<div id="0" class="square"></div>
<div id="1" class="square"></div>
<div id="2" class="square"></div>
<div id="3" class="square"></div>
<div id="4" class="square"></div>
<div id="5" class="square"></div>
<div id="6" class="square"></div>
<div id="7" class="square"></div>
<div id="8" class="square"></div>
</div>
<button class="button-6" onclick="reset()">Reset</button>
<script src="script.js"></script>
</body>
</html>
2
Answers
well in your code you call the
reset
function in thefindWinningCombination
function or thecheckDraw
function and after that you use the question mark condition that changes theturn
to X if its O or O if its X so thereset
would be called before the turn change i just moved theturn = turn === "X" ? "O" : "X";
up 2 lines above the function calls and it worksYou can change turn X According to you when reset game or cleanbord..