Make a tic-tac-toe html,css,js
- User
Make a tic-tac-toe html,css,js
- Assistant
Make a tic-tac-toe html,css,js
Sure! I can help you create a simple Tic-Tac-Toe game using HTML, CSS, and JavaScript. Below is a complete, step-by-step guide to building the game. You can copy the code into your own files and run it in your browser.
Create three files in your project directory:
index.html – The HTML structure of the game.styles.css – The CSS styles for the game.script.js – The JavaScript logic for the game.index.html)This file sets up the structure of the Tic-Tac-Toe board and includes links to the CSS and JavaScript files.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<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="game-container">
<h1>Tic-Tac-Toe</h1>
<div class="game-status" id="gameStatus">Player X's turn</div>
<div class="game-board">
<div class="cell" data-cell-index="0"></div>
<div class="cell" data-cell-index="1"></div>
<div class="cell" data-cell-index="2"></div>
<div class="cell" data-cell-index="3"></div>
<div class="cell" data-cell-index="4"></div>
<div class="cell" data-cell-index="5"></div>
<div class="cell" data-cell-index="6"></div>
<div class="cell" data-cell-index="7"></div>
<div class="cell" data-cell-index="8"></div>
</div>
<button id="restartButton">Restart Game</button>
</div>
<script src="script.js"></script>
</body>
</html>
<head>): Includes the page title and links to the CSS file.<body>):
styles.css)This file styles the game, making it visually appealing and responsive.
/* Reset some default styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: #f0f0f0;
font-family: Arial, Helvetica, sans-serif;
display: flex;
height: 100vh;
align-items: center;
justify-content: center;
}
.game-container {
background-color: #fff;
padding: 20px 30px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
}
h1 {
margin-bottom: 20px;
color: #333;
}
.game-status {
margin-bottom: 20px;
font-size: 1.2em;
color: #555;
}
.game-board {
display: grid;
grid-template-columns: repeat(3, 100px);
gap: 10px;
margin-bottom: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: #e0e0e0;
display: flex;
align-items: center;
justify-content: center;
font-size: 2em;
cursor: pointer;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #d5d5d5;
}
.cell.X {
color: #ff4d4d;
}
.cell.O {
color: #4d79ff;
}
button {
padding: 10px 20px;
font-size: 1em;
background-color: #4d79ff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #3b5fdc;
}
script.js)This file contains the game logic, handling player turns, checking for winners, and resetting the game.
const cells = document.querySelectorAll('.cell');
const gameStatus = document.getElementById('gameStatus');
const restartButton = document.getElementById('restartButton');
let currentPlayer = 'X';
let gameActive = true;
const gameState = ["", "", "", "", "", "", "", "", ""];
// Winning combinations
const winningConditions = [
[0, 1, 2], // Rows
[3, 4, 5],
[6, 7, 8],
[0, 3, 6], // Columns
[1, 4, 7],
[2, 5, 8],
[0, 4, 8], // Diagonals
[2, 4, 6]
];
// Messages
const winMessage = () => `Player ${currentPlayer} has won!`;
const drawMessage = () => `Game ended in a draw!`;
const currentPlayerTurn = () => `Player ${currentPlayer}'s turn`;
// Initialize game
gameStatus.textContent = currentPlayerTurn();
// Handle cell click
function handleCellClick(e) {
const clickedCell = e.target;
const clickedIndex = parseInt(clickedCell.getAttribute('data-cell-index'));
if (gameState[clickedIndex] !== "" || !gameActive) {
return;
}
updateCell(clickedCell, clickedIndex);
checkResult();
}
// Update cell with current player's symbol
function updateCell(cell, index) {
gameState[index] = currentPlayer;
cell.textContent = currentPlayer;
cell.classList.add(currentPlayer);
}
// Switch to the next player
function switchPlayer() {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
gameStatus.textContent = currentPlayerTurn();
}
// Check if there's a win or a draw
function checkResult() {
let roundWon = false;
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (gameState[a] && gameState[a] === gameState[b] && gameState[a] === gameState[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
gameStatus.textContent = winMessage();
gameActive = false;
return;
}
if (!gameState.includes("")) {
gameStatus.textContent = drawMessage();
gameActive = false;
return;
}
switchPlayer();
}
// Restart the game
function restartGame() {
currentPlayer = 'X';
gameActive = true;
gameState.fill("");
gameStatus.textContent = currentPlayerTurn();
cells.forEach(cell => {
cell.textContent = "";
cell.classList.remove('X', 'O');
});
}
// Add event listeners to cells and restart button
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartButton.addEventListener('click', restartGame);
Selecting Elements:
Game Variables:
'X' or 'O').Winning Conditions:
Messages:
Initialize Game:
handleCellClick:
updateCell:
switchPlayer:
checkResult:
restartGame:
Event Listeners:
Set Up:
index.html, styles.css, and script.js with the code provided above.Open in Browser:
index.html in your preferred web browser.Play the Game:
X or O).Here are some ideas if you want to enhance the game further:
Feel free to modify and expand upon this basic implementation to suit your needs. Enjoy building and playing your Tic-Tac-Toe game!
Not yet included in a published release.