Upload files to "/"
This commit is contained in:
+28
@@ -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 Game</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Tic Tac Toe</h1>
|
||||
<div id="status">Player X's turn</div>
|
||||
<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="resetBtn">Reset Game</button>
|
||||
</div>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,76 @@
|
||||
const board = Array.from(document.querySelectorAll('.cell'));
|
||||
const status = document.getElementById('status');
|
||||
const resetBtn = document.getElementById('resetBtn');
|
||||
|
||||
let currentPlayer = 'X';
|
||||
let gameActive = true;
|
||||
let gameState = ['', '', '', '', '', '', '', '', ''];
|
||||
|
||||
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
|
||||
];
|
||||
|
||||
const winningMessage = () => `Player ${currentPlayer} wins!`;
|
||||
const drawMessage = () => `Game ended in a draw!`;
|
||||
const currentPlayerTurn = () => `Player ${currentPlayer}'s turn`;
|
||||
|
||||
status.innerHTML = currentPlayerTurn();
|
||||
|
||||
function handleCellClick(e) {
|
||||
const cell = e.target;
|
||||
const cellIndex = parseInt(cell.getAttribute('data-index'));
|
||||
|
||||
if (gameState[cellIndex] !== '' || !gameActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
gameState[cellIndex] = currentPlayer;
|
||||
cell.innerHTML = currentPlayer;
|
||||
cell.classList.add(currentPlayer.toLowerCase());
|
||||
|
||||
if (checkWin()) {
|
||||
status.innerHTML = winningMessage();
|
||||
gameActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (checkDraw()) {
|
||||
status.innerHTML = drawMessage();
|
||||
gameActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
|
||||
status.innerHTML = currentPlayerTurn();
|
||||
}
|
||||
|
||||
function checkWin() {
|
||||
for (let i = 0; i < winningConditions.length; i++) {
|
||||
const [a, b, c] = winningConditions[i];
|
||||
if (gameState[a] && gameState[a] === gameState[b] && gameState[a] === gameState[c]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkDraw() {
|
||||
return !gameState.includes('');
|
||||
}
|
||||
|
||||
function resetGame() {
|
||||
currentPlayer = 'X';
|
||||
gameActive = true;
|
||||
gameState = ['', '', '', '', '', '', '', '', ''];
|
||||
status.innerHTML = currentPlayerTurn();
|
||||
|
||||
board.forEach(cell => {
|
||||
cell.innerHTML = '';
|
||||
cell.classList.remove('x', 'o');
|
||||
});
|
||||
}
|
||||
|
||||
board.forEach(cell => cell.addEventListener('click', handleCellClick));
|
||||
resetBtn.addEventListener('click', resetGame);
|
||||
@@ -0,0 +1,75 @@
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
background-color: #f0f0f0;
|
||||
}
|
||||
|
||||
.container {
|
||||
text-align: center;
|
||||
background-color: white;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
#status {
|
||||
font-size: 1.2em;
|
||||
margin-bottom: 20px;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.board {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 100px);
|
||||
grid-gap: 5px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.cell {
|
||||
width: 100px;
|
||||
height: 100px;
|
||||
background-color: #e0e0e0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 3em;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
border-radius: 5px;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
.cell:hover {
|
||||
background-color: #d0d0d0;
|
||||
}
|
||||
|
||||
.cell.x {
|
||||
color: #ff4444;
|
||||
}
|
||||
|
||||
.cell.o {
|
||||
color: #4444ff;
|
||||
}
|
||||
|
||||
#resetBtn {
|
||||
padding: 10px 20px;
|
||||
font-size: 1em;
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
#resetBtn:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
Reference in New Issue
Block a user