Introduction: Micro:Bit Battleship - School Assignment

To create some sense of depth to a project almost exclusively using two Micro:Bits and the parts built-in, it felt appropriate to utilise the connective functionality of these devices to create an, admittedly limited, game.

The 25 LED grid and 3 buttons set up a few strict limitations that have to be followed. Thankfully, the official guides to these little devices come with multiple basic ideas which all fit comfortably within these limits.

The one to focus on in this instructable will be Battleship.

Taking out the bare essentials of the game, we have a few key aspects that can be used as a minimal framework to make this game. That being; the grid, directional inputs, a way of "firing", and finally a way of indicating a hit or a miss.

Supplies

Required Materials:

2 Micro:bits [optional]

PC with Internet Access


If physical Micro:Bits are unavailable, [makecode.microbit.org] can serve as a substitute.

For all purposes of this tutorial, all steps are completed virtually.

Step 1: Setting Up the Connection

There are three languages that MakeCode accepts as input, being:

  1. Block Code
  2. Python
  3. JavaScript

This tutorial will show Block.

The first step is to assign the pair of Micro:Bits a "radio group". This effectively serves as a channel for the two devices to communicate across.

In the basic section and the radio section of code blocks, taking radio set group and on start, and placing them together allows for communication across the assigned group. Any positive integer is accepted for this step.

For this example, the value has been set to (72)

Step 2: Setting Up the Board

Following the On Start command from the radio set up, we can continue further down, adding in the looping functions that create the board, and placing the ships onto the display.

The first loop creates an empty array. Then the amount of ships is determined (5).

The second loop is what determines where each "ship" is placed. The position is determined by a pair of random numbers, ([x, y] coordinates). The numbers are generated between (0 and 4) and (1 and 4), to maintain the information displayed in the top-most row.

The [x] and [y] coordinates are then saved as positional coordinates.

At the end of this setup, the variable determining the number of ships placed is decreased by one, until all the determined ships are placed onto the board.

Step 3: Pressing Buttons

Now that both players have a board, the buttons need to dictate as to whether or not anything actually happens.

The [A] button determines the [x] axis input, the [B] button determines the [y] axis input. The [A+B] button saves the two-axis inputs and sends them to the opposing player.

Step 4: Keeping Score

If the receiver has a ship on the same [x, y] position that the sender fires at, the battleship is unplotted, or otherwise removed from the board. Then, the value is sent back to indicate a hit.

If the receiver does not have a ship on the same coordinate, a miss signal is sent back instead.


The choice to take turns is left to the players, as the code has no way of enforcing any such rules.

Step 5: Win Conditions

After hitting all five of the opponent's ships, the screen is cleared and victory is declared!

This stops all actions as the victory message scrolls across the display until the program is ended.

To play again, shut down and restart the game.