Game Commands

The MATLAB game commands that are provided by the instructors are as follows:

Battleship.mat

This file holds the images and the two game boards for quick setup and initialization of the game.

To use this command, follow what is displayed below:

load Battleship.mat

Displaying Images

To display images of the boards, the imshow command can be used in combination with the arrays of images. These boards are automatically setup by running the load Battleship.mat command.

A command like displayed below will show the battleship board:

imshow([Opponent_Board{1, :};Opponent_Board{2, :};Opponent_Board{3, :};
Opponent_Board{4, :};Opponent_Board{5, :};Opponent_Board{6, :};
Opponent_Board{7, :};Opponent_Board{8, :};Opponent_Board{9, :};
Opponent_Board{10, :}]);

To change images on the board, change the position in the specific array to the new image. The images are all loaded by the load Battleship.mat command and have names like Hit, Miss, Boat_mid_hor. A command like displayed below will change the image at the specified position to a Hit.

Opponent_Board{8, 5} = Hit;

Setup

The instructors have included a file called Setup.m that contains a function that when used will produce a matrix that corresponds to random choices for the computer’s ship placement. The matrix is arranged as follows below:

Column 1 contains the ship number (1 = Aircraft Carrier, 2 = Battleship, 3 = Submarine, 4 = Cruiser, 5 = PT Boat)

Column 2 contains the ship’s orientation with 1 being vertical and 2 being horizontal

Column 3 contains the row for the ship placement with (0, 0) being placed at the top-left corner

Column 4 contains the column for the ship placement with (0, 0) being placed at the top-left corner

This matrix can be parsed to determine where pictures need to be lined up at then used in the previous Displaying Images section.

The setup function itself can be used as follows below:

Computer_Board = Setup();

Board Placement

To place the boards both on one figure, with the opponent board on top of the player board, use the subplot command as follows below:

subplot(2,1,1)

% Display Opponent_Board here

subplot(2,1,2)

% Display Player_Board here

Dialog Boxes

To display dialog boxes, use the errordlg and msgbox commands:

errordlg(‘Error Text!’);

msgbox(‘This is a message!’);

Playing Audio

To play audio for certain events, use the audioread, audioplayer, and play commands. Audio files must be in the file directory for them to work.

hitSound = audioread(‘explosion.wav’);

out_hitSound = audioplayer(hitSound, 44100);

play(out_hitSound); pause(2);

stop(out_hitSound);