Matlab Programming: Simple Guessing Game

AI Thread Summary
The discussion focuses on creating a simple guessing game in Matlab, where the user guesses a number between 1 and 100. The initial code successfully prompts for guesses and provides feedback on whether the guess is too high or too low. The main challenge discussed is implementing a feature that allows the game to restart based on user input after a correct guess. Suggestions include using a while loop to manage the game's continuation and utilizing integer values to represent boolean states in Matlab. Ultimately, the user successfully resolves their issue with the help of the provided suggestions.
_N3WTON_
Messages
350
Reaction score
3

Homework Statement


Write a program for a simple guessing game. The program should first prompt the user for a
number between 1 and 100. After each guess the program should inform the user whether the guess was too high or too low. When the user picks the correct number, inform them and let them know how many guesses it took. Also, after the correct number has been picked, ask them if they would like to play again. If they enter 'N' the program should terminate. If they enter 'Y' the program should run again, prompting the user to guess a new random number.

Homework Equations


None

The Attempt at a Solution


I have had no problem doing the first part of the program, this is my code thus far:
clc
TargetNum = randi(100);
UserGuess = 0;
GuessNumber = 0;

while ((UserGuess ~= TargetNum))
GuessNumber = GuessNumber + 1;
UserGuess = input('Guess an integer between 1 and 100 \n');
if (UserGuess < TargetNum)
fprintf('The number "%i" is too low \n', UserGuess)
elseif (UserGuess > TargetNum)
fprintf('The number "%i" is too high \n', UserGuess)
else
fprintf('You guessed the correct number: %i \n',TargetNum)
fprintf('This required "%i" guesses \n', GuessNumber)
end
end

However, I am having some trouble figuring out how to get the program to run again if the user decides to play again. I have experience using Java, so I am fairly knowledgeable when it comes to programming. However, in JAVA I would probably use a "Boolean" operator to get the program to run again, but I cannot do this in MatLab. I also decided to look up how to compare strings in MatLab using the "TF = strcmpi(string,string)" technique. However, I am unsure if this would work, or even how to go about doing it. Any advice on how to complete this program would be greatly appreciated. Thanks.
 
Physics news on Phys.org
Can't you use a while loop around your computation that runs if the user says yes? and for the first iteration you set the condition to yes.

If there's no boolean then use an integer value 0 = false and 1= true.
 
jedishrfu said:
Can't you use a while loop around your computation that runs if the user says yes? and for the first iteration you set the condition to yes.

If there's no boolean then use an integer value 0 = false and 1= true.
nvm
 
Last edited:
Ok I think I understand what you mean...I'm going to try it out and get back to you..
 
nvm
 
Last edited:
Ok, I got it too work thanks to your suggestion, thanks :)
 

Similar threads

Replies
10
Views
2K
Replies
6
Views
3K
Replies
1
Views
2K
Replies
15
Views
2K
Replies
4
Views
1K
Replies
7
Views
3K
Replies
3
Views
1K
Back
Top