Matlab Programming: Simple Guessing Game

In summary, the program is a simple guessing game that prompts the user for a number between 1 and 100 and informs them if their guess is too high or too low. It also keeps track of the number of guesses and asks the user if they would like to play again. The program uses a while loop and an integer value to run again if the user chooses to play again.
  • #1
_N3WTON_
351
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
  • #2
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.
 
  • #3
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:
  • #4
Ok I think I understand what you mean...I'm going to try it out and get back to you..
 
  • #5
nvm
 
Last edited:
  • #6
Ok, I got it too work thanks to your suggestion, thanks :)
 

FAQ: Matlab Programming: Simple Guessing Game

1. What is Matlab programming?

Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows for data analysis, modeling, and visualization, making it useful for a wide range of fields.

2. What is a simple guessing game in Matlab programming?

A simple guessing game in Matlab involves creating a program that generates a random number and prompts the user to guess it. The user's guess is then compared to the random number and the program gives feedback on whether the guess was too high or too low. The game continues until the user guesses the correct number.

3. How do I create a simple guessing game in Matlab programming?

To create a simple guessing game in Matlab, you will need to use functions such as randi to generate a random number, input to prompt the user for their guess, and if/else statements to compare the guess to the random number and give feedback. You can also use loops to allow for multiple guesses.

4. Can I customize the simple guessing game in Matlab programming?

Yes, you can customize the simple guessing game in Matlab by adding additional features such as setting a maximum number of guesses, keeping track of the number of attempts, or allowing the user to choose the range of numbers to guess from. You can also add graphics or sound effects to enhance the game.

5. Is Matlab the best language for creating a simple guessing game?

While Matlab can be used to create a simple guessing game, it may not be the best language for this specific task. Other languages such as Python or Java may be better suited for creating games as they have more built-in features and libraries for graphics and user interaction. However, Matlab can still be a good option for creating a simple guessing game, especially if you are already familiar with the language.

Similar threads

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