How to determine whether any elements of an array are a certain a number
72 views (last 30 days)
Show older comments
I am creating a simple minesweeper game for an assignment, but I am having trouble making the program determine whether the player has won or not. I am trying to find a function that determines whether any of the elements of A are equal to -2 and then use that in a while loop.
I have a simplified test code that I have been using to try to do this, but I can't find any functions that do what I want to do.
With everything I've tried so far, it either just keeps asking for more input even when it should say that the game is over, or it skips the while loop entirely. The last thing probably happens because all the elements of the start array are equal.
I have looked at xor, unique, setdiff, intersect, ismember isempty, and probably a couple more that I can't remember.
As this is not the complete code, I have just answered yes to the flag question every time in order to change all the elements from -2 to -3.
I also know that this wouldn't be the best way to determine whether the player has won or not, as it doesn't take into consideration if all the flags are placed correctly, but I wanna sort this part out first before I get on to that:)
A=zeros(2)
selected=0;
flag=-3
for r=1:2
for c=1:2
A(r,c)==-2;
end
end
while % any of the elements in A are equal to -2
while selected~=-1
selectRow=input('Which row is the cell you would like to access on? ');
selectCol=input('Which column is the cell you would like to access on? ');
selectFlag=input('Would you like to put a flag in this cell? ','s');
if selectRow<=2 && selectRow>=1 && selectCol<=2 && selectCol>=1
while strcmp(selectFlag, 'yes') || strcmp(selectFlag, 'no')
if strcmp(selectFlag, 'yes')
A(selectRow,selectCol)=flag;
disp(A);
elseif strcmp(selectFlag, 'no')
selected=mineBoard(selectRow,selectCol);
A(selectRow,selectCol)=selected;
disp(A);
end
end
end
end
fprintf('You have hit a mine. Please restart.\n');
end
fprintf('Congrats! You have won!');
0 Comments
Answers (2)
Adam Danz
on 3 May 2019
Edited: Adam Danz
on 3 May 2019
Convert the matrix into a vector using (:) and then use any() to determine if there are any values of -2.
while any(A(:)==-2)
3 Comments
Adam Danz
on 5 May 2019
You have 3 nested while-loops and the execution of the code is never escaping those nested while loops which prevents the first while-loop from competing. There are some deep problems with the script you shared. For example, if the user-response to the first three questions are '1', '1', and 'yes', the code enters what seems to be an infinite loop (I quit waiting after several seconds).
Steven Lord
on 3 May 2019
First, the == operator is for comparing two arrays element-wise.
A = magic(3);
A == (10-A) % Only returns true for the 5 inside A
The = operator (one equals sign) is for assignment. Therefore this line inside your first set of nested for loops doesn't do what you think it does. It performs a comparison then throws away the result of that comparison.
A(r,c)==-2;
In fact, you can replace that whole block with any of these lines:
A = -2 + zeros(2);
A = -2*ones(2);
A = repmat(-2, [2, 2]);
Since you already preallocated A using zeros on your first line, it would be a simple matter to add -2 (or subtract 2) on that first line.
For your main question, the any function will be of use to you. If you're using release R2018b or later, the 'all' option for the any function will be of particular interest.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!