Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

guys can you chek this code its not working where is the fault?

조회 수: 1 (최근 30일)
Ezgi kaya
Ezgi kaya 2020년 7월 1일
마감: MATLAB Answer Bot 2021년 8월 20일
clear;
clc;
level = inputdlg('enter level');
str2double(level)
if level == 1
boardgame = ones(8,8);
elseif level == 2
boardgame = ones(16,16);
elseif level == 3
boardgame = ones(24,24);
else
fprintf('pls choose a level')
end
ship_x = [];
ship_y = [];
settle = ceil(rand(1,1),2);
if settle == 1
ship_x(1) = ceil(rand(1,1),8);
ship_x(2) = ship_x(1);
ship_x(3) = ship_x(2);
ship_y(1) = ceil(rand(1,1),6);
ship_y(2) = ship_y(1)+1;
ship_y(3) = ship_y(2)+1;
elseif settle == 2
ship_y(1) = ceil(rand(1,1),8);
ship_y(2) = ship_y(1);
ship_y(3) = ship_y(2);
ship_x(1) = ceil(rand(1,1),6);
ship_x(2) = ship_x(1)+1;
ship_x(3) = ship_x(2)+1;
end
settle = ceil(rand(1,1),2);
for k = 1:1
if settle == 1
ship_x(1) = ceil(rand(1,1),8);
ship_x(2) = ship_x(1);
ship_x(3) = ship_x(2);
ship_y(1) = ceil(rand(1,1),6);
ship_y(2) = ship_y(1)+1;
ship_y(3) = ship_y(2)+1;
elseif settle == 2
ship_y(1) = ceil(rand(1,1),8);
ship_y(2) = ship_y(1);
ship_y(3) = ship_y(2);
ship_x(1) = ceil(rand(1,1),6);
ship_x(2) = ship_x(1)+1;
ship_x(3) = ship_x(2)+1;
end
end
for a = 1:3
A(a) = [ship_x(a),ship_y(a)];
end
  댓글 수: 1
John D'Errico
John D'Errico 2020년 7월 1일
So many possible reasons why it i not worksing. Mainly, I can't say this any better, but you probably need to learn more about MATLAB. And sadly, I have no idea why it is not working, because I don't know what the code is supposed to do.
For example, I'm not sure what uou think this does:
settle = ceil(rand(1,1),2);
but it ALWAYS generates the scalar value 1. NEVER 2.
Other things:
for k = 1:1
this is not a loop. It just assigns the value 1 to k.
But knowing what the code should do? This I cannot guess.

답변 (1개)

neil jerome
neil jerome 2020년 7월 13일
편집: neil jerome 2020년 7월 13일
hi ezgi,
you've got yourself into some trouble here; don't worry, matlab isn't straightforward, and you had some decent ideas :)
i've written some code here to set up a battleship board with one boat, where you can freely choose the size of both. it does mostly what you were trying, i think, but maybe gives you a few new functions to look at and some hints as to how to structure your code. and don't forget to write comments for yourself, always!
your main problem was the syntax on using rand(); see how i have used it here.
good luck!
neil.
% battleship code
% get board and ship size
answer = inputdlg({'input board size', 'ship size'}, 'set difficulty', [1 10; 1 10], {'8', '3'});
boardSize = str2double(answer{1});
shipSize = str2double(answer{2});
if shipSize > boardSize
error('good luck with a boat that size!');
end
% decide if ship if horizontal/vertical on board
orient = round(rand());
if orient % vertical ship
shipStart = [ceil((boardSize-shipSize)*rand())+1 ceil((boardSize)*rand())+1];
shipCoord = repmat(shipStart', [1 shipSize]) + vertcat((1:shipSize)-1,zeros(1,shipSize));
else % horizontal ship
shipStart = [ceil((boardSize)*rand())+1 ceil((boardSize-shipSize)*rand())+1];
shipCoord = repmat(shipStart', [1 shipSize]) + vertcat(zeros(1,shipSize),(1:shipSize)-1);
end
% show board
figure;
board = zeros(boardSize);
for aa = 1:shipSize
board(shipCoord(1,aa), shipCoord(2,aa)) = 1;
end
imagesc(board);

이 질문은 마감되었습니다.

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by