raymondoyondi/Multiplayer-Battleship-Game-Server
A high-performance, concurrent multiplayer Battleship game server built with Go (Golang) and WebSockets, implementing classic game rules with modern networking architecture. This project serves as a robust backend for a real-time, turn-based gaming experience.
โ Multiplayer Battleship Game Server
A high-performance, concurrent backend for the classic Battleship game, engineered with Go (Golang) and WebSockets. This server manages real-time game state, player matchmaking, and turn-based logic with a focus on low latency and thread-safe scalability.
๐ Features
- Real-time Communication: Bi-directional data flow via WebSockets for instantaneous move updates.
- Concurrent Session Management: Efficiently handles multiple simultaneous game rooms using Go's lightweight goroutines.
- Robust Game Logic: Full implementation of Battleship rules, including ship placement validation, hit/miss tracking, and win conditions.
- Event-Driven Architecture: Uses a central hub to broadcast game states and manage player disconnections gracefully.
๐ Tech Stack
- Language: Go (Golang) 1.21+
- Networking: Gorilla WebSocket
- State Management: Channels & Mutexes for thread-safe memory operations.
๐ System Architecture
The server operates on a Hub-Client model. The Hub manages the lifecycle of game rooms, while each client represents a unique socket connection.
Key Components:
- The Hub: The central "brain" that registers/unregisters players and routes messages to the correct game session.
- The Room: Encapsulates the state of a single match between two players.
- The Player: Maintains the private game board and ship coordinates, ensuring the opponent cannot "see" ship locations via the API.
๐ฆ Getting Started
Prerequisites
- Backend: Go (Gorilla WebSocket, gRPC)
- Networking: WebSockets for real-time updates
- Database (Optional): Redis/PostgreSQL for persistence (if needed)
- Containerization: Docker (for easy deployment)
Installation & Run
-
Clone the repository:
git clone [https://github.com/raymondoyondi/Multiplayer-Battleship-Game-Server.git](https://github.com/raymondoyondi/Multiplayer-Battleship-Game-Server.git) cd Multiplayer-Battleship-Game-Server -
Install Dependencies for both client and sever
go mod tidy
-
Run the server
cd game-severs make run -
Run the client
cd game-clientsthen
make installrun the command to connect to the game server
game-client connect
โ๏ธ How It Works
- Players Connect via WebSockets โ Clients connect to the server via
ws://localhost:8000/ws. - Matchmaking System โ Players are matched with opponents automatically.
- Game Board Initialization โ Each player places ships before the game starts.
- Turn-Based Attacks โ Players take turns attacking a grid position.
- Game State Updates โ The server broadcasts updates to both players.
- Win Condition โ The first player to sink all opponent ships wins.
๐ค Contributing
- Fork the repository
- Create a feature branch
git checkout -b feature-name
- Commit your changes
git commit -m "Add new feature" - Push to GitHub
git push origin feature-name
- Create a Pull Request
๐ License
This project is licensed under the MIT License.