Our website uses cookies to ensure the quality of the services you provide. By continuing to browse, you agree to our cookie policy. Learn more

Battleship game

Date: 2020-03-30 Author: Vilimantas Bernotaitis


If you are looking for a fun event for programmers at work or school I have an idea to suggest. I would challenge you to play battleship, but instead of shooting by yourself, create a program that would make moves for you and play it against your opponent. It is fun to watch how your program competes against others.

Battleship game rules

Most of you probably know the battleship game, but if you don’t try this one: http://en.battleship-game.org/. Anyway, I will remind some basic rules: 

  1. There should be 10 ships:
    1. 1 ship of size 4
    2. 2 ships of size 3
    3. 3 ships of size 2
    4. 4 ships of size 1
  2. Ships shouldn’t touch each other.
  3. After the shoot there can be three possible results:
    1. Miss
    2. Hit
    3. Ship is destroyed
  4. After hitting a ship you have another shot until you miss.

Here is an example of the ships placement:

Since this game will be between two programs, there should be some additional rules:

  1. If the program returns an exception or incorrect coordinates, it is a technical loss.
  2. If the program hits the same field the second time, the turn goes to the opponent even if the result is hit.

Tournament implementation

We already tried this game and there are two options for how it can be executed: using a physical board or using an app that simulates a game.

Physical board

Each player should create a console application that will return coordinates of all ships, shoot coordinates and will accept the status of the last shot.

When you have your applications you can simply execute your apps and just mark the state of the game on a paper or if you want more entertainment you can make boards as we did:

Since it was a tournament, we used a tournament table so that each player would play against each other. Most points collected person won the whole game.

Simulation application

Another way to play this game is to use another application that would simulate everything. We created an application that does that: https://github.com/vbernotaitis/battleship-board. It is required that each player would implement a specific interface and compile it to a dll: 

public interface IBattleShipShooter
    /// <summary>
    /// Name of the captain
    /// </summary>
    string CaptainName { get; set; }

    /// <summary>
    /// Generates all ships with their coordinates.
    /// Fleet should consist of 10 ships: 1 of size 4, 2 of size 3, 3 of size 2, 4 of size 1.
    /// </summary>
    /// <returns>Fleet of ships</returns>
    Ship[] PrepareShipsForNewBattle();

    /// <summary>
    /// Shoots to the oponents fleet by the determined coordinates.
    /// </summary>
    /// <returns>Coordinates of the shot</returns>
    Coordinates Shoot();

    /// <summary>
    /// Reports result of the last shot.
    /// </summary>
    /// <param name="coordinates">Coordinates of the last shot</param>
    /// <param name="result">Shot result</param>
    void ReportLastShotResult(Coordinates coordinates, ShotResult result);

    /// <summary>
    /// Reports result of the last oponent's shot.
    /// </summary>
    /// <param name="coordinates">Coordinates of the oponent's last shot</param>
    /// <param name="result">Shot result</param>
    void ReportOponentsLastShotResult(Coordinates coordinates, ShotResult result);


When you want to simulate a game, it is needed to add dlls to an application, register the implementation classes in the Startup.cs and start the application. 

public void ConfigureServices(IServiceCollection services)
    services.AddTransient(x =>
        new BattleShipGame(
            new BattleShipShooterTracker(typeof(BattleShipShooter), “Player 1”),
            new BattleShipShooterTracker(typeof(BattleShipShooter), "Player 2"))

It will have a user interface where you will see every step of the game or you will be able to run it instantly and find out who is the winner:

Our experience

From our experience, it was a really fun event. When we played this game, we set a time limit for players to write their shooters, approximately 3 hours. It was interesting that some players developed a good strategy but forgot about exception handling, while others created poor shooting but without errors which led them to win. Surprisingly, the best strategy does not always win.

I hope you will accept my challenge to write your shooter!



About the author
Vilimantas Bernotaitis .NET Developer

Other services

.NET Software Development

Read more >

.NET Specialist Sourcing and Recruitment

Read more >

.NET Team Extension

Read more >

.NET consulting

Read more >

Get a quote

Tell us about your challenge.

Get a quote

Tell us about your challenge.
Company (optional)
Phone number
Thank you for your message!