A real-time multiplayer territory conquest strategy game built with HTML5 Canvas, Node.js, and Socket.io. Players compete to control territories on a procedurally generated map through strategic attacks and expansion.
- Turn-based Strategy: Players take turns to attack adjacent territories and expand their empire
- Procedural Map Generation: Each game features a unique map with 60 territories generated using Voronoi-like algorithms
- Real-time Multiplayer: 2-8 players can compete simultaneously with real-time game state synchronization
- AI Opponents: Intelligent bots with configurable difficulty levels (easy/medium/hard)
- Combat System: Strategic combat resolution based on troop counts with randomness factor
- Resource Management: Territories generate troops over time, requiring strategic planning
- Responsive Canvas Rendering: Smooth HTML5 Canvas graphics with territory highlighting and animations
- Lobby System: Players can create games, invite friends via lobby ID, and configure match settings
- Turn Timer: 45-second turn limit to keep games moving
- Game State Synchronization: Server-authoritative architecture prevents cheating
- Spectator Mode: Eliminated players can continue watching the game
- Reconnection Handling: Graceful handling of player disconnections
territory-conquest-game/
├── server/
│ └── server.js # Node.js backend with Socket.io
├── shared/
│ ├── Territory.js # Territory class (used by both client/server)
│ ├── MapGenerator.js # Procedural map generation algorithm
│ ├── GameState.js # Core game logic and state management
│ └── AIBot.js # AI opponent logic
├── public/
│ ├── index.html # Main HTML page
│ ├── css/
│ │ └── style.css # Game styling
│ └── js/
│ ├── game.js # Main client-side game controller
│ ├── renderer.js # Canvas rendering engine
│ ├── input.js # Input handling (mouse/touch)
│ └── ui.js # UI management
├── package.json
└── README.md
- Node.js (v14 or higher)
- npm (Node Package Manager)
-
Clone or download the repository
cd territory-conquest-game -
Install dependencies
npm install
-
Start the server
npm start
For development with auto-reload:
npm run dev
-
Open the game
- Open your browser and navigate to
http://localhost:3000 - The game is now ready to play!
- Open your browser and navigate to
-
Create a Lobby
- Enter your name
- Click "Create Game"
- Share the Lobby ID with friends
-
Join a Game
- Enter your name
- Enter the Lobby ID
- Click "Join Game"
-
Configure the Match
- Host can add AI bots using the "Add Bot" button
- Players click "Ready" when prepared
- Host clicks "Start Game" when all players are ready
- Click on one of your territories to select it (territories you own are highlighted in your color)
- Click on an adjacent enemy or neutral territory to attack
- The attack is automatically resolved based on troop counts
- Click "End Turn" when you're done attacking
- Attack Power = (Attacker Troops - 1) × Random Factor (0.8-1.2)
- Defense Power = Defender Troops × Random Factor (0.8-1.2)
- If Attack Power > Defense Power: Attacker conquers the territory
- Otherwise: Both sides lose troops but defender retains the territory
- Each territory generates 1 troop per turn for its owner
- You must leave at least 1 troop in a territory to attack from it
- Expand strategically to control more territory and generate more troops
- Eliminate all opponents by conquering all their territories
- Last player standing wins
- Game ends when only one player remains or all territories are owned
- Early Game: Focus on capturing neutral territories for easy expansion
- Mid Game: Identify weak opponents and target their territories
- Late Game: Defend borders while pressing advantages
- Always keep strong territories on your borders to prevent enemy breakthroughs
- Watch the turn timer - plan your moves in advance!
The map generator uses a Voronoi-like approach:
- Point Generation: Distributes seed points across the map using Poisson-disc sampling for even spacing
- Territory Creation: Creates polygonal territories by assigning each grid cell to its nearest seed point
- Boundary Extraction: Extracts territory boundaries using convex hull algorithm
- Neighbor Calculation: Determines adjacent territories based on proximity of centers
See shared/MapGenerator.js for implementation details.
The AI evaluates potential attacks using multiple factors:
- Troop Advantage: Prefers attacking weaker territories
- Strategic Value: Considers number of neighbors and connectivity
- Territorial Expansion: Prioritizes territories that connect to existing holdings
- Defensive Considerations: Avoids weakening border territories
- Opponent Strength: Targets weaker players preferentially
AI difficulty levels adjust aggressiveness and decision-making quality. See shared/AIBot.js for details.
- Server-Authoritative: All game logic runs on the server to prevent cheating
- Event-Based: Socket.io events trigger game state updates
- Optimistic UI: Client-side rendering updates immediately for responsiveness
- State Synchronization: Full game state is periodically broadcast to all clients
Key events:
createLobby/joinLobby: Lobby managementstartGame: Initialize game stateattack: Process combatendTurn: Advance to next playergameStateUpdate: Broadcast updated stategameEnded: Handle victory conditions
{
turnTimeLimit: 45000, // Turn duration in milliseconds
mapWidth: 1200, // Map width in pixels
mapHeight: 800, // Map height in pixels
territoryCount: 60, // Number of territories to generate
troopGenerationRate: 1 // Troops generated per territory per turn
}- Port: Set via
PORTenvironment variable (default: 3000) - Max Players: 8 per game (configurable in lobby)
- Chrome/Edge: Full support
- Firefox: Full support
- Safari: Full support
- Mobile browsers: Touch controls supported
- Game Logic: Modify
shared/GameState.js - AI Behavior: Update
shared/AIBot.js - Rendering: Edit
public/js/renderer.js - UI: Modify
public/js/ui.jsandpublic/css/style.css - Networking: Update both
server/server.jsandpublic/js/game.js
- Test locally with multiple browser tabs
- Add AI bots to test single-player scenarios
- Use browser dev tools to monitor Socket.io events
- No fog of war (all territories visible)
- No reconnection for disconnected players (player slot remains but is inactive)
- Single game mode (no variations)
- No persistent statistics or match history
Potential features for future development:
- Fog of War: Only show territories adjacent to your own
- Territory Types: Mountains, water, forests with different properties
- Power-ups: Special abilities or bonuses
- Multiple Game Modes: Capture the flag, king of the hill, etc.
- Persistent Stats: Win/loss tracking, leaderboards
- Reconnection Logic: Allow players to rejoin after disconnection
- Mini-map: Overview for large maps
- Sound Effects: Audio feedback for attacks and victories
- Animations: Smooth territory transitions and combat effects
- Mobile App: Native mobile version
- Ensure Node.js is installed:
node --version - Check if port 3000 is already in use
- Verify dependencies are installed:
npm install
- Ensure server is running
- Check firewall settings
- Try
http://localhost:3000explicitly
- Close other applications
- Reduce number of players/territories
- Check network connection for multiplayer
- Refresh the browser
- Check browser console for errors (F12)
- Ensure Canvas is supported in your browser
Built with:
- Node.js - Backend runtime
- Express - Web server
- Socket.io - Real-time communication
- HTML5 Canvas - Graphics rendering
Inspired by territorial.io
MIT License - Feel free to use and modify for your own projects!
Enjoy conquering territories! 🎮🗺️