Skip to main content
16-Pearl
October 31, 2025
Question

Mathcad Community Challenge November 2025 - You Sank My Battleship!

  • October 31, 2025
  • 10 replies
  • 5877 views

Recently, I was in the position of trying to explain to a 6- and 8- year old how to play the board game Battleship. (Kids from my generation had commercials that seemed to explain the basics of the game fairly well.) While trying to explain the concept of the grid to them and search algorithms, I couldn’t help but wonder, “How could this be done in Mathcad Prime?”

 

BB61_USS_Iowa_BB61_broadside_USN.jpg

 

This month’s challenge, should you choose to accept it, is fairly open-ended: simulate various aspects of the Battleship game in Mathcad. Here are some suggestions:

 

  • Write a program that will place the ships on the ocean grid. The ships must be placed horizontally or vertically; no diagonals. Ships can’t overlap one another. Do you place the ships randomly or according to some kind of optimized algorithm?

  • Alternatively, can you use Advanced Input Controls to allow the user to place the ships on the ocean grid?

  • Display the placement of the ships using the plotting capability.

  • Write an algorithm for hunting the ships. Do you use something like a random walk? A standard grid search? Your own preferred algorithm from when you were a kid?
  • Alternatively, use Advanced Input Controls to let the user enter their own coordinates. Use these same controls to provide feedback to the user on hit or miss. (Note: David Newman and I discussed whether you could use Advanced Input Controls for the user to enter guesses and have Mathcad keep track of previous shots. David said that it doesn't work like that. I thought you could append new guesses to an existing matrix; if that doesn't work, I think the brain power we have in the community can figure out a way. That have surprised me before.)
  • Display the hits and misses on a plot or chart.

  • Can you program Mathcad Prime to run through a number of iterations of placing and hunting ships, and then evaluate statistics on number of shots needed to sink the fleet?

Again, these are just suggestions. Feel free to create a worksheet that tackles any aspects of the classic board game. Do as little or as much as you like. Also, your worksheet should stand on its own; a stranger should be able to open it and understand what it’s for.

For the sake of this challenge, we will use the following ships:

  • Aircraft carrier 5
  • Battleship 4
  • Cruiser 3
  • Submarine 3
  • Destroyer 2

It’s a kids game, so have fun with this!

 

Find the Mathcad Community Challenge Guidelines here!

 

 

10 replies

18-Opal
October 31, 2025

Just wanna say that if the Community can collectively figure out the "keep track of previous shots without overwriting the history" problem in Mathcad, then the people contributing to that will be immortalised in Mathcad history as super-geniuses. 'cause I spent a lot of time trying to figure that out for the Mathcad Game Gallery, to no avail.

23-Emerald V
November 1, 2025

@DJNewman wrote:

Just wanna say that if the Community can collectively figure out the "keep track of previous shots without overwriting the history" problem in Mathcad, then the people contributing to that will be immortalised in Mathcad history as super-geniuses. 'cause I spent a lot of time trying to figure that out for the Mathcad Game Gallery, to no avail.


Could you please further explain what you mean, Dave, preferably with an example?

 

Stuart 

16-Pearl
November 1, 2025

David and I were talking about having a person be able to play Battleship - call their guesses - using Advanced Input Controls.

For example, let's say I use a couple drop-downs to select B5. 

I am informed it's a miss.

I call out H2. It's a miss.

I call out J9. It's a hit.

 

Try to do that in Mathcad Prime. That's what David and I were talking about.

 

19-Tanzanite
October 31, 2025

I take it the number against a ship type  refers to the number of grid squares covered by that ship, not to the number of ships of that type.

 

Do we also assume the standard Battleship battle zone grid to be of size 10x10?   

18-Opal
October 31, 2025

@AlanStevens wrote:

I take it the number against a ship type  refers to the number of grid squares covered by that ship, not to the number of ships of that type.


Correct. One ship per type.

 


@AlanStevens wrote:

Do we also assume the standard Battleship battle zone grid to be of size 10x10?   


Good assumption.

19-Tanzanite
November 1, 2025

