Battleship is a strategy type guessing game for two players. It is played on ruled grids (paper or board) on which each player’s fleet of warships are marked. The locations of the fleets are concealed from the other player. Players alternate turns calling “shots” at the other player’s ships, and the objective of the game is to destroy the opposing player’s fleet. Ships cannot touch each other horizontally, vertically or diagonally.
Writing a game from scratch, including agents, seems like a pretty daunting task. Nevertheless we could start with a tiniest prototype implementation ever possible, simplifying everything that is not necessary at the moment.
Any tile in a grid could consist any ship part:
But from an oppononent point of view, we should include some information whether a damaged ship is sinking after shooting:
Players need distinguish boards to arrange their ships and track opponent ones. For the sake of simplicity we start with one dimensional area:
We represent all ships as lists of units:
Before players arrange their boards, they both should know a set of available ships.
Let’s say we have one 2-sized and one 3-sized ships:
There is not so much we can do with it except print it out to console.
Printing a title is straightforward, we just need to specify exast datastructure behind a string literal:
Printing out ships is more tricky, we need first to traverse a list of ships, then ships themselves, making a space break after each one:
Finally, putting it all together:
Here is the result:
Having all this in mind, how the bot should understand where to hit? By calculating probabilities.
Full source code is available here.