how to avoid intersecting array within a a matlab?

1 visualización (últimos 30 días)
roshan varkey
roshan varkey el 3 de Dic. de 2013
Comentada: Walter Roberson el 3 de Dic. de 2013
sorry the question is a bit abstract .i am making a battleship game on a 10x10 matrix. the user has to type in coordinate in (alphabets,number) style. i tried doing this and was able to place ships horizontally or vertically......but since the ships are of different unit length they are now intersecting with each other....and I don't know how to fix it.. this is what I did:
% code
Board=zeros(10);
A=1;
B=2;
C=3;
D=4;
E=5;
F=6;
G=7;
H=8;
I=9;
J=10;
%destroyer align=randi(10); if align < 5 ALPHArange = 'A':'J'; Drow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for destroyer Dcol = randi(10,1,1); %picks a random column for destroyer idx = find(ismember(Drow, ALPHArange)); Board(idx, Dcol:Dcol + 1) = 2; elseif align >= 5 ALPHArange = 'A':'J'; Drow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for destroyer Dcol = randi(10,1,1); %picks a random column for destroyer idx = find(ismember(Drow, ALPHArange)); Board(idx:idx+1,Dcol) = 2; end
%submarine align=randi(10); if align < 5 ALPHArange = 'A':'J'; Srow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for sub Scol = randi(10,1,1); %picks a random column for sub idx = find(ismember(Srow, ALPHArange)); Board(idx, Scol:Scol + 2) = 3; elseif align >= 5 ALPHArange = 'A':'J'; Srow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for sub Scol = randi(10,1,1); %picks a random column for sub idx = find(ismember(Srow, ALPHArange)); Board(idx:idx+2,Scol) = 3; end
%battleship align=randi(10); if align < 5 ALPHArange = 'A':'J'; Brow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for BS Bcol = randi(10,1,1); %picks a random column for BS idx = find(ismember(Brow, ALPHArange)); Board(idx, Bcol:Bcol + 3) = 4; elseif align >= 5 ALPHArange = 'A':'J'; Brow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for BS Bcol = randi(10,1,1); %picks a random column for BS idx = find(ismember(Brow, ALPHArange)); Board(idx:idx+3,Bcol) = 4; end
%cruiser align=randi(10); if align < 5 ALPHArange = 'A':'J'; Crow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for Crus Ccol = randi(10,1,1); %picks a random column for Crus idx = find(ismember(Crow, ALPHArange)); Board(idx, Ccol:Ccol + 2) = 6; elseif align >= 5 ALPHArange = 'A':'J'; Crow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for Crus Ccol = randi(10,1,1); %picks a random column for Crus idx = find(ismember(Crow, ALPHArange)); Board(idx:idx+2,Ccol) = 6; end
%aircraft carrier align=randi(10); if align < 5 ALPHArange = 'A':'J'; ACrow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for air craft ACcol = randi(10,1,1); %picks a random column for aircraft idx = find(ismember(ACrow, ALPHArange)); Board(idx, ACcol:ACcol + 4) = 5 elseif align >= 5 ALPHArange = 'A':'J'; ACrow = ALPHArange(randi(numel(ALPHArange))); %picks a random row for air craft ACcol = randi(10,1,1); %picks a random column for air craft idx = find(ismember(ACrow, ALPHArange)); Board(idx:idx+4,ACcol) = 5 end I am new at matlab.help will be really appreciated!

Respuestas (0)

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by