GitHunt
RA

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:

  1. The Hub: The central "brain" that registers/unregisters players and routes messages to the correct game session.
  2. The Room: Encapsulates the state of a single match between two players.
  3. 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

  1. 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
  2. Install Dependencies for both client and sever

    go mod tidy
  3. Run the server

    cd game-severs
    make run
    
  4. Run the client

    cd game-clients
    

    then

    make install
    

    run the command to connect to the game server

    game-client connect 
    

โš™๏ธ How It Works

  1. Players Connect via WebSockets โ€“ Clients connect to the server via ws://localhost:8000/ws.
  2. Matchmaking System โ€“ Players are matched with opponents automatically.
  3. Game Board Initialization โ€“ Each player places ships before the game starts.
  4. Turn-Based Attacks โ€“ Players take turns attacking a grid position.
  5. Game State Updates โ€“ The server broadcasts updates to both players.
  6. Win Condition โ€“ The first player to sink all opponent ships wins.

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature-name
  3. Commit your changes
    git commit -m "Add new feature"
  4. Push to GitHub
    git push origin feature-name
  5. Create a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License.