Below, I'll provide a complete, simple strategy game that you can run in a web browser. As a web game developer, I've chosen to create a basic version of Tic-Tac-Toe with a twist to add more strategy: it's player vs. AI, and the AI uses a simple minimax algorithm for intelligent moves (making it a real strategy challenge, as you have to outthink the AI). This keeps it simple to implement and play, but engaging.
Game Rules
- It's a 3x3 grid.
- You play as "X" (human player), and the AI plays as "O".
- Players take turns marking a cell.
- The first to get 3 in a row (horizontal, vertical, or diagonal) wins.
- If the board fills up without a winner, it's a tie.
- Strategy comes from anticipating the AI's moves – the AI is "smart" and will block you or win if possible.
How to Run the Game
- Copy the code below into three separate files:
index.html (the main page)
styles.css (styling)
script.js (game logic)
- Open
index.html in a web browser (e.g., Chrome, Firefox).
- Click on the grid cells to play. The AI will respond automatically.
- Reset the game with the button.
This is a fully functional web game using HTML, CSS, and JavaScript. No external libraries are needed.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Strategy Game: Tic-Tac-Toe vs AI</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Tic-Tac-Toe Strategy Game</h1>
<p>Play as X. Outsmart the AI (O) to win!</p>
<div id="board"></div>
<button id="reset">Reset Game</button>
<p id="status">Your turn (X)!</p>
<script src="script.js"></script>
</body>
</html>
styles.css
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
h1 {
color: #333;
}
#board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 5px;
margin: 20px auto;
width: 310px;
}
.cell {
width: 100px;
height: 100px;
background-color: #fff;
border: 2px solid #333;
font-size: 48px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.cell:hover {
background-color: #eee;
}
#reset {
padding: 10px 20px;
font-size: 16px;
margin-top: 20px;
cursor: pointer;
}
#status {
font-size: 18px;
margin-top: 10px;
color: #007bff;
}
script.js
// Game variables
let board = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X'; // Human starts as X
let gameActive = true;
const statusDisplay = document.getElementById('status');
const resetButton = document.getElementById('reset');
// Winning combinations
const winningConditions = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
[0, 4, 8], [2, 4, 6] // Diagonals
];
// Create the game board
function createBoard() {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
board.forEach((cell, index) => {
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.innerText = cell;
cellElement.addEventListener('click', () => handleCellClick(index));
boardElement.appendChild(cellElement);
});
}
// Handle cell click (human move)
function handleCellClick(index) {
if (!gameActive || board[index] !== '' || currentPlayer !== 'X') return;
board[index] = 'X';
createBoard();
checkResult();
if (gameActive) {
currentPlayer = 'O';
statusDisplay.innerText = "AI's turn (O)...";
setTimeout(aiMove, 500); // Slight delay for realism
}
}
// AI move using minimax
function aiMove() {
let bestScore = -Infinity;
let bestMove;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = 'O';
let score = minimax(board, 0, false);
board[i] = '';
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
}
}
board[bestMove] = 'O';
createBoard();
checkResult();
currentPlayer = 'X';
if (gameActive) statusDisplay.innerText = "Your turn (X)!";
}
// Minimax algorithm for AI strategy
function minimax(board, depth, isMaximizing) {
let result = checkWinner();
if (result !== null) {
return result === 'O' ? 10 - depth : result === 'X' ? depth - 10 : 0;
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = 'O';
let score = minimax(board, depth + 1, false);
board[i] = '';
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = 'X';
let score = minimax(board, depth + 1, true);
board[i] = '';
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
}
// Check for winner or tie
function checkWinner() {
for (let condition of winningConditions) {
let [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a];
}
}
if (!board.includes('')) return 'Tie';
return null;
}
function checkResult() {
let winner = checkWinner();
if (winner) {
gameActive = false;
if (winner === 'Tie') {
statusDisplay.innerText = "It's a tie!";
} else {
statusDisplay.innerText = `${winner} wins!`;
}
}
}
// Reset game
function resetGame() {
board = ['', '', '', '', '', '', '', '', ''];
currentPlayer = 'X';
gameActive = true;
statusDisplay.innerText = "Your turn (X)!";
createBoard();
}
// Initialize
createBoard();
resetButton.addEventListener('click', resetGame);
How the Game Works
- Human Player: Click an empty cell to place "X".
- AI Player: Uses minimax to make optimal moves, so it's unbeatable (it will always tie or win if possible). This adds strategy – you need to think ahead!
- Win/Tie Detection: Automatically checks after each move.
- Reset: Click the button to start over.
Why This is a Strategy Game
- It's turn-based, requiring foresight to block the opponent or set up wins.
- The AI's intelligence forces strategic thinking (e.g., controlling the center, creating forks).
- Simple rules, but deep gameplay – perfect for a beginner web game.
If you'd like enhancements (e.g., larger grid, multiplayer over network, different theme, or a completely different game like a basic tower defense), let me know! I can iterate on this.