Sure, I can help you with that. Here's an example of how you can create a Tic Tac Toe game using HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<style>
body {
text-align: center;
font-family: fantasy;
}
button {
width: 100px;
height: 100px;
margin: 5px;
font-size: 24px;
}
#game-board {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
</style>
</head>
<body>
<h1>Tic Tac Toe</h1>
<button id="multiplayer" onclick="startGame('multiplayer')">Multiplayer</button>
<button id="solo" onclick="startGame('solo')">Solo</button>
<div id="game-board"></div>
<script>
let currentPlayer;
let gamePlaying = false;
let gameMode;
const squares = [...document.querySelectorAll('div#game-board button')];
const winningCombos = [
[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 startGame(mode) {
gameBoard.style.display = 'block';
currentPlayer = 'X';
gamePlaying = true;
gameMode = mode;
// Prompt for player names
if (gameMode === 'multiplayer') {
const player1 = prompt('Enter Player 1 name');
const player2 = prompt('Enter Player 2 name');
document.getElementById('player1').textContent = player1;
document.getElementById('player2').textContent = player2;
} else if (gameMode === 'solo') {
const playerName = prompt('Enter Player name');
document.getElementById('player1').textContent = playerName;
document.getElementById('player2').textContent = 'AI';
}
// Add event listener to each square
squares.forEach(square => square.addEventListener('click', handleClick));
}
function handleClick(event) {
const square = event.target;
const squareIndex = squares.findIndex(square => square === event.target);
if (squares[squareIndex].textContent !== '' || !gamePlaying) return;
squares[squareIndex].textContent = currentPlayer;
squares[squareIndex].style.background = 'lightgreen';
if (checkWin(currentPlayer)) {
document.querySelector('.message').textContent =
`Congratulations ${currentPlayer}, you won the game.`;
gamePlaying = false;
} else if (checkDraw()) {
document.querySelector('.message').textContent = 'It\'s a draw!';
gamePlaying = false;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
// AI Player move (solo game)
if (gameMode === 'solo' && gamePlaying) {
makeAIMove();
}
}
function checkWin(player) {
return winningCombos.some(combination => {
return combination.every(index => {
return squares[index].textContent === player;
});
});
}
function checkDraw() {
return squares.every(square => square.textContent !== '');
}
function makeAIMove() {
const emptySquares = squares.reduce((accumulator, currentValue, currentIndex) => {
if (currentValue.textContent === '') {
accumulator.push(currentIndex);
}
return accumulator;
}, []);
const randomIndex = Math.floor(Math.random() * emptySquares.length);
const squareIndex = emptySquares[randomIndex];
squares[squareIndex].textContent = currentPlayer;
squares[squareIndex].style.background = 'lightgreen';
if (checkWin(currentPlayer)) {
document.querySelector('.message').textContent =
`Congratulations ${currentPlayer}, you won the game.`;
gamePlaying = false;
} else if (checkDraw()) {
document.querySelector('.message').textContent = 'It\'s a draw!';
gamePlaying = false;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
</script>
</body>
</html>
Note: This is a basic implementation of the Tic Tac Toe game and does not include advanced AI for the solo mode. The AI just randomly selects an empty square for its move. You can improve the AI algorithm for a more challenging gameplay experience.
Hope this helps you get started!