In: Computer Science
Video games are rather complicated to program, not least of which because of the graphics work that needs to be completed to finish a video game. Still, even relatively simple games have historically had a chance of becoming popular (e.g. Tetris®). Since you are learning to program for the first time, let's look at a text-only game.
Write a program that has the computer generate a pseudorandom integer between -100 and +100, and ask the user to guess what the number is. To generate the pseudorandom number, research and use the randi command. If the user's guess is higher than the computer-generated pseudorandom number, print a statement to that effect. If the user's guess is lower than the computer-generated pseudorandom number, print a statement to that effect. Keep track of how many guesses it takes for the user to guess the right number, and print that information to the screen when the program terminates. Do not re-initialize the computer-generated pseudorandom number between iterations, otherwise the user will have a hard time trying to guess the computer-generated pseudorandom number.
Validate the user's input; if the user enters a number greater than 100 or less than -100, prompt the user to enter a number within the guess range. Do not count guesses out of range (i.e. greater than 100 or less than -100) as an iteration. Do not concern yourself with testing if the user entered non-numeric input, and assume that the user will enter an integer value. If the user enters the value inf, terminate the program (though count that iteration as a valid iteration for the purposes of seeing how many times the program iterated).
Use no more than two while loops to solve this problem, and emulate the output format in these two sample runs:
Sample Run #1 (with a computer-generated value of 9):
Enter your guess:
8
Sorry, your guess was too low. Please try again.
Enter your guess:
10
Sorry, your guess was too high. Please try again.
Enter your guess: 9
You guessed the correct value!
The correct value was
9.
The program iterated 3 times.
Sample Run #2 (with a computer-generated value of -3):
Enter your guess:
100
Sorry, your guess was too high. Please try again.
Enter your guess:
-100
Sorry, your guess was too low. Please try again.
Enter your guess: inf
You asked to terminate the program.
The correct value was
-3.
The program iterated 3 times.
% Generate a random value
computer_value = randi([-100 100]);
% Variable to keep track of iterations
iterations = 0;
while(1)
% Ask user to make a guess
user_value = input('Enter your guess: ');
% Validate user input, if it's not between -100,100 and not equal
to "inf" ask to enter again
while((user_value < -100 || user_value > 100) &&
(user_value) != str2num("inf"))
fprintf('Sorry, guess within the range [-100, 100].\n');
user_value = input('Enter your guess: ');
end
% Increment iterations by 1
iterations = iterations + 1;
% If user_value is inf, print and exit the loop
% If user_value < computer_value, print its low
% If user_value > computer_value, print its high
% If user_value = computer_value, print guess is correct and exit
the loop
if (user_value) == str2num("inf")
fprintf('You asked to terminate the program.\n');
break
elseif user_value < computer_value
fprintf('Sorry, your guess was too low. Please try
again.\n');
elseif user_value > computer_value
fprintf('Sorry, your guess was too high. Please try
again.\n');
else
fprintf('You guessed the correct value!\n');
break;
end
end
% Print correct value
fprintf('The correct value was %d.\n',computer_value);
% Print number of iterations
fprintf('The program iterated %d times.\n',iterations);
SCREENSHOT
OUTPUT