필터 지우기
필터 지우기

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