# help with MATLAB battleship program

79 views (last 30 days)
Alex Skantz on 6 Oct 2021
Commented: Walter Roberson on 7 Oct 2021
Hi all, Im working on a battleship assignment for a class. Here is the code I have so far:
clear all
close all
clc
rowdim=input('give me the number of rows on the board: ');
coldim=input('give me the number of columns on the board: ');
GameBoard=zeros(rowdim,coldim); %defined board dimensions
%matlab puts a random ship on the board
battleship_row=randi(rowdim,[1 1]) %random row
battleship_col=randi(coldim,[1 1]) %random column
GameBoard(battleship_row,battleship_col)=0;%%'0' hides the loaction of the battleship (unless it is correct)
%playguess
for guessnum=1:5
rowguess=input('guess what row the battleship is in: ');
colguess=input('guess what column the battleship is in: ');
[winloss]=results(rowguess,colguess,battleship_row,battleship_col);
%assesses the results
if winloss == 1
GameBoard(rowguess,colguess)=3; %marks winning guess with '3' on board
break; %finishes the loop, because you won
GameBoard %displays the winning gameboard
else
disp('try again')
GameBoard(rowguess,colguess)=2; %mark loss with '2' on board if guess is incorrect
GameBoard %displays the gameboard to visualize your loss
end
end
GameBoard
[winloss]=results(rowguess,colguess,battleship_row,battleship_col); %calling subplot
%subfunction from here on
function [winloss]=results(rowguess,colguess,battleship_row,battleship_col)
if (rowguess == battleship_row) && (colguess == battleship_col);
winloss=1;
disp ('You sunk the battleship! You won!');
else
winloss=0;
disp(' ');
end
end
I would like to add more battleships to the game board (there is currently only 1) . What are some ways I can go about doing that?

Walter Roberson on 7 Oct 2021
N = 5;
battleship_row=randi(rowdim,[1 N]) %random row
battleship_col=randi(coldim,[1 N]) %random column
%the following is actually useless as you initialized GameBoard to all 0
%anyhow
GameBoard(sub2ind(size(GameBoard,battleship_row,battleship_col))=0;
but you need to change
if (rowguess == battleship_row) && (colguess == battleship_col);
to
if any((rowguess == battleship_row) & (colguess == battleship_col))
Walter Roberson on 7 Oct 2021
No, my change causes N single-space ships to be added to the board. But unless the user changes more of their code, hitting any one of them would be a "win".

Image Analyst on 7 Oct 2021
You need a function that's called BuildBoard() or something. Then in there call a function called PlaceShip() the required number of times.
function gameBoard = BuildBoard(rows, columns, numberOfShips)
gameBoard = false(rows, columns); % Initialize a blank board.
maxShipLength = 5;
shipSizes = randi(maxShipLength, 1, numberOfShips)
for k = 1 : numberOfShips
gameBoard = PlaceShip(shipSizes(k));
end
Then in PlaceShip() you get a random location and direction. Then see if any of those locations is yet occupied by a prior ship. If none are then set those locations of gameBoard to true, otherwise try a different location until you succeed in placing it.

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!