Welcome to the Battleship Game Backend! This repository contains a self-hosted backend server written in Go for the classic Battleship game.
- Multiplayer Support: Play the classic Battleship game with your friends.
- Websocket API: Easy-to-use API for interacting with the game server.
- Game State Persistence: Ability to continue playing within 2-minute period of grace period (abnormal closure).
- Real-Time Gameplay: Smooth, responsive gameplay with real-time updates.
- Docker Support: Easily deploy the server using Docker.
| Waiting Room | Choose Difficulty | Place Ships | Game Play I |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
| Game Play II | End Match | Rematch | |
|---|---|---|---|
![]() |
![]() |
![]() |
- Go (version 1.22 or later)
- Docker (optional, for containerized deployment)
git clone https://github.com/yourusername/battleship-backend.git
cd battleship-backendEnsure you have Go installed on your machine. Then, you can build the server with:
go build -o battleship_server cmd/main.goAfter building, you can start the server with:
./battleship-serverConnect to the server with:
websocat ws://127.0.0.1:1313/battleshipUpon a successful websocket connection, you should receive a request as below:
{"code":0,"payload":{"session_id":"MjhkOTUzNWEtYjYxNC00MjM1LTk2YTgtZTRmMWEyYWNlYjIz"}}where session_id is your unique id used for reconnection to the server upon abnormal closure.
To know what each code represent in this api, refer to models/connection/signal.go. Through
using the correct code, you can then create a game, select a grid, and attack the opponent.
In case of abnormal closure and wanting to reconnect to resume the game:
# note that session came from above
websocat ws://127.0.0.1:1313/battleship\?sessionID=MjhkOTUzNWEtYjYxNC00MjM1LTk2YTgtZTRmMWEyYWNlYjIzFor a smooth experience of gaming, a frontend is required which you can find here:
You can configure the server using the following environment variables:
STAGE: Represents the stage of development. Choice of dev or prod (refer to models/server/stage.go)
PORT: The port on which the server will run (default is 8080).
DATABASE_URL: The connection string for the database (if applicable).
To test the API, you can use Makefile templates:
# Equivalent to `go test -v ./test -count=1` // count=1 bypasses the cached info
make testThis project is licensed under the MIT License. See the LICENSE file for more details.
For any questions or inquiries, feel free to reach out to saeidalz96@gmail.com or open an issue on GitHub.






