Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Battleship Ships not plotting

1 visualización (últimos 30 días)
Gabriella Lazzara
Gabriella Lazzara el 2 de Nov. de 2020
Cerrada: Rik el 2 de Nov. de 2020
I know this has been done before but my code is finished and there must be a minor problem in my code because the only output when my grid shows up is 'miss' meaning my ships arent plotting. I know they were plotting before now I cant figure out why they aren't now. This will be my last question if someone can just help me out.
Thanks a lot
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

Respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by