<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dinkar Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Dinkar</h1>
<div class="grid" id="gameBoard">
<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="resetButton">Reset Game</button>
<div id="result" class="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
margin-bottom: 20px;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
}
.cell {
width: 100px;
height: 100px;
background-color: #e9ecef;
display: flex;
justify-content: center;
align-items: center;
font-size: 2.5em;
cursor: pointer;
border-radius: 8px;
}
.cell:hover {
background-color: #d1d1d1;
}
button {
margin-top: 20px;
padding: 10px;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1em;
width: 100%;
}
button:hover {
background: #0056b3;
}
.result {
margin-top: 20px;
font-size: 1.2em;
color: #333;
}
const cells = document.querySelectorAll('.cell');
const resetButton = document.getElementById('resetButton');
const resultDiv = document.getElementById('result');
let currentPlayer = '🌟'; // Player 1 uses 🌟
let gameActive = true;
let board = ['', '', '', '', '', '', '', '', ''];
// Winning conditions
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(e) {
const clickedCell = e.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (board[clickedCellIndex] !== '' || !gameActive) {
return;
}
board[clickedCellIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;
checkResult();
currentPlayer = currentPlayer === '🌟' ? '💖' : '🌟'; // Switch players
}
function checkResult() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
if (board[a] === '' || board[b] === '' || board[c] === '') {
continue;
}
if (board[a] === board[b] && board[a] === board[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
resultDiv.textContent = `Player ${currentPlayer} wins! 🎉`;
gameActive = false;
return;
}
if (!board.includes('')) {
resultDiv.textContent = 'It\'s a draw! 🙁';
gameActive = false;
return;
}
}
function resetGame() {
gameActive = true;
board = ['', '', '', '', '', '', '', '', ''];
resultDiv.textContent = '';
cells.forEach(cell => {
cell.textContent = '';
});
currentPlayer = '🌟'; // Start with player 1
}
// Event Listeners
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.