Count the number of guesses made by player

4 vues (au cours des 30 derniers jours)
Chris
Chris le 27 Juil 2022
Commenté : Chris le 27 Juil 2022
I am making a game where a payer has to guess the random number between 1-6. I now want to count the number of guesses and display this information at the end when the player guesses the correct number. How can i do this?
clear
X=randi(6);
while (1)
prompt = 'Guess the random number between 1 and 6 :)';
n = input(prompt);
if n~=X
disp('Guess again');
else
disp('Correct! Well done :)')
break;
end

Réponse acceptée

Steven Lord
Steven Lord le 27 Juil 2022
Define a variable that records how many guesses the player has made.
Each time the player makes a guess, add 1 to that variable.
At the end display the contents of that variable.
  1 commentaire
Chris
Chris le 27 Juil 2022
Thanks, got it working!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by