Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanSingh0208 authored Oct 13, 2024
1 parent afd4cf6 commit 03f749a
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tic-tac-toe/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const board = document.getElementById('board');
const cells = document.querySelectorAll('.cell');
const statusDisplay = document.getElementById('status');
const resetButton = document.getElementById('reset');

let currentPlayer = 'X';
let gameActive = true;
let gameState = ['', '', '', '', '', '', '', '', ''];

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 (gameState[clickedCellIndex] !== '' || !gameActive) {
return;
}

gameState[clickedCellIndex] = currentPlayer;
clickedCell.classList.add(currentPlayer.toLowerCase());
clickedCell.innerHTML = currentPlayer;
checkResult();
}

function checkResult() {
let roundWon = false;
for (const condition of winningConditions) {
const [a, b, c] = condition;
if (gameState[a] === '' || gameState[b] === '' || gameState[c] === '') {
continue;
}
if (gameState[a] === gameState[b] && gameState[a] === gameState[c]) {
roundWon = true;
break;
}
}

if (roundWon) {
statusDisplay.innerHTML = `Player ${currentPlayer} has won!`;
gameActive = false;
return;
}

if (!gameState.includes('')) {
statusDisplay.innerHTML = 'It\'s a draw!';
gameActive = false;
return;
}

currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
statusDisplay.innerHTML = `It's ${currentPlayer}'s turn.`;
}

function handleCellHover(event) {
const clickedCell = event.target;
if (clickedCell.classList.contains('x') || clickedCell.classList.contains('o')) {
return;
}
clickedCell.style.cursor = 'pointer';
}

function resetGame() {
gameActive = true;
currentPlayer = 'X';
gameState = ['', '', '', '', '', '', '', '', ''];
statusDisplay.innerHTML = `It's ${currentPlayer}'s turn.`;
cells.forEach(cell => {
cell.classList.remove('x', 'o');
cell.innerHTML = '';
});
}

cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
cell.addEventListener('mouseover', handleCellHover);
});

resetButton.addEventListener('click', resetGame);
statusDisplay.innerHTML = `It's ${currentPlayer}'s turn.`;
56 changes: 56 additions & 0 deletions tic-tac-toe/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
margin: 0;
}

.container {
text-align: center;
}

.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: 20px auto;
}

.cell {
width: 100px;
height: 100px;
background-color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 36px;
cursor: pointer;
border: 2px solid #ccc;
}

.cell:hover {
background-color: #f0f0f0;
}

.cell.x {
color: #ff5722;
}

.cell.o {
color: #2196f3;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

#status {
font-size: 20px;
margin-top: 20px;
}

0 comments on commit 03f749a

Please sign in to comment.