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!');