# Battleship

## Summary

This activity aims to engage students in a low-stakes activity to build confidence and review basic topics including indexing matrices and vectors, extracting data from a matrix, operating on data from a matrix, and using a matrix to store data. For this activity, we will look at a practical application of data storage. Consider the game Battleship. Two players have 5 ships each: Cruiser (length 2), Destroyer (length 3), Submarine (length 3), Battleship (Length 4), and Aircraft Carrier (length 5). Players secretly arrange their ships on a 10 by 10 grid. Players alternate bombing the opponent, by calling out a space. A player wins when they sink all of their opponent's ships. We will use the context of this game to introduce the concepts of data storage and extraction using a matrix.

**your**modifications and improvements to this activity through the Community Contribution Tool »

## Learning Goals

The main learning objectives in this activity include (1) indexing matrices and vectors, (2) extracting data from a matrix, (3) operating on data from a matrix, and (4) using a matrix to store data. MATLAB commands are used, but the actual use of MATLAB is not necessary for this activity. This is a smaller activity in a larger set of tasks and activities focused on tactile learning for introductory programming courses.

## Context for Use

This classroom activity should be done in an introductory programming course and is best suited for smaller classes (less than 30 students). This can be done early in the semester, as it requires very few prerequisite programming skills. Students need to be familiar with basic MATLAB commands that extract data from a matrix. Although this activity was designed to be done at the beginning of the semester, the instructor may include more advanced examples if they wish to implement this later in the semester.

## Description and Teaching Materials

For this activity, we will look at a practical application of data storage. Consider the game Battleship. Two players have 5 ships each: Cruiser (length 2), Destroyer (length 3), Submarine (length 3), Battleship (Length 4), and Aircraft Carrier (length 5). Players secretly arrange their ships on a 10 by 10 grid. Players alternate bombing the opponent, by calling out a space. A player wins when they sink all of their opponent's ships. We will use the context of this game to introduce the concepts of data storage and extraction using a matrix.

- Suppose that you wanted to design a Battleship app in MATLAB. What information would you need to keep track of?
- How would you store this information?
- Matrices are a great way to store this information. Think with a partner about how you would store the following.
- Where are your ships?
- Where are your opponent's ships?
- Where have you guessed?
- Was that guess a hit or a miss?
- Where has your opponent guessed?
- Was that guess a hit or a miss?

- The figures below are pictures from a game of battleship. Store the information using matrices.
- Arrange the ships on your group's battleship board. Write down the code to store this information in MATLAB.

Battleship Handout (Acrobat (PDF) 2.2MB Sep13 23)

## Teaching Notes and Tips

This is a straightforward activity that should not require too much additional planning. As with many tasks and activities, it is important to set clear expectations before you begin. For example, make sure that you encourage collaboration and willingness to participate so that students do not feel uncomfortable. These expectations should be set on the first day of class, but it helps to reiterate them before an activity such as this. This is a rather brief activity and should only take one class period to complete. It is recommended that you begin class with this activity and then transition into some indexing problems using MATLAB.

Note that depending on how early in the semester this activity is implemented, students may complete this task with varying levels of sophistication. For example, if students are just learning basic MATLAB commands, they may manually type the entries of the matrix. However, students with knowledge of for loops may store the board state as follows.

% Define the dimensions of the Battleship board.

rows = 10; % Number of rows.

cols = 10; % Number of columns.

% Initialize the board with zeros (empty spaces).

board = zeros(rows, cols);

% You can place your ships by updating the board with 1s in appropriate locations.

% For example, place a ship of length 3 horizontally starting from (1,1).

ship_length = 3;

start_row = 1;

start_col = 1;

for i = 1:ship_length

board(start_row, start_col + i - 1) = 1;

end

% Display the initial board state.

disp(board);

**your**modifications and improvements to this activity through the Community Contribution Tool »

## Assessment

Since this is a short in-class activity, no summative assessment is necessary. However, summative assessment could be done by using a rubric that gauges students' understanding of indexing matrices and vectors, extracting data from a matrix, operating on data from a matrix, and using a matrix to store data.

Good: Board state is represented using a script that initializes a matrix and updates using a for loop.

Fair: Board state is represented by manually typing the entries of the 10 by 10 matrix.

Poor: MATLAB code does not run.

It is also easy to do a formative assessment of student understanding based on their participation in this activity. It is also recommended that you survey students to capture their perceptions of such activities. Note that this can be done directly following this task or after a series of tactile activities.

## References and Resources

[1] Barnes, J., & Libertini, J. M. (2018).Tactile Learning Activities in Mathematics: A Recipe Book for the Undergraduate Classroom (Vol. 54). American Mathematical Soc.

*This teaching activity was created as a part of the Teaching Computation with MATLAB Workshop held in 2023 at Carleton College.*