Question

In: Computer Science

Video games are rather complicated to program, not least of which because of the graphics work...

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.

Solutions

Expert Solution

% 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


Related Solutions

The Wisconsin state legislature enacts a statute that prohibits the advertising of video games “because the...
The Wisconsin state legislature enacts a statute that prohibits the advertising of video games “because the games might be harmful to minors.” Despite this new statute, the president of xGamez, Inc., orders xGamez marketers to place ads in various media. When an xGamez ad appears on YUTV, a local television station, xGamez and YUTV are charged with violating the statute. What is the defendants’ best defense against a conviction?
Describe Ethical, Social, and Global issues that relate to violent video games. List at least 3...
Describe Ethical, Social, and Global issues that relate to violent video games. List at least 3 of them Ethical: *List 1 here *List 2 here *List 3 here Social: *List 1 here *List 2 here *List 3 here Global: *List 1 here *List 2 here *List 3 here
Aluminum can be analyze by the following rather complicated procedure. The procedure features 8-hydroxyquinoline, C9H7ON, which...
Aluminum can be analyze by the following rather complicated procedure. The procedure features 8-hydroxyquinoline, C9H7ON, which reacts with Al3+ to form a solid and reacts with Br2to form Br2C9H5ON. We will abbreviate the formula of 8-hydroxyquinoline as H2L so we don't have to keep writing C9H7ON. Al3+ reacts with H2L at pH 5 in reaction (1) to form a solid. The solid can be filtered and rinsed to give pure Al(H2L)3. In more acidic solution, the reaction goes in the...
Parker Corp. develops computer video games for sale. A new development project which began in 2018...
Parker Corp. develops computer video games for sale. A new development project which began in 2018 reached technological feasibility at the end of Sept. 2019 and the project was available for release to customers early in 2020. Development costs incurred prior to Sept. 30 were $1,600,000 and costs incurred from Oct. 1 to product availability were $1,200,000. Revenues in 2020 from the sale of the new product were $4,000,000 and the company anticipates another $12,000,000 in revenues. The economic life...
Assignment Write a program using turtle graphics which writes your initials, or any other three unique...
Assignment Write a program using turtle graphics which writes your initials, or any other three unique letters, to the display. Look to your program for lab 1 for reminders of how to use turtle graphics. Functions that must be written: def drawLetter (x, y, letterColor): Write three functions, one for three unique letters. Personally, I would write drawA, drawR, and drawS. Each function must draw that letter to the screen. The x and y values determine the upper left-hand location...
1) Which of the following would increase GDP? a. More people walk to work rather than...
1) Which of the following would increase GDP? a. More people walk to work rather than drive cars. b. Consumers in rural areas switch from buying home heating oil to burning wood they collect on their own land. c. Farm families grow more for themselves than for the market. d. Neither the price nor the quantity of television sets changes, but the quality of sets improves. e. The amount spent on vacuum cleaners does not change, but quality decreases so...
  Which type of technology has been proven to work, but they offer an advantage because...
  Which type of technology has been proven to work, but they offer an advantage because not all companies knows about them or uses them?   Pacing Emerging  Key Base Elevated     This type of technology has not yet proven their worth in the marketplace, but they have the potential to change the rules of competition.   pacing  base large-batch emerging key
1. Which type of firm would you rather work for? One who lays people off, or...
1. Which type of firm would you rather work for? One who lays people off, or one that cuts everyone’s hours? Why? Think about the tradeoffs involved. Please use marginal analysis in your reasoning, and back up any claims you make with some documentation. 2. If a law was passed that required all firms to cut hours instead of lay people off, so that everybody was guaranteed a job and only hours worked fluctuated (instead of experiencing joblessness), what do...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
How do you exhibit professionalism in your daily work?  Describe at least one situation in which you...
How do you exhibit professionalism in your daily work?  Describe at least one situation in which you demonstrated professionalism in nursing, explaining how your behavior in the situation is different than it would have been without your additional education from this RN to BSN program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT