フィルターのクリア

Randomly insert new variable

3 ビュー (過去 30 日間)
Krish Desai
Krish Desai 2015 年 11 月 10 日
回答済み: Walter Roberson 2015 年 11 月 11 日
My code currently creates a board of size n x n (user input) What I want to do now is to have certain positions on this board be called "kings", and I want roughly 1 king per 20 spaces i.e for a 10 x 10, there are 100 spaces so 5 kings. How do I randomly pick a board position to put a king?
function board = makeboard(size)
board = cell(size+1,size+1);
% initialize the board
for i=1:size+1
for j=1:size+1
if i==1
if j<=size
board{i,j+1} = j;
board{j+1,i} = j;
end
elseif j==1
% do nothing
else
board{i,j}= 'x';
end
end
end
amount=size*size;
king=amount/20;
king=floor(king);
end

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 11 日
Sorry, all of the normal ways of doing this involve using the MATLAB function named size(), which is not possible in your code because you named a variable "size". But you could change
else
board{i,j}= 'x';
to
elseif rand < 1/5
board{i,j} = 'k';
else
board{i,j} = 'x';

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBoard games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by