Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
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?”
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.
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:
It’s a kids game, so have fun with this!
Find the Mathcad Community Challenge Guidelines here!
I've just wasted some time working on the "idea":
a) Looking at an extension to my idea, which involved portraying a board with representative images of a carrier, battleship, cruiser, destroyer, frigate, submarine, etc. I'd got so far along before Mathcad stated that half the functions I wanted to use were Premium. Never mind.
b) After bypassing that little problem, I forgot that Mathcad Prime doesn't have a Plot component. Don't ask me how I forgot; it's not as if I haven't been asking for one since Prime 1.0.
c) I had an idea for finding empty regions of the board of sufficient size to place a ship of a given size and orientation. Unfortunately, I think I need cfft to do the job simply, but cfft is Premium; convolve isn't Premium but needs further thought to determine if it's worth the effort. Perhaps edge detection? That way, there should be gaps between ships built-in to the process. In the meantime, I've gone for brute force. Keep hitting the board until the moles don't pop up. C'est la vie.
I have, however, successfully tested the main core of the idea -- keeping track of hits on each ship and updating its status. (I've also disregarded the "one of each type" rule)
Brian and I were definitely fighting each other in our little game. It's just that when the Mad Scientists declare war, the villagers suffer once it goes nuclear. "Collateral damage", I think it's called. 😈
Stuart
Oh, and I'm also considering the merits of a wrap-around board.
And having ships move between each salvo.
@StuartBruff wrote:
Oh, and I'm also considering the merits of a wrap-around board.
And having ships move between each salvo.
Well, you've still got about three weeks left...
@Werner_E wrote:
@StuartBruff wrote:
Oh, and I'm also considering the merits of a wrap-around board.
And having ships move between each salvo.
Well, you've still got about three weeks left...
The way I'm feeling after yesterday's car-fixing shenanigans, I wouldn't be at all surprised.
I've decided against ships moving for the moment. I vaguely recall having a clock ticking away in the background for some pseudo-animation in MM11..MM15 but ISTR the process involved a scripted component (3D Plot), and I'd have liked to have the ships moving independently of a player's shots. I'm also not sure that it would be adding any particular value to the game itself.
A wrap around board, OTOH, would allow more flexibility in ship placement.
Stuart
If y'all want, I could also easily convince Dave Martin to extend this to a little bit further in December.
But there's also something to be said about the nature of deadlines and how that'll just get people to start a week or whatever later...
Hmm, at the moment, I think I'm done with the challenge. My comment was directed at Stuart in a not entirely serious way, because he comes up with such a wealth of different ideas for the game. It's really a pity that he is limited to Express 😞
I'm getting a little fed up with seeing squirrels popping into rabbit holes and diving down to see what they're up to.
No sooner had I posted the above than I looked at the ships and their "sizes" (as represented by their size vectors, which double up as damage locations) than I thought,
"Hmm, perhaps I should scale their sizes by their real-life lengths?"
& "Perhaps I could save some effort here by using their respective class lengths?".
& "So, should length be taken as gundeck length or total length, including bowsprits?"
& "Hmm, how much variation is there within a class?
& "How do I handle ships with no class? -- belay that thought, sailors! Meh, just make one up based on some characteristic or ship name.".
& "Should I include the relevant Wikipedia links?"
Now I'm wondering whether to further incorporate the idea.
Oh, and I really do not like strings necessarily including quote marks when displayed. I experienced a moment's confusion looking at the last "" in the FIF strings -- "Is "" a string delimiter or inches?", followed by, "And is the "-" a FIF delimiter or a negative sign? Why not use a space?".
All I wanted to do was copy Wiki ship lengths & paste/convert them into Mathcad. Is that so much to ask?
https://en.wikipedia.org/wiki/HMS_Victory
| Length |
|
|---|
I am manfully resisting the urge to write Yet Another FIF.
I'm also manfully resisting the urge to mention the mixed number operator found in certain other versions of Mathcad -- but I can resist everything except temptation (and stealing quotations)
Stuart
You know me. I'm completely flexible. Besides, we haven't had a challenge with this much activity since... maybe ever?
OTOH, perhaps I could steal amend Luc's Text Plot worksheet to do what I want ... ?
I'd started to modify his worksheet to use Hershey fonts, then several squirrels passed by, then I got side-tracked into modifying it to print cuneiform,
𒐕𒐖𒐗𒐼𒐊𒐋𒑂𒑄𒑆𒌋𒎙𒌍
Then a few more squirrels passed by, and I've somehow got involved in sinking warships.
Stuart
( @LucMeekes )
Reminder that this thread is about the specific community challenge and not general discussion about the merits of legacy Mathcad vs Mathcad Prime.
Regarding interactivity, I agree (though I think I tried my best in the Game Gallery and even exceeded expectations!), hence the note in the opening post about how Dave and David disagree, though this Battleship challenge doesn't require interactivity in your worksheet!
@DJNewman wrote:
Reminder that this thread is about the specific community challenge and not general discussion about the merits of legacy Mathcad vs Mathcad Prime.
Regarding interactivity, I agree (though I think I tried my best in the Game Gallery and even exceeded expectations!), hence the note in the opening post about how Dave and David disagree, though this Battleship challenge doesn't require interactivity in your worksheet!
Actually the general discussion was more about the missing features in Prime, regardless if legacy Mathcad was better or not.
I tend to disagree that the current challenge would not require interactivity. At least not if the goal is to create a worksheet which is fun to use and not boring. For this I would expect to be able to click right at the field to place a shoot and get immediately the reaction if it was a miss or a hit and the field would automatically change color accordingly. After this I would either see the opponent (computers) guess or I would be able to make the next shoot if its just me against the computer. All this without having to scroll, input values to a variable or push any other button. I would call this interactivity.
I thinks its sure interesting to see what people can come up with and its sure a way to learn and maybe to overcome one limitation or another. But its not what Prime was made for and so Prime is missing a lot of necessary features (and BTW legacy Mathcad as well even though scripted components were more powerful). And as I already said I think that implementing such features may be beneficial for tasks Prime was made for as well. Especially if you think of creating worksheets which should be used by others not that well versed with using Prime.
@Werner_E wrote:
So I consider Prime not to be the tool to create a nice game. Its rather like asking "who is best in driving in a screw using just a hammer" and the only answer can be: “Put down the hammer and use a screwdriver!” ;-(
Well, I hope you, Dave, and David are satisfied. 👿
I thought, "Well, OK. Perhaps I've got something more screwdriverform in my arsenal ...". Bad move on my part.
I eBayed a second Mindstorms EV3 kit (mostly present) and now I have the means to build two Battleship-playing robots.
All I need now is a 3D printer to make pseudo-scale models of a few ships, and I'm in business. 😈
In anticipation of future events, I'm just off to have a T-shirt printed ...
(https://girlgenius.fandom.com/wiki/Spark)
Stuart
This afternoon's task is testing out the new EV3. In the words of Captain Oates, "I may be some time".
Sounds like a productive afternoon!
I would suggest teaching the robots to write (or carve) cuneiform as well—two birds with one stone.
@Werner_E wrote:
Sounds like a productive afternoon!
Oh, yes. EV3 Triage Complete.
It's Alive!
It lives!
Serendipitously, the Bonzo Dog version of the Monster Mash has literally just popped up on my Alexa playlist as I type.
And the really good thing is that I've got a second complete EV3 set for less than the average price of the Brick itself on eBay.
@Werner_E wrote:
I would suggest teaching the robots to write (or carve) cuneiform as well—two birds with one stone.
Don't think that hasn't occurred to me! I'm just working out how to use 6xAA batteries to run a robot and power the terawatt laser (start off small, I always say).
Pulse width modulation? That way, I could have the burning material play back the words of Shulgi, whilst enumerating them.
"Where I stand, I destroy foreign villages; where I sit, I set fire to local villages. At my command, ....... Where my robots strike, ....... Their bricks are dug up from the footings and absorbed into my EV3 collection ....... The villages which I lase shall not be restored; the houses which I destroy shall be counted as ruin mounds; the walls proudly rising to heaven shall not open"
(I might have been a bit loose with the translation)
I need more EV3s. It seems I may have a worthy foe.
My war elephant has Lasers and Jets, though (eat your hearts out, Sharks).
First task, though -- the villagers will wait, I'm sure; they're good like that -- is to tidy up my Mathcad utilities library. I noticed a couple of conflicts whilst randomly copying functions from other worksheets into my Battleship attempt, but not the kind of conflicts I prefer.
Stuart
Is it just me, or is MP 11.0.1.0 a memory hog compared to MP 11.0.0.0? I've only got two worksheets open, but 1.0 keeps going off to sulk in protest, which I don't recall 0.0 doing.
Is it just me, or is MP 11.0.1.0 a memory hog compared to MP 11.0.0.0? I've only got two worksheets open, but 1.0 keeps going off to sulk in protest, which I don't recall 0.0 doing.
I can't comment on that because I haven't installed the new version. I couldn't find any information to help me decide whether it would be worth doing. Your experience seems to be more of an indicator not to do it.
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?
@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.
Yes. Per the PDF in the first hyperlink, that number corresponds to the number of holes in each type of game piece, corresponding to the number of hits required to sink that game piece.
Although a standard game is 10 x 10, the challenge is open ended. It would be interesting to see how changing the grid size affects game play, especially if you write your own "hunter algorithm." How does an 8 x 8, 12 x 12, or 20 x 20 grid affect the average number of shots for your algorithm to sink the fleet? I hope people have fun with this. But again, do as little or as much as you want. No one should feel compelled to replicate the entire game play in Mathcad.
I'm really interested in seeing what people come up with. As always, people can build on previous submissions.
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)
Oops! I forgot to show the default starting position above. Here it is:
Alan
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).
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)
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.
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!)
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)
https://www.youtube.com/watch?v=jDQwPNleZ6I
I recorded playing through Alan's worksheet (and Werner's, to be uploaded next week).
I got 54 again. Hm...
Werner's worksheet (with Verbose Mode on):
https://www.youtube.com/watch?v=M_TXFbJP1No
It's very cool.
@AlanStevens wrote:
Here is my third (and probably final) contribution.
I'm sorry, Alan. I couldn't read that statement without thinking ...
Stuart
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)
"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.
(Though suddenly I'm feeling pretty good about my 54 shots.)
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
Full confession: I recalculated until I got 100, just for the screenshot.
