0% found this document useful (0 votes)
11 views2 pages

نوجا

نين دو بي

Uploaded by

sentsweates
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

نوجا

نين دو بي

Uploaded by

sentsweates
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const board = document.

getElementById('board');
const cells = document.querySelectorAll('.cell');
const message = document.getElementById('message');
const resetButton = document.getElementById('reset');

let currentPlayer = 'X';


let gameActive = true;
let boardState = ['', '', '', '', '', '', '', '', ''];

const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];

function handleCellClick(event) {
const clickedCell = event.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));

if (boardState[clickedCellIndex] !== '' || !gameActive) {


return;
}

boardState[clickedCellIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;

checkWinner();
}

function checkWinner() {
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (boardState[a] && boardState[a] === boardState[b] && boardState[a] ===
boardState[c]) {
message.textContent = `‫ الالعب‬${currentPlayer} ‫;`!فاز‬
gameActive = false;
return;
}
}

if (!boardState.includes('')) {
message.textContent = '‫;'!تعادل‬
gameActive = false;
return;
}

currentPlayer = currentPlayer === 'X' ? 'O' : 'X';


}

function resetGame() {
gameActive = true;
currentPlayer = 'X';
boardState = ['', '', '', '', '', '', '', '', ''];
message.textContent = '';
cells.forEach(cell => {
cell

You might also like