C / C++ / C#

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

Programming Challenge 13 - Best Battleships Algorithm

By David Bolton, About.com

Battleship Board
This programming Challenge ran until the end of June 2008 and was about sinking ships in the game Battleships in the minimum number of shots.

This takes place on a 10 x 10 grid with a sub (2 squares), destroyer (three squares), cruiser (4 squares), aircraft carrier (5 squares) and a 6 square battleship. 20 squares in total occupied out of the 100. Note that ships can be aligned horizontally, vertically and diagonally. The picture shows a typical layout.

  • S - Sub 2 Squares
  • D - Destroyer Three Squares
  • C - Cruiser. Four Squares
  • A - Aircraft Carrier - Five Squares
  • B - Battleship - Six Squares.
  • To win a game you have to correctly identify all 20 squares.

    This programming challenge requires you to write code that reads a file of boards, play each board and then outputs how many shots it took to sink all five ships on all boards. It goes without saying that although your program knows how where the ships are, the ai routine you write must not cheat.

    The input file is called boards.txt and contains 10,000 lines of 102 characters. Each line is exactly 100 characters long and has a . for an empty location and S, D, C, A or B plus a carriage return (0xd) and line feed (0xa) at the end of the line.

    This is what the file looks like:

    .......B.....S...B....S....B.........B.........B..
    C.DDD..B.A.C......A...C....A.....C..A........A....
    Except I've split it over two lines to fit in here.

    Download the Data file. This is a zip file about 123Kb in size.

    Your program must read the boards.txt file* and from each line create one grid of ships. Your AI routine should then play against this grid and keep track of how many shots it took to sink all the ships in all 10,000 boards.

    Note: (Thanks to William Fishburne who raised this). When you register a hit, your code can both identify the type of ship hit and whether this sank that ship. This was changed from the original specification in the challenge on June 22 2008.

    Output

    Please output the following text.

    Total Shots to sink all ships = 398,645
    Obviously the figure will be your total.

    *Note: The file supplied is for your testing. I will compile and run your program with another file with 10,000 other boards. You can assume that all 10,000 boards will be legitimate to the specification I've given. Each line will be exactly 100 chars long using only the characters .SDCAB plus a carriage return/line feed to separate each 100 char line. (ie 102 chars * 10,000).

    Results

    Thank you to all six who entered. Congratulations to first place Makis Tsintsikloglou from Greece who managed to just beat Chris Greth's score. Makis wasn't the fastest but it was the score (total number of shots) that decided. One entry sadly never completed and is still running...

    Congrats also to Chris Greth, Dennis Muhlestein and St0le for very fast programs.

    1. Chris Greth - C++ Score = 499573 Time(secs) = 0.1438715
    2. Makis Tsintsikloglou (Greece) - C# Score = 505059 Time(secs) = 5
    3. Dennis Muhlstein (USA) - C++ Score = 594706 Time(secs) = 0.742978
    4. St0le (India,20) - C++ Score = 650186 Time(secs) = 0.254
    5. Pedro Graca (Portugal) - C Score = 788276 Time(secs) = 0.854
    6. Michael Chock - C++ Score = 4522920 Time(secs) = 1497
    Please note. Due to an oversight over late received versions from Chris Greth and Pedro Graca I used earlier versions. To be fair to Makis who I'd put in first place, I'm now calling this a joint draw between him and Chris for 1st place. I've uploaded the latest versions which gave these times.

    All entries were compiled with Microsoft Visual C++ or C# 2008 and ran in release mode.

    More Programming Challenges

Explore C / C++ / C#

About.com Special Features

C / C++ / C#

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. Projects
  5. Programming Challenges
  6. Programming Challenge 13 - Best Battleships Algorithm

©2009 About.com, a part of The New York Times Company.

All rights reserved.