Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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 Comment
John D'Errico
John D'Errico on 1 Jul 2020
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.

Answers (1)

neil jerome
neil jerome on 13 Jul 2020
Edited: neil jerome on 13 Jul 2020
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);

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!