Filter löschen
Filter löschen

I have a battle ship code but I cant get the ships to be placed

2 Ansichten (letzte 30 Tage)
Amanda
Amanda am 3 Dez. 2022
Kommentiert: Walter Roberson am 4 Dez. 2022
I have labeled the ships for the code but they are being placed in the games I want them to be randomly placed by idk how to fix this issue
function battleship(n, names)
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
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;
shipName= names(shipIndex,1);
titleText= sprintf('You sunk a %s',shipName);
movesMadeText= sprintf('Number of Moves Made: %d',movesMade);
end
end
end
end

Antworten (1)

Walter Roberson
Walter Roberson am 3 Dez. 2022
During initialization, keep a logical array occupied that starts out all false.
For each piece to be placed, randomly generate an orientation (horizontal or vertical)
If the orientation is horizontal, randomly generate a starting row between 1 and height of the board, and a random column between 1 and (width of board minus length of ship plus 1). Check the occupied array for that row between the start column and (start column plus width of ship minus 1); if any of the locations are true then there is already a ship there so go back to retry another position. If no entries are already occupied, then mark them as occupied now and place the ship there in your official board.
If the orientation is vertical you do the same sort of thing but by rows instead of by columns.
You will probably find it convenient to hold on to the occupied array for later fast checking to see whether an enemy missile has or has not hit one of your ships.
  4 Kommentare
Amanda
Amanda am 4 Dez. 2022
can you put this into the code that i gave you so I can see where its suppose to go/work?
Walter Roberson
Walter Roberson am 4 Dez. 2022
I could, but I am not going to. It is your assignment, and if I put all of this into place in the code you already wrote, then I would have done most of the work for you. You need to read the code and figure out how things like Ship_Lengths match up to your existing variables.
You would insert this code after you have initialized the ship lengths, and before the code returns, and it should not be inside the code that allows the user to place their ships.
You should be considering having this code as a separate function with appropriate input values.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Word games finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by