A fast, real-time Connect 4 game built with Go and WebSockets. Play against other players online or challenge the bot.
- Real-Time Multiplayer - Play with anyone, anywhere via WebSockets
- AI Opponent - Minimax bot that thinks 6 moves ahead
- Quick Matchmaking - Find a match in 10 seconds or play the bot
- Reconnection Support - 30-second grace period if you disconnect
- Leaderboard - Track your wins and climb the rankings
- Clean UI - Dark theme with smooth animations
git clone https://github.com/prokriti11/connect4-multiplayer.git
cd connect4-multiplayer
go mod download
go run ./cmd/serverOpen http://localhost:8080 and start playing!
docker build -t connect4 .
docker run -p 8080:8080 connect4export DATABASE_URL="postgres://user:pass@localhost:5432/connect4?sslmode=disable"
go run ./cmd/serverConnect4/ ├── cmd/ │ └── server/ # Main entry point ├── internal/ │ ├── game/ # Game logic and AI │ ├── websocket/ # WebSocket handler │ ├── matchmaking/ # Player queue system │ ├── state/ # Game state manager │ ├── storage/ # PostgreSQL integration │ ├── leaderboard/ # Stats tracking │ └── analytics/ # Event logging ├── frontend/ # HTML/CSS/JS UI ├── config/ # Server config └── Dockerfile # Container build
| Variable | Default | Description |
|---|---|---|
PORT |
8080 | Server port |
DATABASE_URL |
- | PostgreSQL connection |
MATCH_TIMEOUT |
10 | Seconds before bot match |
RECONNECT_WINDOW |
30 | Reconnection time limit |
KAFKA_BROKERS |
- | Event streaming (optional) |
- Enter your username
- Click "Play Now" to join matchmaking
- Get matched with a player or the bot in 10 seconds
- Click columns to drop your pieces
- Connect four in a row to win!
GET /- Game interfaceGET /leaderboard- Top 10 players (JSON)GET /health- Server statusWebSocket /ws- Game connection
Join matchmaking:
{"type": "join_queue", "payload": {"username": "Player1"}}Make a move:
{"type": "move", "payload": {"game_id": "abc123", "column": 3}}Game started:
{
"type": "game_start",
"payload": {
"game_id": "abc123",
"opponent_name": "Player2",
"is_bot": false,
"symbol": 1,
"your_turn": true
}
}Board update:
{
"type": "game_state",
"payload": {
"board": [[0,0,0,...], ...],
"turn": 1,
"winner": 0,
"status": "active"
}
}The AI uses Minimax with alpha-beta pruning:
- Check if it can win this turn → take it
- Check if opponent can win → block them
- Search 6 moves deep to find the best move
- Prefer center columns (stronger position)
- Evaluate threats and opportunities
Position scoring:
- Three in a row with space: +100 points
- Two in a row with spaces: +10 points
- Center columns: bonus points
Performance:
- Average decision time: 50-100ms
- Search depth: 6 moves
This project includes render.yaml for one-click deployment:
- Fork this repository
- Go to Render
- Click "New" → "Blueprint"
- Connect your GitHub repo
- Click "Apply"
npm install -g railway
railway login
railway init
railway upfly launch
fly deployBackend:
- Go 1.21+ (concurrency, performance)
- Gorilla WebSocket (real-time communication)
- PostgreSQL (persistent storage)
- Docker (containerization)
Frontend:
- Vanilla JavaScript (ES6+)
- WebSocket API
- CSS3 (animations, dark theme)
- Responsive design
Algorithms:
- Minimax with alpha-beta pruning (depth 6)
- Position evaluation and pattern recognition
- Bot Response: 50-100ms per move
- WebSocket Latency: <10ms on local networks
- Matchmaking: 10-second timeout
- Reconnection Window: 30 seconds
- Memory: ~15MB per 1,000 active games
- Tournament mode with brackets
- ELO rating system
- Game replay functionality
- Mobile app
- Spectator mode
- In-game chat
- Multiple board sizes
- Custom game rules
Connect 4 is a simple game, but building it right is surprisingly complex. This project tackles real problems: how do you keep two players in sync across the internet? How do you handle someone's WiFi cutting out mid-game? How do you make an AI that's challenging but not frustrating?
The answers involve WebSockets, careful state management, and a chess-like algorithm called Minimax. But more than that, this project is about taking something familiar and making it work flawlessly in the real world. It's production-grade code that handles edge cases, recovers from failures, and scales gracefully.
Whether you're here to play, learn from the code, or build something similar, I hope this shows that even classic games can teach us something new about software architecture, concurrency, and real-time systems.
Thanks for checking it out. Now go connect four. 🎮
Made by Kriti Porwal