Battleship Program Ships plotting

17 visualizaciones (últimos 30 días)
Gabriella Lazzara
Gabriella Lazzara el 2 de Nov. de 2020
Comentada: Image Analyst el 2 de Nov. de 2020
Hello, I am aware that many battleship programs have been done before, but this is my final question. I am done with my code but now I realize that my ships haven't printed on my map. There are no errors the only thing that shows up every time I click is 'miss' with a white space so I am just confused. Please any help would be great.
function battleship(n, names)
close all
clear all
figure
axis equal off
hold on
running= 1; % true if game is running, false otherwise
%board
n= 10;
for i=0:n-1
for j=0:n-1
% Draw board
fill([i i+1 i+1 i i],[j j j+1 j+1 j],'b')
end
end
Destroyer = 2;
Submarine = 3;
Cruiser = 3;
Battleship = 4;
Carrier = 5;
shiping= [Destroyer, Submarine, Cruiser, Battleship, Carrier];
names = [2, 3, 3, 4, 5];
board = zeros(10)
%Place ships
for i = 1:1:numel(names);
shipsize = shiping(i);
ship = "not placed";
while ship == "not placed"
column = randi(10); %pick a start column
row = randi(10); %pick a start row
orient = randi(2)
if board(row, column) == 0
lastcolumn = 0;
lastcolumn = column + shipsize;
lastrow = row + shipsize;
score = 0;
if orient == 1
if lastcolumn > n
else
for j = 1:1:shipsize
if board(row, column+(j-1)) == 0
if j == shipsize && score == 0
board(row, column:column+(shiping(i)-1)) ~= names(i);
ship = "placed";
end
else
score = 1;
end
end
end
end
if orient == 2
if lastrow > n
else
for j = 1:1:shipsize
if board(row+(j-1), column) == 0
if j == shipsize && score == 0;
board(row:row+shiping(i)-1, column) ~= names(i);
ship = "placed";
end
else
score = 1;
end
end
end
end
end
end
end
squares= zeros(n);
movesMade = 0;
titleText='Battleship';
movesMadeText= sprintf('Number of Moves Made: %d',movesMade);
while running == 1
title({titleText, movesMadeText}, 'FontSize', 12');
[row,column] = ginput(1);
X= ceil(row);
Y= n-ceil(column)+1;
%click on a square
if X<=n && Y <=n && X>=1 && Y>=1
if squares(X,Y)==0
squares(X,Y)= 1;
movesMade = movesMade + 1;
if board(X,Y)~=0
titleText= 'Hit!';
movesMadeText= sprintf('Number of Moves Made: %d',movesMade);
names{board(X,Y),2}= names{board(X,Y),2} - 1;
fill ([X-1 X X X-1 X-1],[n-Y n-Y n-Y+1 n-Y+1 n-Y],'r');
else
titleText= 'Miss!';
movesMadeText= sprintf('Number of Moves Made: %d',movesMade);
fill ([X-1 X X X-1 X-1],[n-Y n-Y n-Y+1 n-Y+1 n-Y],'w');
end
end
%check if any ship has been sunk
sumS= 0;
for shipIndex= 1:names
if names(shipIndex)==0
names(shipIndex,2) = -1; %-1 means that ship has been sunk
shipName= names(shipIndex,1);
titleText= sprintf('You sunk a %s',shipName);
movesMadeText= sprintf('Number of Moves Made: %d',movesMade);
end
sumS= sumS + names(shipIndex);
if sumS== -1 * names %all ships have been sunk
titleText= 'Winner!';
title({titleText, movesMadeText}, 'FontSize', 12');
running = 0;
end
end
end
end
  2 comentarios
Rik
Rik el 2 de Nov. de 2020
Why are you ignoring the mlint advice? You have a function with input arguments, which you promptly nuke with clear all.
Have you used the debugging tools to step through your code, see the flow of the program in action and see what each line is doing to your variables?
Gabriella Lazzara
Gabriella Lazzara el 2 de Nov. de 2020
Sorry I am new to matlab this is my first project ive done completely on my own.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 2 de Nov. de 2020
Get rid of "clear all" because it deletes your input variables.
Next, I didn't notice any way that you display the board. Where are you calling something like
imshow(board, [], 'InitialMagnification', 1000);
  2 comentarios
Gabriella Lazzara
Gabriella Lazzara el 2 de Nov. de 2020
I got rid of the clear all, but when I put that on it overlays a figure ontop of the existing board I have and I still have no ships for some reason.
Image Analyst
Image Analyst el 2 de Nov. de 2020
Set break points at the places where you call fill() to see if it ever gets to the places where you're supposed to set the grid cell fill color to red, white, and blue. What color is a hit ship? Red? Okay, see if it ever gets to that call to fill it with red.

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by