o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Create a a ticktac toe game using html css Java all in one file
- User
Create a a ticktac toe game using html css Java all in one file
- Assistant
Create a a ticktac toe game using html css Java all in one file
Certainly! Below is a complete Tic-Tac-Toe game implemented in a single HTML file using HTML, CSS, and JavaScript. You can copy this code into a .html file and open it in your web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tic-Tac-Toe Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
margin: 0;
}
.game-container {
text-align: center;
}
h1 {
margin-bottom: 20px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: 0 auto 20px;
}
.cell {
background-color: #fff;
border: 2px solid #333;
font-size: 2em;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #eaeaea;
}
.cell.disabled {
cursor: not-allowed;
background-color: #ddd;
}
.status {
margin-bottom: 20px;
font-size: 1.2em;
}
.reset-button {
padding: 10px 20px;
font-size: 1em;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
.reset-button:hover {
background-color: #555;
}
</style>
</head>
<body>
<div class="game-container">
<h1>Tic-Tac-Toe</h1>
<div class="status" 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 class="reset-button" id="reset">Reset Game</button>
</div>
<script>
const cells = document.querySelectorAll('.cell');
const statusDisplay = document.getElementById('status');
const resetButton = document.getElementById('reset');
let board = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let isGameActive = true;
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 cellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (board[cellIndex] !== '' || !isGameActive) {
return;
}
updateCell(clickedCell, cellIndex);
checkResult();
}
function updateCell(cell, index) {
board[index] = currentPlayer;
cell.textContent = currentPlayer;
cell.classList.add('disabled');
}
function checkResult() {
let roundWon = false;
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
statusDisplay.textContent = `Player ${currentPlayer} wins!`;
isGameActive = false;
return;
}
if (!board.includes('')) {
statusDisplay.textContent = `It's a draw!`;
isGameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
statusDisplay.textContent = `Player ${currentPlayer}'s turn`;
}
function resetGame() {
board = ['', '', '', '', '', '', '', '', ''];
isGameActive = true;
currentPlayer = 'X';
statusDisplay.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('disabled');
});
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);
</script>
</body>
</html>
HTML Structure:
div with the class cell and a data-index attribute to identify its position.Styling with CSS:
Game Logic with JavaScript:
cells: All the cell elements.statusDisplay: The element that shows the current game status.resetButton: The reset button element.board: An array representing the game state.currentPlayer: Tracks whose turn it is ('X' or 'O').isGameActive: Indicates if the game is currently active.handleCellClick: Handles the logic when a cell is clicked.updateCell: Updates the cell's display and the game state.checkResult: Checks if a player has won or if the game is a draw.resetGame: Resets the game to its initial state.Play the Game:
X or O).Reset the Game:
Feel free to customize the styles or enhance the functionality as per your requirements!
Not yet included in a published release.