Write a function that takes a number to be guessed and a player number, and checks if the winning criteria is met.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/928584/image.png)
This is what I have to work with, I've tried some functions already, but they came back as invalid.
i.e.:
win = Numberguess(gameNum, userGuess)
if abs gameNum==userGuess
win=logical 1;
else
win=logial 0;
0 Kommentare
Antworten (2)
Davide Masiello
am 16 Mär. 2022
Bearbeitet: Davide Masiello
am 16 Mär. 2022
It could be something like this
numberGuess([3,7,12,9,17])
function numberGuess(input)
if length(input) ~= 5
error('Input must have five elements')
end
gameNum = randi(20,1,1);
if sum(abs(gameNum-input) <= 3) >=2
fprintf('The number to guess was %.0f. You win!\n',gameNum)
else
fprintf('The number to guess was %.0f. Sorry, try again.\n',gameNum)
end
end
0 Kommentare
Arif Hoq
am 16 Mär. 2022
trying to find out the nearest minimum and maximum value
userguess=[2 5 10 17 19];
gamenum=13;
y=NumberGuess(gamenum,userguess);
%
function win=NumberGuess(gamenum,userguess)
% userguess=[2 5 12 17 19];
% gamenum=3;
[~,~,idx]=unique(round(abs(userguess-gamenum)));
minVal=userguess(idx==1);
maxVal=userguess(idx==2);
if (gamenum-minVal)<=3
win=1
elseif (maxVal-gamenum)<=3
win=1
else
win=0
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!