フィルターのクリア

how to perform matrix manipulation for a game of battle ship?

2 ビュー (過去 30 日間)
roshan varkey
roshan varkey 2013 年 12 月 2 日
コメント済み: roshan varkey 2013 年 12 月 4 日
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 for one ship and the matrix has gone all over the place and i am lost. what i did was
% 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
ALPHArange='A':'J';
Drow= ALPHArange(randi(numel(ALPHArange))); %picks a random row for destroyer
Dcol=randi(100,1,1); %picks a random column for destroyer
Board(Drow,Dcol:Dcol+1)=2
here i want to place a ship called destroyer of unit length 2.(on the matrix two zeros ,beside each other,should become 2).....upon running the script i got an extremely lage matrix which made no sense

採用された回答

sixwwwwww
sixwwwwww 2013 年 12 月 2 日
編集済み: sixwwwwww 2013 年 12 月 2 日
roshan try this:
Board = zeros(10);
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Board(idx, Dcol:Dcol + 1) = 2;
  3 件のコメント
sixwwwwww
sixwwwwww 2013 年 12 月 2 日
idx = find(ismember(ALPHArange, Drow));
this line find the index of Drow in array ALPHArange. For example, if randomly choosen alphabet is 'E' then idx will have value 5, for 'C' value 3 and so on and it will be your row number in the next line
Board(idx, Dcol:Dcol + 1) = 2;
roshan varkey
roshan varkey 2013 年 12 月 4 日
but when i tried this,the value of .......idx = find(ismember(ALPHArange, Drow))........... is always 1,even when the randomly choosen alphabet is 'E' or 'F'........ so, all the points i try to put into matrix always ends up in the first row.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by