Here's a simple implementation of a Tic Tac Toe game against an AI using HTML, CSS, and JavaScript. The game includes configurable settings for the AI difficulty and the game board size.
index.html
<!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</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="game-container">
<div class="game-settings">
<label for="board-size">Board Size:</label>
<select id="board-size" onchange="updateBoardSize()">
<option value="3">3x3</option>
<option value="4">4x4</option>
<option value="5">5x5</option>
</select>
<label for="ai-difficulty">AI Difficulty:</label>
<select id="ai-difficulty" onchange="updateAIDifficulty()">
<option value="easy">Easy</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>
</select>
<button id="reset-button" onclick="resetGame()">Reset Game</button>
</div>
<div class="game-board" id="game-board"></div>
<div class="game-status" id="game-status"></div>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
.game-container {
width: 80%;
margin: 40px auto;
text-align: center;
}
.game-settings {
margin-bottom: 20px;
}
.game-board {
display: grid;
grid-gap: 10px;
margin-bottom: 20px;
}
.game-board div {
width: 50px;
height: 50px;
background-color: #f0f0f0;
border: 1px solid #ccc;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
cursor: pointer;
}
.game-board div:hover {
background-color: #ccc;
}
.game-status {
font-size: 24px;
font-weight: bold;
}
#reset-button {
margin-left: 20px;
}
script.js
let boardSize = 3;
let aiDifficulty = 'easy';
let gameBoard = [];
let currentPlayer = 'X';
let gameOver = false;
function updateBoardSize() {
boardSize = parseInt(document.getElementById('board-size').value);
resetGame();
}
function updateAIDifficulty() {
aiDifficulty = document.getElementById('ai-difficulty').value;
}
function resetGame() {
gameBoard = [];
currentPlayer = 'X';
gameOver = false;
document.getElementById('game-status').innerHTML = '';
renderGameBoard();
}
function renderGameBoard() {
const gameBoardElement = document.getElementById('game-board');
gameBoardElement.innerHTML = '';
gameBoardElement.style.gridTemplateColumns = `repeat(${boardSize}, 1fr)`;
for (let i = 0; i < boardSize * boardSize; i++) {
const cell = document.createElement('div');
cell.onclick = handleCellClick;
gameBoardElement.appendChild(cell);
gameBoard.push('');
}
}
function handleCellClick(event) {
if (gameOver) return;
const cellIndex = Array.prototype.indexOf.call(event.target.parentNode.children, event.target);
if (gameBoard[cellIndex] !== '') return;
gameBoard[cellIndex] = currentPlayer;
event.target.innerHTML = currentPlayer;
checkGameOver();
if (!gameOver) {
makeAImove();
}
}
function makeAImove() {
let bestMove = null;
let bestScore = -Infinity;
for (let i = 0; i < boardSize * boardSize; i++) {
if (gameBoard[i] === '') {
gameBoard[i] = 'O';
const score = minimax(gameBoard, 0, false);
gameBoard[i] = '';
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
}
}
gameBoard[bestMove] = 'O';
const cell = document.getElementById('game-board').children[bestMove];
cell.innerHTML = 'O';
checkGameOver();
}
function minimax(board, depth, isMaximizing) {
const winner = checkWinner(board);
if (winner === 'O') {
return 10 - depth;
} else if (winner === 'X') {
return depth - 10;
} else if (winner === 'Tie') {
return 0;
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < boardSize * boardSize; i++) {
if (board[i] === '') {
board[i] = 'O';
const score = minimax(board, depth + 1, false);
board[i] = '';
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < boardSize * boardSize; i++) {
if (board[i] === '') {
board[i] = 'X';
const score = minimax(board, depth + 1, true);
board[i] = '';
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
}
function checkWinner(board) {
for (let i = 0; i < boardSize; i++) {
let row = [];
let col = [];
for (let j = 0; j < boardSize; j++) {
row.push(board[i * boardSize + j]);
col.push(board[j * boardSize + i]);
}
if (row.every(cell => cell === row[0]) && row[0] !== '') {
return row[0];
}
if (col.every(cell => cell === col[0]) && col[0] !== '') {
return col[0];
}
}
let diagonal1 = [];
let diagonal2 = [];
for (let i = 0; i < boardSize; i++) {
diagonal1.push(board[i * boardSize + i]);
diagonal2.push(board[i * boardSize + boardSize - i - 1]);
}
if (diagonal1.every(cell => cell === diagonal1[0]) && diagonal1[0] !== '') {
return diagonal1[0];
}
if (diagonal2.every(cell => cell === diagonal2[0]) && diagonal2[0] !== '') {
return diagonal2[0];
}
if (board.every(cell => cell !== '')) {
return 'Tie';
}
return null;
}
function checkGameOver() {
const winner = checkWinner(gameBoard);
if (winner === 'X') {
document.getElementById('game-status').innerHTML = 'You win!';
gameOver = true;
} else if (winner === 'O') {
document.getElementById('game-status').innerHTML = 'AI wins!';
gameOver = true;
} else if (winner === 'Tie') {
document.getElementById('game-status').innerHTML = 'It\'s a tie!';
gameOver = true;
}
}
renderGameBoard();
This code creates a Tic Tac Toe game with a configurable board size and AI difficulty. The game uses the minimax algorithm to make AI moves. The game also checks for a winner or tie after each move.
You can adjust the AI difficulty by changing the aiDifficulty variable in the script.js file. The easy difficulty uses a simple random move selection, while the medium and hard difficulties use the minimax algorithm with different depths.
You can also adjust the board size by changing the boardSize variable in the script.js file. The game will automatically update the board size and the AI moves accordingly.
Note that this is a basic implementation of Tic Tac Toe, and you may want to add additional features such as user authentication, game history, or more advanced AI algorithms.