Here’s a simple starter (using Prime 11 Express).  It’s a deterministic, two-player game.  The figures below show part of a game, in which 10 shots have been taken.  The attached file contains the starting position.

I’ll think further about random start positions and multiple automatic games. 

(In fact, in the past I’ve considered a Monte-Carlo approach to multiple games, using a couple of different strategies. I wrote about it in the following:  STEM Problems with Mathcad and Python, V. Ochkov, A Stevens, A. Tikhonov, CRC Press, 2022, pp 212-219)

 

Bship1.pngBship2.png

Bship3.png

 

Oops! I forgot to show the default starting position above.  Here it is:

Bship4.png

Alan

19-Tanzanite
November 3, 2025

Here's my second version. It's basically the same as the first except the ship positions and the shots are entered using the more 'normal' Battleship letter-number notation.  This makes the game look aesthetically less pleasing to my eye (at least, it does so in the way I've implemented it!)!

 

Alan

(Prime 11 Express).

 

 

19-Tanzanite
November 4, 2025

Here is my third (and probably final) contribution.  It's similar to my first worksheet, but now the computer sets the position of the ships.  This is done in a 'brute force and ignorance' manner (that doesn't deserve to be referred to as an algorithm!), and may give rise to overlapping ship parts, requiring the sheet to be recalculated a few times.  A message is displayed if this is the case.

 

There's no way I'm going to attempt a computerised Monte-Carlo approach to testing different solution strategies with Prime Express!  This shouldn't be too difficult with the full version of Prime though.

 

Alan

(Prime 11 Express used)  

18-Opal
November 4, 2025

