const cells = document.querySelectorAll('.cell');
const statusText = document.getElementById('status-text');
const restartButton = document.getElementById('restart-button');
const potionButton = document.getElementById('use-potion');
const potionCountText = document.getElementById('potion-count');
let board = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let isGameActive = true;
let potions = 0;
let potionUsed = false;
const PLAYER_X_WON = 'PLAYER_X_WON';
const PLAYER_O_WON = 'PLAYER_O_WON';
const TIE = 'TIE';
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8], // Rows
[0, 3, 6],
[1, 4, 7],
[2, 5, 8], // Columns
[0, 4, 8],
[2, 4, 6] // Diagonals
];
function handleCellPlayed(clickedCell, index) {
board[index] = currentPlayer;
clickedCell.textContent = currentPlayer;
}
function handleResultValidation() {
let roundWon = false;
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
announce(currentPlayer === 'X' ? PLAYER_X_WON : PLAYER_O_WON);
isGameActive = false;
if (currentPlayer === 'X') {
potions++;
updatePotionCount();
}
return;
}
if (!board.includes('')) {
announce(TIE);
isGameActive = false;
}
}
function announce(type) {
switch (type) {
case PLAYER_X_WON:
statusText.textContent = 'You won! Potion earned!';
break;
case PLAYER_O_WON:
statusText.textContent = 'Computer won!';
break;
case TIE:
statusText.textContent = 'Tie game!';
break;
}
}
function isValidAction(cell) {
if (cell.textContent === 'X' || cell.textContent === 'O') {
return false;
}
return true;
}
function updatePotionCount() {
potionCountText.textContent = potions;
potionButton.disabled = potions === 0;
}
function handlePlayerChange() {
currentPlayer = 'X';
statusText.textContent = 'Your turn!';
}
function computerMove() {
let availableCells = [];
board.forEach((cell, index) => {
if (cell === '') {
availableCells.push(index);
}
});
if (availableCells.length === 0) return;
let randomIndex = availableCells[Math.floor(Math.random() * availableCells.length)];
if (potionUsed) {
// Skip the computer's turn when potion is used
potionUsed = false;
statusText.textContent = 'You used a potion! It\'s your turn again!';
} else {
board[randomIndex] = 'O';
cells[randomIndex].textContent = 'O';
statusText.textContent = 'Your turn!';
}
handleResultValidation();
}
function handleCellClick(event) {
const clickedCell = event.target;
const cellIndex = parseInt(clickedCell.getAttribute('data-cell-index'));
if (!isGameActive || !isValidAction(clickedCell)) {
return;
}
handleCellPlayed(clickedCell, cellIndex);
handleResultValidation();
if (isGameActive) {
setTimeout(() => {
computerMove();
}, 500);
}
}
function handleRestartGame() {
board = ['', '', '', '', '', '', '', '', ''];
isGameActive = true;
currentPlayer = 'X';
statusText.textContent = 'Your turn!';
cells.forEach(cell => cell.textContent = '');
}
function handleUsePotion() {
if (potions > 0 && !potionUsed) {
potions--;
updatePotionCount();
potionUsed = true;
statusText.textContent = 'Potion used! Computer skips a turn!';
}
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartButton.addEventListener('click', handleRestartGame);
potionButton.addEventListener('click', handleUsePotion);
updatePotionCount();