OddThinking

A blog for odd things and odd thoughts.

Battleship Strategy

This is a quick discussion on a puzzle, because I have an idea buzzing in my head, and I need to get rid of it so I can focus back on a task which I would prefer to finish.

Once, in spite of the jeering of my fellow students, I chose a topic for a post-graduate university assignment of “Electronic Battleships”. It used the game of Battleships as a metaphor for securing online transactions, and worked on approaches to prevent cheating. (When you are playing against an untrusted opponent with a computer, what is to stop them from moving their ships around after they find out where you fired?)

I wrote a prototype that never got finished. It was my first C++ project, and my code was awful. I threw together an essay to cover for the lack of running code. It, too, was awful, which I found out when I gave it to a friend to critique, and she ripped it to shreds. That gave me a few hours of self-pity, until I realised she was absolutely right. I rearranged my schedule, re-wrote the essay, added some actual insight into the problem, and got an A for the assignment, which I largely owe to my friend not pulling any punches.

So, Battleships holds a special place in my heart.

I have often considered writing a Battleship-playing AI, but I never have.

Now, I discover, there is a Battleship-playing AI competition, which I don’t have the time (or .NET experience) to enter.

However, I can’t focus on my real task because I am having ideas about what my AI, if such a thing existed, would do.

So here are my expectations about what you would see in a winning strategy.

Aiming

After you score a hit, you should circle around the hit find the rest of the ship and sink it. This is bloody obvious! However, I have played against another adult (on a long flight) who didn’t do this. Needless to say, she didn’t do well.

Naive statement: There is no need to bomb the even positions. You can merely bomb the odd ones (e.g. the black-squares if you were playing on a chequer-board.) That ensures that you will get even the smallest submarine, while covering the area more efficiently. (You generally gain less information from bombing a square next to a square that is already a miss, than another square.)

Of course, if you choose this algorithm, be sure to randomly swap between even and odd cells between games or the other player may learn from you and ensure most of their ships lay in even positions.

Slightly less naive statement: The submarine’s length of two, means you need to bomb every second square. Once the submarine is sunk, the distance between squares can increase to the length of the smallest remaining ship.

More intelligent approach: While still sticking to only odd squares, you should start with more distance between them (e.g. 5 cells), and reduce that the 4, 3 or 2 cells only once those positions have all been filled. If you are lucky enough to sink the submarine in the first pass, you may never need to cover the grid that closely.

A certain amount of randomness is desirable to avoid predictability in future games. Don’t simply sweep from left-to-right, top-to-bottom, or edges to centre.

A hit on an edge square (or one nearby an edge) is better than a hit in the center, because it is tells you more about the possible position of the ship – there are less directions the ship may extend. However, there are less ship positions that lie on the edge for the same reason, reducing the chance of hitting a ship on the edge.

The general rule is that the value of a cell bombing is proportional to the chance of a hit succeeding at that cell, and proportional to the value of a hit.

The chance of a hit is proportional to the number of combinations of ship that could lie on that cell. This is reduced on an edge.

The value of a hit is higher for an edge, because it takes less attempts, on average, to sink a ship discovered on an edge.

Any strategy that leads you to prefer the edge risks that the other player will know that and avoid the edge. Or know you know that, and prefer the edge! That is why randomness is key to prevent particular strategies from being easily perceived.

So, you don’t always shoot at the highest valued target, but you bomb targets randomly, with a probability distribution in proportion to their value.

As a teenager, I was falsely accused of cheating by my brother after a long game of Battleships. It was getting to the point where there weren’t many places on the board he could be, so my run of luck wasn’t as outrageous as he felt it was.

Monitor the opponent’s placement strategy as the games progress. Record the number of ships on odd-cells versus even cells. Record the distribution of distances from the edge. Use that to modify your probability distribution to match.

Placement

