Bulgarian Solitaire code doesn't iterate using if loop and for loop
1 次查看(过去 30 天)
显示 更早的评论
I need to create a game of Bulgarian Solitaire. It's not the most elegant, but the code I have so far works for one iteration. There's a flaw in that the code doesn't iterate properly (stops after the first time through the code). I'd appreciate your help in finding the error. Thanks!
% Bulgarian Solitaire
% Generate a random integer array whose elements total less than 46
array = randperm(45,1);
k = sum(array);
i = 2;
while k <= 45
array(i) = randperm(45,1);
k = sum(array);
i = i+1;
end
while k > 46
array(i) = 0;
k = sum(array);
i = i-1;
end
% Remove zeros from the initial array
value = find(array(:) == 0);
array(value) = [];
array = reshape(array, size(array,1), []);
% Sort array from smallest to largest
array = sort(array)
% Find the value of the largest element in the array
maxValue = max(array(:));
% Iterate the solitaire process
% If maxValue > 9, continue interation
count = 0;
if maxValue > 9
% Subtract one from each element of the array and increment counter
for i = 1:length(array)
array(i) = array(i) - 1;
count = count + 1;
i = i + 1;
end
% Increment array length by 1 and add the count value to the array
array(length(array) + 1) = count;
% Remove zeros from the array
value = find(array(:) == 0);
array(value) = [];
array = reshape(array, size(array,1), []);
% Find the value of the largest element in the array
maxValue = max(array(:));
% Sort array from smallest to largest
array = sort(array)
end
2 个评论
采纳的回答
Image Analyst
2017-1-29
You mean like this:
array = [3 33]
theSum = sum(array)
linearArray = 1 : theSum
index = find(cumsum(linearArray) >= theSum)
% Take subarray
array = linearArray(1:index)
It produces:
array =
1 2 3 4 5 6 7 8
like you're asking for.
0 个评论
更多回答(1 个)
Image Analyst
2017-1-29
What do you mean by "elements total less than 46"? You mean that the sum of them is less than 46? That will never, ever, ever happen if your array is randperm(45). Not if you sum the whole array.
You can use cumsum(), instead of a for loop, to determine exactly when the sum goes over 46.
index = find(cumsum(array) >= 46);
% Take subarray
array = array(1:index-1);
You'd be better off using randi() and even then it will go over unless you put a very small upper limit on it.
I don't know what requirements you have, like a requirement on the max value of an element, or a requirement on the number of elements.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!