-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from RohanSingh0208/main
Added Tic Tac Toe Game
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Tic Tac Toe</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Tic Tac Toe</h1> | ||
<div class="board" id="board"> | ||
<div class="cell" data-index="0"></div> | ||
<div class="cell" data-index="1"></div> | ||
<div class="cell" data-index="2"></div> | ||
<div class="cell" data-index="3"></div> | ||
<div class="cell" data-index="4"></div> | ||
<div class="cell" data-index="5"></div> | ||
<div class="cell" data-index="6"></div> | ||
<div class="cell" data-index="7"></div> | ||
<div class="cell" data-index="8"></div> | ||
</div> | ||
<button id="reset">Reset</button> | ||
<p id="status"></p> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |