Question
Part B: Number Guessing Game
Requirements
Write a Windows application that randomly generates a number from 0 through 100. Prompt the user to guess the number. Let the user know if the guess is too low, too high, or is the correct number. Give the user another chance to guess the number. The user keeps guessing until he or she gets it correct.
Sample output:
I entered a number and pressed the button.
I pressed the button and the box and message are cleared.
I entered a number and pressed the button.
After entering several numbers, I got the following:
The following message is displayed when the guess is right.
Tips
Pseudocode:
Form constructor method
Evaluate Button Click Event Handler Method
Try Again Button Click Event handler
Part B: Number Guessing Game
Requirements
Write a Windows application that randomly generates a number from 0 through 100. Prompt the user to guess the number. Let the user know if the guess is too low, too high, or is the correct number. Give the user another chance to guess the number. The user keeps guessing until he or she gets it correct.
Sample output:
I entered a number and pressed the button.
I pressed the button and the box and message are cleared.
I entered a number and pressed the button.
After entering several numbers, I got the following:
The following message is displayed when the guess is right.
Tips
Pseudocode:
Form constructor method
- Randomly pick a target number between 0 and 100
Evaluate Button Click Event Handler Method
- If the guess text box is not blank
- Pull the guess from the text box
- Add 1 to the number of guesses
- If the guess is less than the target
- Make the Evaluate button invisible
- Change the label message to "Too Low!!"
- Make the label message visible
- Change the background color of the form to LightSeaGreen
- Make the Try Again button visible
- Else if the guess is greater than the target
- Make the Evaluate button invisible
- Change the label message to "Too High!!"
- Make the label message visible
- Change the background color of the form to SlateBlue
- Make the Try Again button visible
- Else
- Display a message indicating the user guessed correctly and display the number of guesses it took.
- End-If
- End-If
Try Again Button Click Event handler
- Clear the guess text box
- Make the label message and Try Again buttons invisible
- Make the Evaluate button visible
Explanation / Answer
namespace ilab06b { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); Random number = gcnew Random(); // This is declaring number; our randomizer int rn; // This is declaring rn; our randomized number int gn; // This is declaring gn; our guessed number rn = number.Next(1,10); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Label^ label2; private: System::Windows::Forms::Label^ label3; private: /// /// Required designer variable. /// System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label2 = (gcnew System::Windows::Forms::Label()); this->label3 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(66, 170); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(181, 35); this->button1->TabIndex = 0; this->button1->Text = L"Press to EVALUATE your guess"; this->button1->UseVisualStyleBackColor = true; // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(63, 59); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(134, 13); this->label1->TabIndex = 1; this->label1->Text = L"Enter a guess from 0 - 100:"; // // textBox1 // this->textBox1->Location = System::Drawing::Point(203, 56); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(44, 20); this->textBox1->TabIndex = 2; // // label2 // this->label2->AutoSize = true; this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->label2->Location = System::Drawing::Point(91, 112); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(106, 25); this->label2->TabIndex = 3; this->label2->Text = L"Too Low!!"; this->label2->Visible = false; // // label3 // this->label3->AutoSize = true; this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->label3->Location = System::Drawing::Point(91, 112); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(111, 25); this->label3->TabIndex = 4; this->label3->Text = L"Too High!!"; this->label3->Visible = false; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::SystemColors::ActiveCaption; this->ClientSize = System::Drawing::Size(292, 266); this->Controls->Add(this->label3); this->Controls->Add(this->label2); this->Controls->Add(this->textBox1); this->Controls->Add(this->label1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Guessing Game"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; }