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

2 ビュー (過去 30 日間)
Gabriella Lazzara
Gabriella Lazzara 2020 年 10 月 28 日
コメント済み: Rik 2020 年 11 月 2 日
%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 Center および File ExchangeRed についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by