I want to randomly plot battleships on my board, but no outputs are present?
1 回表示 (過去 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
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
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
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)
figure
heatmap(board)
title('Battleship')
letters = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J'};
set(gca,'XDisplayLabels',letters);
colorbar off
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Marine and Underwater Vehicles についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!