(Solved) : Write Windows Form Application Program Plays Guess Number Follows Program Chooses Number G Q42690239 . . .

Write a windows form application program that plays “guess thenumber” as follows: Your program chooses the number to be guessedby selecting an int at random in the range 1–1000. The program thendisplays the following text in a label:
I have a number between 1 and 1000–can you guess mynumber?
Please enter your first guess.
A TextBox should be used to input the guess. As each guess isinput, the background color should change to red or blue. Redindicates that the user is getting “warmer,” blue that the user isgetting “colder.” A Label should display either “Too High” or “TooLow,” to help the user zero in on the correct answer. When the userguesses the correct answer, display “Correct!” in a message box,change the Form’s background color to green and disable theTextBox. Recall that a TextBox (like other controls) can bedisabled by setting the control’s Enabled property to false.Provide a Button that allows the user to play the game again. Whenthe Button is clicked, generate a new random number, change thebackground to the default color and enable the TextBox.
This is the code from the GuessForm.cs file. You’ll need toput the controls on the form, and rename them. Then you shouldenter this code in the various events. There are two Procedureshere: CheckGuess() and GenerateSecret().
// Exercise 14.8 Solution: GuessForm.cs// Demonstrating a guessing game that uses GUI.using System;using System.Drawing;using System.Windows.Forms;
namespace Guess{public partial class GuessForm : Form{int secret = 0;int lastGuess = 0;bool firstGuess = true;
// constructorpublic GuessForm(){InitializeComponent();} // end constructor
// randomly generate secret number (1-1000)private void GenerateSecret(){Random randomNumber = new Random();secret = randomNumber.Next( 1000 ) + 1;} // end method GenerateSecret
private int CheckGuess( int user ){if ( user > secret ) // too highreturn 1;if ( user < secret ) // too lowreturn -1;else // correctreturn 0;} // end method CheckGuess
private void inputTextBox_KeyDown( object sender, KeyEventArgse ){// allow user to press enter in textboxif ( e.KeyCode == Keys.Enter ){int userGuess = Convert.ToInt32( inputTextBox.Text );
// check if user is warmer or colderif ( !firstGuess ){if ( Math.Abs( userGuess – secret ) <Math.Abs( lastGuess – secret ) )BackColor = Color.Red;elseBackColor = Color.LightBlue;}
firstGuess = false;
// CheckGuess returns 0 if correct, -1 if too low,// 1 if too highint rightOrWrong = CheckGuess( userGuess );
// if guess is right allow user to play againif ( rightOrWrong == 0 ){outputLabel.Text = “Correct!”;newGameButton.Enabled = true;inputTextBox.ReadOnly = true;BackColor = Color.LightGreen;firstGuess = true;} // end ifelse if ( rightOrWrong == -1 )outputLabel.Text = “Too Low!”;elseoutputLabel.Text = “Too High!”;
// clear guessinputTextBox.SelectAll();lastGuess = userGuess;} // end if} // end method inputTextBox_KeyDown
private void newGameButton_Click( object sender, EventArgs e){outputLabel.Text = “New Game Started!”;
GenerateSecret();
newGameButton.Enabled = false;inputTextBox.ReadOnly = false;BackColor = Color.FromName( “Control” );} // end method newGameButton_Click} // end class GuessForm} // end namespace Guess
image.pngGuessing Game - O X I have a number between 1 and 1000--can you guess my number? Please enter a guess. New Game New Game StarWe were unable to transcribe this imageGuessing Game – O X I have a number between 1 and 1000–can you guess my number? Please enter a guess. New Game New Game Started! . Guessing Game – O X I have a number between 1 and 1000–can you guess my number? Please enter a guess. 50 New Game Too Low! Guessing Game – O X I have a number between 1 and 1000–can you guess my number? Please enter a guess 500 New Game Too High! Show transcribed image text
Guessing Game – O X I have a number between 1 and 1000–can you guess my number? Please enter a guess. New Game New Game Started! . Guessing Game – O X I have a number between 1 and 1000–can you guess my number? Please enter a guess. 50 New Game Too Low! Guessing Game – O X I have a number between 1 and 1000–can you guess my number? Please enter a guess 500 New Game Too High!

Expert Answer


Answer to Write a windows form application program that plays “guess the number” as follows: Your program chooses the number t…

Leave a Comment

About

We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?