{"id":1050,"date":"2009-10-29T02:03:01","date_gmt":"2009-10-28T16:03:01","guid":{"rendered":"http:\/\/www.somethinkodd.com\/oddthinking\/?p=1050"},"modified":"2009-10-29T02:03:01","modified_gmt":"2009-10-28T16:03:01","slug":"battleship-strategy","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2009\/10\/29\/battleship-strategy\/","title":{"rendered":"Battleship Strategy"},"content":{"rendered":"<p>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.<\/p>\n<p>Once, in spite of the jeering of my fellow students, I chose a topic for a post-graduate university assignment of &#8220;Electronic Battleships&#8221;. 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?)<\/p>\n<p>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.<\/p>\n<p>So, Battleships holds a special place in my heart.<\/p>\n<p>I have often considered writing a Battleship-playing AI, but I never have.<\/p>\n<p>Now, I discover, there is a <a href=\"http:\/\/stackoverflow.com\/questions\/1631414\/what-is-the-best-battleship-ai\">Battleship-playing AI competition<\/a>, which I don&#8217;t have the time (or .NET experience) to enter.<\/p>\n<p>However, I can&#8217;t focus on my real task because I am having ideas about what my AI, if such a thing existed, would do.<\/p>\n<p>So here are my expectations about what you would see in a winning strategy.<\/p>\n<h3>Aiming<\/h3>\n<p>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&#8217;t do this. Needless to say, she didn&#8217;t do well.<\/p>\n<p>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.)<\/p>\n<p>Of course, if you choose this algorithm, be sure to randomly swap between even and odd cells <em>between games<\/em> or the other player may learn from you and ensure most of their ships lay in even positions.<\/p>\n<p>Slightly less naive statement: The submarine&#8217;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.<\/p>\n<p>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.<\/p>\n<p>A certain amount of randomness is desirable to avoid predictability in future games. Don&#8217;t simply sweep from left-to-right, top-to-bottom, or edges to centre.<\/p>\n<p>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 &#8211; 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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>So, you don&#8217;t always shoot at the highest valued target, but you bomb targets randomly, with a probability distribution in proportion to their value.<\/p>\n<div class=\"aside\">As a teenager, I was falsely accused of cheating by my brother after a <em>long<\/em> game of Battleships. It was getting to the point where there weren&#8217;t many places on the board he could be, so my run of luck wasn&#8217;t as outrageous as he felt it was.<\/div>\n<p>Monitor the opponent&#8217;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.<\/p>\n<h3>Placement<\/h3>\n<p>Don&#8217;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&#8217;t take advantage of the fact you predictably play this way, but this is such a strong rule I suspect it isn&#8217;t worth ever breaking.)<\/p>\n<p>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.<\/p>\n<p>Monitor the opponent&#8217;s aiming strategy as the games progress. Record the number of attacks on odd-cells versus even cells &#8211; 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.<\/p>\n<h3>Conclusion<\/h3>\n<p>It is a shame I don&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have often considered writing a Battleship-playing AI, but I never have.<\/p>\n<p>Now, I discover, there is a <a href=\"http:\/\/stackoverflow.com\/questions\/1631414\/what-is-the-best-battleship-ai\">Battleship-playing AI competition<\/a>, which I don&#8217;t have the time (or .NET experience) to enter.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[31,33],"tags":[141,325,91,90,69],"class_list":["post-1050","post","type-post","status-publish","format-standard","hentry","category-geek","category-puzzle-solving","tag-computer-science","tag-games","tag-puzzles","tag-security","tag-software"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/1050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=1050"}],"version-history":[{"count":2,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"predecessor-version":[{"id":1052,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/1050\/revisions\/1052"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}