Create a site where you play Tic Tac To games with html, Js, etc.
- User
Create a site where you play Tic Tac To games with html, Js, etc.
- Assistant
Create a site where you play Tic Tac To games with html, Js, etc.
Creating a simple Tic Tac Toe game using HTML, CSS, and JavaScript is a great way to practice web development skills. Below is a complete example with explanations that you can use to build your own Tic Tac Toe website.
For simplicity, we'll use a single HTML file that contains embedded CSS and JavaScript. However, for larger projects, it's recommended to separate these into different files.
tic-tac-toe/
│
└── index.html
index.html<!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 Game</title>
<style>
/* CSS Reset */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
margin-bottom: 20px;
color: #333;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
}
.cell {
background-color: #fff;
border: 2px solid #333;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
cursor: pointer;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #f0f0f0;
}
.info {
margin-top: 20px;
font-size: 1.2em;
color: #333;
}
.restart-btn {
margin-top: 10px;
padding: 10px 20px;
font-size: 1em;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
.restart-btn:hover {
background-color: #555;
}
</style>
</head>
<body>
<div class="game-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>
<div class="info" id="info">Player X's turn</div>
<button class="restart-btn" id="restartBtn">Restart Game</button>
</div>
<script>
// Game State
const boardElement = document.getElementById('board');
const cells = Array.from(document.getElementsByClassName('cell'));
const info = document.getElementById('info');
const restartBtn = document.getElementById('restartBtn');
let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
let isGameActive = true;
// 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 to handle cell click
function handleCellClick(event) {
const clickedCell = event.target;
const cellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[cellIndex] !== '' || !isGameActive) {
return;
}
updateCell(clickedCell, cellIndex);
checkResult();
}
// Update the cell with the current player's symbol
function updateCell(cell, index) {
gameState[index] = currentPlayer;
cell.textContent = currentPlayer;
}
// Switch player turns
function switchPlayer() {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
info.textContent = `Player ${currentPlayer}'s turn`;
}
// Check for win or draw
function checkResult() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const condition = winningConditions[i];
const a = gameState[condition[0]];
const b = gameState[condition[1]];
const c = gameState[condition[2]];
if (a === '' || b === '' || c === '') {
continue;
}
if (a === b && b === c) {
roundWon = true;
break;
}
}
if (roundWon) {
info.textContent = `Player ${currentPlayer} wins!`;
isGameActive = false;
return;
}
if (!gameState.includes('')) {
info.textContent = `It's a draw!`;
isGameActive = false;
return;
}
switchPlayer();
}
// Restart the game
function restartGame() {
currentPlayer = 'X';
gameState = ['', '', '', '', '', '', '', '', ''];
isGameActive = true;
info.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => {
cell.textContent = '';
});
}
// Add event listeners to cells
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
// Add event listener to restart button
restartBtn.addEventListener('click', restartGame);
</script>
</body>
</html>
HTML Structure:
<h1>): Displays the title of the game.<div class="board">): A 3x3 grid representing the Tic Tac Toe board. Each cell has a data-index attribute to identify its position.<div class="info">): Shows the current game status, such as which player's turn it is or who won.<button>): Allows players to restart the game at any time.CSS Styling:
body uses Flexbox to center the game container both vertically and horizontally..board uses CSS Grid to create a 3x3 layout for the cells..cell is styled with borders, background color, and centered text.JavaScript Functionality:
currentPlayer: Tracks whose turn it is ('X' or 'O').gameState: An array representing the board's current state.isGameActive: A boolean to determine if the game is ongoing.handleCellClick: Handles user clicks on cells, updates the game state, and checks for a win or draw.updateCell: Updates the clicked cell with the current player's symbol.switchPlayer: Switches the turn to the other player.checkResult: Checks if the current move results in a win or a draw.restartGame: Resets the game to its initial state.Create the Project Folder:
tic-tac-toe.Create the HTML File:
index.html.Paste the Code:
index.html and save the file.Open in Browser:
index.html file in your preferred web browser (e.g., Chrome, Firefox).Play the Game:
X or O. The game will automatically detect wins or draws and allow you to restart the game using the "Restart Game" button.Here are some ideas to further enhance your Tic Tac Toe game:
This simple Tic Tac Toe game demonstrates the basics of using HTML, CSS, and JavaScript to create an interactive web application. Feel free to modify and expand upon this foundation to create more complex and feature-rich games!
Not yet included in a published release.