105k views
0 votes
Write a GUI-based program that plays a guess-the-number game in which the roles of the computer and the user are the reverse of what they are in the Case Study of this chapter. In this version of the game, the computer guesses a number between 1 (lowerBound) and 100 (upperBound) and the user provides the responses. The window should display the

1 Answer

3 votes

Answer:

In C#

See attachment 1 for program interface

See attachment 2 for complete program source code

Step-by-step explanation:

First, define a function to generate random numbers

int cguess; ---> This declares computer guess as integer

void randomGen() {

Clear both text boxes -- for the computer and the user

textBox1.Text = string.Empty;

textBox2.Text = string.Empty;

Generate a new random number

Random rnd = new Random();

cguess = rnd.Next(1, 101); }

Write the following code segment in the submit button click event

If user guess equals computer guess

if(Convert.ToInt32(textBox2.Text) == cguess) {

Display the computer guess

textBox1.Text = cguess.ToString();

Display a congratulatory message

MessageBox.Show("Congrats!!\\Proceed to new game");

Call the randomGen function

randomGen(); }

If otherwise, print too big or too small, as the case may be

else if (Convert.ToInt32(textBox2.Text) > cguess)

{ MessageBox.Show("Too big")}

else { MessageBox.Show("Too small"); }

Write the following in the form load event. So that the computer starts the game immediately the form loads

randomGen();

Write the following in the new game click event. So that the computer starts begins a new game immediately the new game button is clicked

randomGen();

Write a GUI-based program that plays a guess-the-number game in which the roles of-example-1
User Keno Fischer
by
3.0k points