I want to randomly plot battleships on my board, but no outputs are present?

2 次查看(过去 30 天)
%Board
size = 10 %A-J on regular battleship and 1-10
board = zeros(size);
axis([0,10,0,10]);
title('Battleship')
letters = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J'};
set(gca,'xtick',[1:10],'xticklabel',letters);
grid on
disp(board);
%ship lengths
destroyer = [2,2];
submarine = [31,31,31];
cruiser = [32,32,32];
battleship = [4,4,4,4]
carrier = [5,5,5,5,5]
x1 = randi(10,1);
y1 = randi(1,10);
x2 = x1 + length(submarine) - 1;
col2 = y1;
board(x1:x2, y1) = submarine(1);
  1 个评论
Rik
Rik 2020-11-2
Regarding your flag ("delete question not necessary anymore"): that is not how this forum works. If you decide to edit away your question, everybody with editing privileges can restore it from this backup.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2020-10-28
You are taking two approaches simultaneously in your code. You create a plot where I suspsect you want to visualize the ships, but then you are populating a matrix with the values. In order for the ships to appear on your axes, you must plot them somehow (plot, scatter, line, etc).
If you want to display board instead, perhaps somethink like heatmap will work?
%Board
size = 10 %A-J on regular battleship and 1-10
size = 10
board = zeros(size);
%ship lengths
destroyer = [2,2];
submarine = [31,31,31];
cruiser = [32,32,32];
battleship = [4,4,4,4];
carrier = [5,5,5,5,5];
x1 = randi(10,1);
y1 = randi(1,10);
x2 = x1 + length(submarine) - 1;
board(x1:x2, y1) = submarine(1)
board = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
figure
heatmap(board)
title('Battleship')
letters = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J'};
set(gca,'XDisplayLabels',letters);
colorbar off

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Red 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by