フィルターのクリア

How to assign an index on the edge of an array a certain value

2 ビュー (過去 30 日間)
Ross Anderson
Ross Anderson 2018 年 11 月 1 日
コメント済み: Ross Anderson 2018 年 11 月 1 日
I am working on a function the creates a "maze" or NxN array of strings "0". The goal is to move a player "P" in the array randomly through the maze until the player reaches the exit "E" How do i randomly assign an index on the edge of the array the value "E" to represent the exit of the maze?
ex. I need it to look like this
"0" "0" "0" "0"
"0" "0" "0" "0"
"P" "0" "0" "0"
"0" "0" "0" "E"
where "E" is randomly assigned but needs to be on the edge of the array

採用された回答

Matt J
Matt J 2018 年 11 月 1 日
編集済み: Matt J 2018 年 11 月 1 日
Z=false(N);
Z(:,[1,N])=true; Z([1,N],:)=1;
I=find(Z);
n=numel(I);
idx=I(randperm(n,2));
Maze(1:N,1:N)="0" ;
Maze(idx(1))="P";
Maze(idx(2))="E"
  10 件のコメント
Matt J
Matt J 2018 年 11 月 1 日
If you already have a Maze with a pre-placed "P", then modify as follows:
Z=false(N);
Z(:,[1,N])=true; Z([1,N],:)=1;
Z(Maze=="P")=0;
I=find(Z);
idx=I(randperm(numel(I),1));
Maze(idx)="E"
Ross Anderson
Ross Anderson 2018 年 11 月 1 日
That worked, Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLabyrinth problems についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by