top of page

BattleshipDB

A Battleship game in C# and .NET 10, with an ASP.NET Core API, a PostgreSQL database . Play online in the browser.

  • C# - .NET 10

  • ASP.NET Core minimal API

  • PostgreSQL + Entity Framework Core​​

  • Docker

  • xUnit + Testcontainers

  • Github Actions CI/CD

  • Layered Architecture

icon-park-outline--frigate.png

What it is

BattleshipDB is just like the classic Battleship game. You place your fleets, then take turns firing at the opponent's board, but with fog of war, meaning you don't know where they have placed their fleets. 

Architecture

For this project I split it into layers. They all point to Domain.

  • Domain: holds the rules and logic. Boards, ships, shots, the enemy shooting strategy algorithm, etc. 

  • Infrastructure: saves and loads games in PostgreSQL using Entity Framework Core.

  • Api: ​is an ASP.NET Core API. It takes a request, uses Infrastructure to load the game, lets the Domain play the move, and saves the result.

  • Web: is the game and UI.

Battleship Diagram.png

Testing

For the game rules i worked test-first. There are over 130 tests. The rules tests run in about a second, and the integration tests start the API against a throwaway PostgreSQL in Docker using Testcontainers. GitHub Actions runs them on every push, and the game only deploys when they pass.

Saving games

Every shot is saved as its own row, and a game is loaded by replaying its shots through the rules. If two requests fire at the same game at once, a unique index makes the second one fail with a 409 instead of saving a broken game.

Linus Ekberg

bottom of page