Don’t place your ships so that they are touching (straight on, or at right angles). If the other player hits one ship, and starts circling around it to find its direction and extent, there is a good chance they will discover the second ship. (Perhaps this rule should be to only do this rarely, so the other player can’t take advantage of the fact you predictably play this way, but this is such a strong rule I suspect it isn’t worth ever breaking.)

Consider the same issues with the discoverability of ships on the edge, to determine your placement. Again, use randomness across a probability distribution to avoid detectability.

Monitor the opponent’s aiming strategy as the games progress. Record the number of attacks on odd-cells versus even cells – perhaps watching to see if it changes over time. Record the distribution of distances from the edge; again seeing if it changes over time. Use that to modify your probability distribution of placement, avoiding the areas the other player attacks first.

Conclusion

It is a shame I don’t have the time to enter this competition; writing real-time AIs is not something I have tried before. I am interested in what strategies people come up with. I expeect to see elements of everything I have suggested here to be included, and I expect a lot, lot more.


Comments

  1. Theoretically, when you are losing a game, it makes sense to take bigger risks – in some mythical game, conservative play may guarantee you lose by 5 points, while high-risk play may give you a 10% chance of winning by a point, and 90% chance of losing by 10 points. In a winner-takes-all situation the high risk approach is more rational.

    I’ve looked but cannot see how one could take high-risk/high-return shots in Battleships. The optimal strategy doesn’t seem to change if you are losing.

    I’d be interested if I was proven wrong here; that the opponent’s aiming strategy could affect your own aiming in this game (as opposed to your placement strategy in the next game.)

  2. I too was accused of cheating at Battleship. From memory, it was in primary school and my opponent chose to place a ship in each corner. Once I stumbled onto two ships I guessed his pattern and quickly got two more ships. When I hit his fourth he wondered how I “knew”. The fifth ship was in the exact middle.

    I used to try putting the submarine at right angles to a long ship. My theory was that an opponent might hit the large ship once, then the submarine in circling that point. They’d continue in that direction and when I’d say “hit and sunk” they’d assume they’d sunk a ship of length three1. The resulting confusion was supposed to give me an edge. On reflection it probably doesn’t. However having two adjacent ships can throw opponents for a little while, but probably not enough to risk it, because if they quickly figure it out, you’re down two ships in quick succession.

    1This relied on a literal interpretation of the rules that required you only to say “hit” or “hit and sunk”. That is, you don’t need to say what ship(s) you’ve hit.

  3. There are three variants of the rules.

    1) Hit or miss. This is how I played as a kid.

    2) Hit, sunk or miss. This is how the competition is played.

    3) Hit my battleship (frigate, etc.) or miss.

    Oh my! You’ve just made me realise I’ve done even more Battleship programming.

    No, I am not talking about Solitaire Battleships, or even my contributions to real-live ship systems.

    There was a Commodore 64 game called “Baudleships” which played Battleships over a modem. That’s right; your phone line was tied up while you played a game of battleships over a high-tech 300-baud modem.

    I made two modifications. One was to change the rules from the third item to the first. The other was to enable the delete key to work in the primitive, character-by-character, only-last-40-characters-visible online chat.

  4. I’ll chime in and say that I wrote a Battleship AI as a high-school science fair project. I think it was one of my early forays into Pascal (Turbo Pascal on the PC, naturally). It wasn’t very advanced, but it did at least try to sink any ship it hit, so perhaps it would have been a suitable challenge for extremely naive players on long flights.

  5. I have been desperately trying to improve my battleship skills in hopes of beating my friend Chris who makes me say “Oh Chris so pretty I want to give him a kiss” for a day after I lose. I have yet to beat him and so badly want him to have to say “David the king of battleship, next to him I look like ….” I have been battling this ai here which is supposed to be the hardest that I could find, but I need something harder. Where can I play against real people online for free?

    http://yovia.com/blogs/tabletop/2010/01/27/play-battleship-online/

Leave a comment

You must be logged in to post a comment.

Web Mentions

  1. Iterating Battleship « Rares's CGS blog