Just to be clear, the way to start / reset a new game is just to close the worksheet (DON'T SAVE), and reopen it?

Edit: Oh, no, you have a blank resetter in the Utilities hidden area.

Edit 2: I'm pretty sure I'm bad at Battleship.

DJNewman_0-1762273351233.png


Anyway, I know I just wrote earlier in this thread that interactivity isn't required, buuuut if people will try to implement interactivity within Mathcad's confines, you bet I'm gonna be playing people's worksheets on the clock.

 

(Maybe these would be decent videos for the YouTube channel!)

23-Emerald V
November 5, 2025

I was thinking about the "reality" of the situation, and musing that it should really be called something more akin to "Ships of the Line in Fog".  You're firing blind into a fog bank where you know the enemy is lurking, and expecting the enemy to play nicely and report when you've hit them, with an opposing Ship's captain eventually reporting, "You have deaded me, you filthy rotten swine".

 

Perhaps, I further mused, we should add an extra, slight degree of realism into the affair.

 

Gunnery wasn't too accurate until WW2, and although you might have thought that you were aiming at point (x,y), the chances were that your shells were landing at (x+dx, y+dy) instead. So, maybe we could "improve" Battleships by reporting a hit with only a certain probability of success, even if a ship occupies a square?  This would better align with actual performance (#), even when you could see an enemy ship, and were using dive bombing, torpedoes, or cannons as the weapon of choice.  A question might be whether to perturb the shot into an adjacent square or simply miss.

 

Stuart

 

(#) Even Warspite and Scharnhorst could miss, despite some excellent long-range gunnery.

 

(And there were several instances of opposing fleets mixing to some degree, with the occasional blue-on-blue ... but that's possibly taking improvement a bit too far)

19-Tanzanite
November 5, 2025

Ok, this is definitely my final contribution to this challenge!  This one has a human setting the ship positions and the computer firing the shots.  It uses a really terrible random shooting algorithm (going part way to addressing Stuart's point about shooting into a fog!).

 

Alan

(again, Prime 11 Express)

18-Opal
November 5, 2025

"Player A then collapses the Ship positions area, so that player B cannot see where the ships are."

 

Well, Player B is the computer in this case, so... it doesn't matter if I collapse it or not.

 

DJNewman_0-1762358785200.png

 

(Though suddenly I'm feeling pretty good about my 54 shots.)

19-Tanzanite
November 5, 2025

If you recalculate a few times you will get some smaller numbers than 100.  In fact, the mean value for the number of shots can be shown theoretically to be about 95.4 for this particular algorithm.  There are of course much better algorithms!

 

Alan

 

25-Diamond I
November 7, 2025

In the version of the game I posted a few days ago was an error in the function which places a ship in the game board which prevented ships from using part of the upper half of the board and the rightmost column.

Since I am therefore forced to provide an error-corrected version, I took the opportunity to implement an additional “verbose” mode.
When this is activated, the program also provides information about which ship was hit when a hit occurs.

Werner_E_0-1762498046289.png

Furthermore if a ship is sunk the appropriate info is also displayed and additionally the ship is framed with an orange border.

Werner_E_1-1762498189496.png

The cheat mode also shows borders around the ship if verbose mode is on:

Werner_E_2-1762498364566.png

 

I wished I would have planned the verbose mode right from the start as I sure would have written the sheet in a different way and probably would have turned more calculations into functions. But, alas, that's just how things are...

 

EDIT: I made a minor change and therefore replaced the file. In verbose mode, the ship overview now also shows how many parts of a ship have already been hit. However, the display is not an indicator of which parts have been hit.

Werner_E_2-1762541372629.png

 

Prime 11 file attached

EDIT (20251111): Just noticed that in the attached file the playing field was slightly compressed vertically and did not match the vertical slider. So a new file was attached (no other changes made).

 

18-Opal
November 10, 2025

mathcad-community-challenge-november-2025-werner-43-shots.png

yay

25-Diamond I
November 10, 2025

I guess that 43 shots with verbose (and of course cheat) set to off is pretty good!

However, I don't have any comparative values of my own. I have to admit that I found using two sliders and an additional button too cumbersome to play through the game completely.

23-Emerald V
November 7, 2025

@DaveMartin wrote:

For the sake of this challenge, we will use the following ships:

  • Aircraft carrier 5
  • Battleship 4
  • Cruiser 3
  • Submarine 3
  • Cruiser 2

Dave, can I just confirm - is that list of ships definitive, ie one and only one of each 'ship'?  Or would it be fair to say, 

 

6b64f77f964a559512daaf65b4340e28

 

Edited to add:

 

It's just that I was thinking of adding a little historicity to the proceedings.

 

2025 11 07 B.png

 

Clearly not totally accurate.  For example, Campbeltown took the St Nazaire docks up with her during Operation Chariot, and I don't think Yamato is due for her afterlife upgrade for another 174 years. 

 

Stuart

16-Pearl
November 7, 2025

I think of the challenges more like Dr. Peter Venkman in Ghostbusters: it's more of a guideline than a rule.

 

They are more like loose parameters. As we have seen many times before, people explore tangents that they find interesting. And I think that's great. It's not about coming up with a specific answer; it's more in the way that people use the tools in Mathcad Prime to approach different problems.

 

18-Opal
November 13, 2025

Realising that you called the two-piece a "Cruiser" and not "Destroyer" in the opening post; edited that to fix it. That's probably why I couldn't remember the names of the pieces while I was recording me playing these. 😞

 

Also yeah let's extend the submissions to December 13 as a birthday gift to me. 🙂

 

(There won't be a December 2025 Mathcad Minute anyway, our target for the write-up being published is late December [hardly likely] or early January 2026)

23-Emerald V
November 13, 2025

Happy Birthday in advance, and here's a little birthday present  ...

 

2025 11 13 A.png.

 

Although I'm not sure it's a very good birthday present, installing your namesake ship as something that somebody will be hoping to pound into scrap.  Still, you're in good company.

 

Stuart

 

(Hopefully, that will give me time to complete my game proposal - it seems as if I'm enjoying a recurrence of long-term, intermittent dreaded lurgi, which is slowing things down a bit)

 

https://en.wikipedia.org/wiki/USS_Newman

18-Opal
December 9, 2025

...oh yeah, I guess this ends this week. December 13 seemed so far away...

 

Anyway, it'd be awesome to see last-minute entries / revisions / discussion.

18-Opal
January 9, 2026

Yay, the blog is published now, so I'll unfloat this: https://www.ptc.com/en/blogs/cad/mathcad/community-challenge-battleship-game

16-Pearl
January 9, 2026
Unfloat. I see what you did there.