Here's a simple strategy game called "Territory Wars" where two players take turns capturing territories on a grid. The game is implemented using HTML, CSS, and JavaScript.
Game Rules
The game is played on a 10x10 grid.
Players start with 3 territories each.
On a player's turn, they can click on one of their territories to capture an adjacent neutral territory.
The AI will automatically take its turn after the player's move.
The game ends when one player captures all territories or when there are no more neutral territories left, at which point the player with the most territories wins.
Code Implementation
html
<!DOCTYPE html>
<html>
<head>
<title>Territory Wars</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
.grid {
display: grid;
grid-template-columns: repeat(10, 50px);
grid-template-rows: repeat(10, 50px);
gap: 1px;
margin: 20px auto;
}
.cell {
width: 50px;
height: 50px;
border: 1px solid black;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
font-weight: bold;
}
.player1 {
background-color: blue;
color: white;
}
.player2 {
background-color: red;
color: white;
}
.neutral {
background-color: gray;
}
#status {
margin: 20px;
font-size: 20px;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Territory Wars</h1>
<div id="status">Player 1's turn</div>
<div id="gameBoard" class="grid"></div>
<button id="restartButton">Restart Game</button>
<script>
// Game state
let grid = Array(10).fill().map(() => Array(10).fill(0)); // 0: neutral, 1: player1, 2: player2
let currentPlayer = 1;
let player1Territories = [];
let player2Territories = [];
let gameOver = false;
// Initialize the game board
function initGame() {
grid = Array(10).fill().map(() => Array(10).fill(0));
currentPlayer = 1;
player1Territories = [];
player2Territories = [];
gameOver = false;
const board = document.getElementById('gameBoard');
board.innerHTML = '';
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
const cell = document.createElement('div');
cell.className = 'cell neutral';
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', () => handleCellClick(i, j));
board.appendChild(cell);
}
}
// Place initial territories for players
placeInitialTerritories();
updateBoard();
document.getElementById('status').textContent = "Player 1's turn";
}
// Place initial territories for players
function placeInitialTerritories() {
// Place 3 territories for player 1
for (let i = 0; i < 3; i++) {
let row, col;
do {
row = Math.floor(Math.random() * 10);
col = Math.floor(Math.random() * 10);
} while (grid[row][col] !== 0);
grid[row][col] = 1;
player1Territories.push({row, col});
}
// Place 3 territories for player 2
for (let i = 0; i < 3; i++) {
let row, col;
do {
row = Math.floor(Math.random() * 10);
col = Math.floor(Math.random() * 10);
} while (grid[row][col] !== 0);
grid[row][col] = 2;
player2Territories.push({row, col});
}
}
// Update the board display
function updateBoard() {
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
if (grid[row][col] === 1) {
cell.className = 'cell player1';
cell.textContent = '';
} else if (grid[row][col] === 2) {
cell.className = 'cell player2';
cell.textContent = '';
} else {
cell.className = 'cell neutral';
cell.textContent = '';
}
});
}
// Handle cell click
function handleCellClick(row, col) {
if (gameOver) return;
if (currentPlayer === 1 && grid[row][col] === 1) {
// Player 1 clicked on their own territory
// Find adjacent neutral territories
const adjacentCells = getAdjacentCells(row, col);
const neutralAdjacentCells = adjacentCells.filter(cell => grid[cell.row][cell.col] === 0);
if (neutralAdjacentCells.length > 0) {
// Capture a random adjacent neutral territory
const randomIndex = Math.floor(Math.random() * neutralAdjacentCells.length);
const {row: newRow, col: newCol} = neutralAdjacentCells[randomIndex];
grid[newRow][newCol] = 1;
player1Territories.push({row: newRow, col: newCol});
updateBoard();
// Check win condition
if (checkWinCondition()) {
gameOver = true;
document.getElementById('status').textContent = "Player 1 wins!";
return;
}
// End turn
currentPlayer = 2;
document.getElementById('status').textContent = "Player 2's turn";
// AI's turn
setTimeout(aiTurn, 1000);
}
}
}
// Get adjacent cells
function getAdjacentCells(row, col) {
const adjacentCells = [];
// Check all 4 directions (up, down, left, right)
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
for (let [i, j] of directions) {
const newRow = row + i;
const newCol = col + j;
if (newRow >= 0 && newRow < 10 && newCol >= 0 && newCol < 10) {
adjacentCells.push({row: newRow, col: newCol});
}
}
return adjacentCells;
}
// AI's turn
function aiTurn() {
if (gameOver) return;
// Simple AI: find a territory with adjacent neutral cells and capture one
for (let territory of player2Territories) {
const {row, col} = territory;
const adjacentCells = getAdjacentCells(row, col);
const neutralAdjacentCells = adjacentCells.filter(cell => grid[cell.row][cell.col] === 0);
if (neutralAdjacentCells.length > 0) {
// Capture a random adjacent neutral territory
const randomIndex = Math.floor(Math.random() * neutralAdjacentCells.length);
const {row: newRow, col: newCol} = neutralAdjacentCells[randomIndex];
grid[newRow][newCol] = 2;
player2Territories.push({row: newRow, col: newCol});
updateBoard();
// Check win condition
if (checkWinCondition()) {
gameOver = true;
document.getElementById('status').textContent = "Player 2 wins!";
return;
}
// End turn
currentPlayer = 1;
document.getElementById('status').textContent = "Player 1's turn";
return;
}
}
// If no neutral territories adjacent, just end the turn
currentPlayer = 1;
document.getElementById('status').textContent = "Player 1's turn";
}
// Check win condition
function checkWinCondition() {
let player1Count = 0;
let player2Count = 0;
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
if (grid[i][j] === 1) player1Count++;
if (grid[i][j] === 2) player2Count++;
}
}
// If one player has all territories, they win
if (player1Count === 100) return true;
if (player2Count === 100) return true;
// If no more neutral territories left, the player with more territories wins
if (player1Count + player2Count === 100) {
if (player1Count > player2Count) {
document.getElementById('status').textContent = "Player 1 wins!";
return true;
} else if (player2Count > player1Count) {
document.getElementById('status').textContent = "Player 2 wins!";
return true;
} else {
document.getElementById('status').textContent = "It's a tie!";
return true;
}
}
return false;
}
// Restart game
document.getElementById('restartButton').addEventListener('click', initGame);
// Initialize the game
initGame();
</script>
</body>
</html>
How to Play
Open the HTML file in a web browser.
Click on one of your blue territories to capture an adjacent neutral territory.
The AI will then take its turn, capturing an adjacent neutral territory from one of its red territories.
The game continues until one player captures all territories or when there are no more neutral territories left, at which point the player with the most territories wins.
Click the "Restart Game" button to start a new game at any time.
This game provides a simple yet engaging strategy experience where players must think about which territories to capture to maximize their control over the board.