Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Guess a Number

#pragma once

 

namespace GuessANumber

{

            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>

            /// 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.

            /// </summary>

            public ref class Form1 : public System::Windows::Forms::Form

            {

            public:

                        Form1(void)

                        {

                                    InitializeComponent();

                                    //

                                    //TODO: Add the constructor code here

                                    //

                        }

 

            protected:

                        /// <summary>

                        /// Clean up any resources being used.

                        /// </summary>

                        /// <param name="disposing">"description of the parameter"</param>

                       

            private: System::Windows::Forms::GroupBox^  grpRange;

            private: System::Windows::Forms::RadioButton^  rbnControl1000;

            protected:

 

            protected:

 

            private: System::Windows::Forms::RadioButton^  rbnControl100;

 

            private: System::Windows::Forms::RadioButton^  rbnControl10;

 

            private: System::Windows::Forms::GroupBox^  grpScore;

            private: System::Windows::Forms::CheckBox^  chkVerbose;

            private: System::Windows::Forms::Button^  btnDefaults;

            private: System::Windows::Forms::Label^  lblFeedback;

 

 

 

            private: System::Windows::Forms::Label^  lblInstructions;

 

            private: System::Windows::Forms::Button^  btnReset;

            private: System::Windows::Forms::TextBox^  txtGamesWon;

 

 

            private: System::Windows::Forms::Label^  lblGamesWon;

 

            private: System::Windows::Forms::Button^  btnNewGame;

 

            private: System::Windows::Forms::Button^  btnCheckGuess;

            private: System::Windows::Forms::TextBox^  txtOutput;

 

 

            private: System::Windows::Forms::TextBox^  txtInput;

            private: System::Windows::Forms::StatusStrip^  stsControl;

            private: System::Windows::Forms::ToolStripStatusLabel^  stsLabel;

 

 

 

 

 

            private:

                        /// <summary>

                        /// Required designer variable.

                        /// </summary>

                        Int16 intRandomNumber;

                        Int16 intTextGamesWon;

                        System::ComponentModel::Container ^components;

 

#pragma region Windows Form Designer generated code

                        /// <summary>

                        /// Required method for Designer support - do not modify

                        /// the contents of this method with the code editor.

                        /// </summary>

                        void InitializeComponent(void)

                        {

                                    this->grpRange = (gcnew System::Windows::Forms::GroupBox());

                                    this->btnDefaults = (gcnew System::Windows::Forms::Button());

                                    this->chkVerbose = (gcnew System::Windows::Forms::CheckBox());

                                    this->rbnControl1000 = (gcnew System::Windows::Forms::RadioButton());

                                    this->rbnControl100 = (gcnew System::Windows::Forms::RadioButton());

                                    this->rbnControl10 = (gcnew System::Windows::Forms::RadioButton());

                                    this->grpScore = (gcnew System::Windows::Forms::GroupBox());

                                    this->btnNewGame = (gcnew System::Windows::Forms::Button());

                                    this->btnCheckGuess = (gcnew System::Windows::Forms::Button());

                                    this->txtOutput = (gcnew System::Windows::Forms::TextBox());

                                    this->txtInput = (gcnew System::Windows::Forms::TextBox());

                                    this->lblFeedback = (gcnew System::Windows::Forms::Label());

                                    this->lblInstructions = (gcnew System::Windows::Forms::Label());

                                    this->btnReset = (gcnew System::Windows::Forms::Button());

                                    this->txtGamesWon = (gcnew System::Windows::Forms::TextBox());

                                    this->lblGamesWon = (gcnew System::Windows::Forms::Label());

                                    this->stsControl = (gcnew System::Windows::Forms::StatusStrip());

                                    this->stsLabel = (gcnew System::Windows::Forms::ToolStripStatusLabel());

                                    this->grpRange->SuspendLayout();

                                    this->grpScore->SuspendLayout();

                                    this->stsControl->SuspendLayout();

                                    this->SuspendLayout();

                                    //

                                    // grpRange

                                    //

                                    this->grpRange->Controls->Add(this->btnDefaults);

                                    this->grpRange->Controls->Add(this->chkVerbose);

                                    this->grpRange->Controls->Add(this->rbnControl1000);

                                    this->grpRange->Controls->Add(this->rbnControl100);

                                    this->grpRange->Controls->Add(this->rbnControl10);

                                    this->grpRange->Location = System::Drawing::Point(12, 12);

                                    this->grpRange->Name = L"grpRange";

                                    this->grpRange->Size = System::Drawing::Size(306, 94);

                                    this->grpRange->TabIndex = 0;

                                    this->grpRange->TabStop = false;

                                    this->grpRange->Text = L"Select A Range";

                                    //

                                    // btnDefaults

                                    //

                                    this->btnDefaults->AutoEllipsis = true;

                                    this->btnDefaults->Location = System::Drawing::Point(179, 20);

                                    this->btnDefaults->Name = L"btnDefaults";

                                    this->btnDefaults->Size = System::Drawing::Size(110, 23);

                                    this->btnDefaults->TabIndex = 4;

                                    this->btnDefaults->Text = L"Reset Defaults";

                                    this->btnDefaults->Click += gcnew System::EventHandler(this, &Form1::btnDefaults_Click);

                                    //

                                    // chkVerbose

                                    //

                                    this->chkVerbose->AutoSize = true;

                                    this->chkVerbose->Checked = true;

                                    this->chkVerbose->CheckState = System::Windows::Forms::CheckState::Checked;

                                    this->chkVerbose->Location = System::Drawing::Point(179, 68);

                                    this->chkVerbose->Name = L"chkVerbose";

                                    this->chkVerbose->Size = System::Drawing::Size(119, 17);

                                    this->chkVerbose->TabIndex = 3;

                                    this->chkVerbose->Text = L"Verbose Messaging";

                                    this->chkVerbose->CheckedChanged += gcnew System::EventHandler(this, &Form1::chkVerbose_CheckedChanged);

                                    //

                                    // rbnControl1000

                                    //

                                    this->rbnControl1000->AutoSize = true;

                                    this->rbnControl1000->Location = System::Drawing::Point(7, 68);

                                    this->rbnControl1000->Name = L"rbnControl1000";

                                    this->rbnControl1000->Size = System::Drawing::Size(108, 17);

                                    this->rbnControl1000->TabIndex = 2;

                                    this->rbnControl1000->Text = L"Range: 1 to 1000";

                                    //

                                    // rbnControl100

                                    //

                                    this->rbnControl100->AutoSize = true;

                                    this->rbnControl100->Checked = true;

                                    this->rbnControl100->Location = System::Drawing::Point(7, 44);

                                    this->rbnControl100->Name = L"rbnControl100";

                                    this->rbnControl100->Size = System::Drawing::Size(102, 17);

                                    this->rbnControl100->TabIndex = 1;

                                    this->rbnControl100->TabStop = true;

                                    this->rbnControl100->Text = L"Range: 1 to 100";

                                    //

                                    // rbnControl10

                                    //

                                    this->rbnControl10->AutoSize = true;

                                    this->rbnControl10->Location = System::Drawing::Point(7, 20);

                                    this->rbnControl10->Name = L"rbnControl10";

                                    this->rbnControl10->Size = System::Drawing::Size(96, 17);

                                    this->rbnControl10->TabIndex = 0;

                                    this->rbnControl10->Text = L"Range: 1 to 10";

                                    //

                                    // grpScore

                                    //

                                    this->grpScore->Controls->Add(this->btnNewGame);

                                    this->grpScore->Controls->Add(this->btnCheckGuess);

                                    this->grpScore->Controls->Add(this->txtOutput);

                                    this->grpScore->Controls->Add(this->txtInput);

                                    this->grpScore->Controls->Add(this->lblFeedback);

                                    this->grpScore->Controls->Add(this->lblInstructions);

                                    this->grpScore->Controls->Add(this->btnReset);

                                    this->grpScore->Controls->Add(this->txtGamesWon);

                                    this->grpScore->Controls->Add(this->lblGamesWon);

                                    this->grpScore->Location = System::Drawing::Point(12, 113);

                                    this->grpScore->Name = L"grpScore";

                                    this->grpScore->Size = System::Drawing::Size(306, 255);

                                    this->grpScore->TabIndex = 1;

                                    this->grpScore->TabStop = false;

                                    this->grpScore->Text = L"Score";

                                    //

                                    // btnNewGame

                                    //

                                    this->btnNewGame->Location = System::Drawing::Point(225, 226);

                                    this->btnNewGame->Name = L"btnNewGame";

                                    this->btnNewGame->Size = System::Drawing::Size(75, 23);

                                    this->btnNewGame->TabIndex = 8;

                                    this->btnNewGame->Text = L"New Game";

                                    this->btnNewGame->Click += gcnew System::EventHandler(this, &Form1::btnNewGame_Click);

                                    //

                                    // btnCheckGuess

                                    //

                                    this->btnCheckGuess->Enabled = false;

                                    this->btnCheckGuess->Location = System::Drawing::Point(116, 106);

                                    this->btnCheckGuess->Name = L"btnCheckGuess";

                                    this->btnCheckGuess->Size = System::Drawing::Size(84, 23);

                                    this->btnCheckGuess->TabIndex = 7;

                                    this->btnCheckGuess->Text = L"Check Guess";

                                    this->btnCheckGuess->Click += gcnew System::EventHandler(this, &Form1::btnCheckGuess_Click);

                                    //

                                    // txtOutput

                                    //

                                    this->txtOutput->Location = System::Drawing::Point(9, 161);

                                    this->txtOutput->Multiline = true;

                                    this->txtOutput->Name = L"txtOutput";

                                    this->txtOutput->ReadOnly = true;

                                    this->txtOutput->Size = System::Drawing::Size(289, 59);

                                    this->txtOutput->TabIndex = 6;

                                    this->txtOutput->TabStop = false;

                                    //

                                    // txtInput

                                    //

                                    this->txtInput->Enabled = false;

                                    this->txtInput->Location = System::Drawing::Point(88, 80);

                                    this->txtInput->Name = L"txtInput";

                                    this->txtInput->Size = System::Drawing::Size(130, 20);

                                    this->txtInput->TabIndex = 5;

                                    this->txtInput->TextChanged += gcnew System::EventHandler(this, &Form1::txtInput_TextChanged);

                                    //

                                    // lblFeedback

                                    //

                                    this->lblFeedback->AutoSize = true;

                                    this->lblFeedback->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,

                                                static_cast<System::Byte>(0)));

                                    this->lblFeedback->Location = System::Drawing::Point(88, 145);

                                    this->lblFeedback->Name = L"lblFeedback";

                                    this->lblFeedback->Size = System::Drawing::Size(134, 13);

                                    this->lblFeedback->TabIndex = 4;

                                    this->lblFeedback->Text = L"Feedback and Results";

                                    //

                                    // lblInstructions

                                    //

                                    this->lblInstructions->AutoSize = true;

                                    this->lblInstructions->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,

                                                static_cast<System::Byte>(0)));

                                    this->lblInstructions->Location = System::Drawing::Point(89, 61);

                                    this->lblInstructions->Name = L"lblInstructions";

                                    this->lblInstructions->Size = System::Drawing::Size(142, 17);

                                    this->lblInstructions->TabIndex = 3;

                                    this->lblInstructions->Text = L"Enter Your Guess:";

                                    //

                                    // btnReset

                                    //

                                    this->btnReset->Location = System::Drawing::Point(6, 226);

                                    this->btnReset->Name = L"btnReset";

                                    this->btnReset->Size = System::Drawing::Size(75, 23);

                                    this->btnReset->TabIndex = 2;

                                    this->btnReset->Text = L"Reset Score";

                                    this->btnReset->Click += gcnew System::EventHandler(this, &Form1::btnReset_Click);

                                    //

                                    // txtGamesWon

                                    //

                                    this->txtGamesWon->Location = System::Drawing::Point(163, 24);

                                    this->txtGamesWon->Name = L"txtGamesWon";

                                    this->txtGamesWon->ReadOnly = true;

                                    this->txtGamesWon->Size = System::Drawing::Size(100, 20);

                                    this->txtGamesWon->TabIndex = 1;

                                    this->txtGamesWon->TabStop = false;

                                    //

                                    // lblGamesWon

                                    //

                                    this->lblGamesWon->AutoSize = true;

                                    this->lblGamesWon->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,

                                                static_cast<System::Byte>(0)));

                                    this->lblGamesWon->Location = System::Drawing::Point(43, 27);

                                    this->lblGamesWon->Name = L"lblGamesWon";

                                    this->lblGamesWon->Size = System::Drawing::Size(118, 13);

                                    this->lblGamesWon->TabIndex = 0;

                                    this->lblGamesWon->Text = L"No. of Games Won:";

                                    //

                                    // stsControl

                                    //

                                    this->stsControl->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->stsLabel});

                                    this->stsControl->Location = System::Drawing::Point(0, 372);

                                    this->stsControl->Name = L"stsControl";

                                    this->stsControl->Size = System::Drawing::Size(330, 22);

                                    this->stsControl->SizingGrip = false;

                                    this->stsControl->TabIndex = 2;

                                    this->stsControl->Text = L"statusStrip1";

                                    //

                                    // stsLabel

                                    //

                                    this->stsLabel->Name = L"stsLabel";

                                    this->stsLabel->Size = System::Drawing::Size(72, 17);

                                    this->stsLabel->Text = L"Game Ready!";

                                    //

                                    // Form1

                                    //

                                    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

                                    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

                                    this->ClientSize = System::Drawing::Size(330, 394);

                                    this->Controls->Add(this->grpScore);

                                    this->Controls->Add(this->grpRange);

                                    this->Controls->Add(this->stsControl);

                                    this->Cursor = System::Windows::Forms::Cursors::Hand;

                                    this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;

                                    this->Name = L"Form1";

                                    this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;

                                    this->Text = L"Guess A Number";

                                    this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);

                                    this->grpRange->ResumeLayout(false);

                                    this->grpRange->PerformLayout();

                                    this->grpScore->ResumeLayout(false);

                                    this->grpScore->PerformLayout();

                                    this->stsControl->ResumeLayout(false);

                                    this->stsControl->PerformLayout();

                                    this->ResumeLayout(false);

                                    this->PerformLayout();

 

                        }

#pragma endregion

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

                                     //Clear game variables and text

                                     intRandomNumber = 0;

                                     intTextGamesWon = 0;

                                     txtGamesWon->Text = intTextGamesWon.ToString();

                                     //Set focus on new game button

                                     btnNewGame->Focus();

                         }

private: System::Void btnDefaults_Click(System::Object^  sender, System::EventArgs^  e) {

                                     //Reset all game defaults

                                     rbnControl100->Checked = true;

                                     chkVerbose->Checked = true;

                                     //Set focus on text input

                                     txtInput->Focus();

                         }

private: System::Void btnReset_Click(System::Object^  sender, System::EventArgs^  e) {

                                     //Reset the game score

                                     txtGamesWon->Text = "0";

                         }

private: System::Void txtInput_TextChanged(System::Object^  sender, System::EventArgs^  e) {

                                     //Toggle appropriate buttons when

                                     //  text is being entered for the

                                     //  guess

                                     btnCheckGuess->Enabled = true;

                                     btnNewGame->Enabled = false;

                         }

private: Void GenerateRandomNumber( Void ) {

                                    //Get a random number based on the

                                    //  current time

                                    DateTime moment = DateTime::Now;

                                    Random^ randNumGen = gcnew Random( moment.Millisecond );

                                    Int32 intUpperLimit = 0;

                                    //Check to see which radio button is

                                    //  checked in order to determine

                                    //  upper limit of random number

                                    if( rbnControl10->Checked == true )

                                                intUpperLimit = 10;

                                    if( rbnControl100->Checked == true )

                                                intUpperLimit = 100;

                                    if( rbnControl1000->Checked == true )

                                                intUpperLimit = 1000;

                                    //Get the number based on the upper range

                                    intRandomNumber = randNumGen->Next( intUpperLimit );

                         }

private: System::Void btnCheckGuess_Click(System::Object^  sender, System::EventArgs^  e) {

                                     //Check the player's guesses

 

                                     //Declare variable to store current guess

                                     Double dblNoOfGuesses = 0;

                                     //Declare variable to continuously keep

                                     //  track of player guesses

                                     static Int32 intNoOfGuesses = 0;

 

                                     //First, check to see if text has been entered

                                     if( txtInput->Text->Length > 0 )

                                     {

                                                 //Try to convert the number and store it in

                                                 //  the dblNoOfGuesses variable; if the player

                                                 //  accidently enters text or a fraction,

                                                 //  the Double::TryParse method will fail

                                                 //  gracefully

                                                 if( Double::TryParse( txtInput->Text, dblNoOfGuesses ) )

                                                 {

                                                             //Enable button

                                                             btnCheckGuess->Enabled = true;

                                                             //Check if player guess equals number

                                                             if( dblNoOfGuesses == intRandomNumber )

                                                             {

                                                                         

                                                                         txtInput->Text = "";

                                                                         intNoOfGuesses += 1;

                                                                         //Check if verbose messaging is on

                                                                         if( chkVerbose->Checked == true )

                                                                         {

                                                                                     //Create the message string

                                                                                     txtOutput->Text = \

                                                                                     String::Concat( "Congratulations! "

                                                                                     "You've won the Guess A Number Game! "

                                                                                     "Number of guesses made = ",

                                                                                     intNoOfGuesses.ToString() );

                                                                         }

                                                                         else

                                                                                     txtOutput->Text = \

                                                                                     "Congratulations!";

                                                                         //Reset number of guesses

                                                                         intNoOfGuesses = 0;

                                                                         //Disable text input

                                                                         txtInput->Enabled = false;

                                                                         //Update number of games won

                                                                         intTextGamesWon += 1;

                                                                         txtGamesWon->Text = \

                                                                                     intTextGamesWon.ToString();

                                                                         //Enable the New Game button

                                                                         btnNewGame->Enabled = true;

                                                                         //Disable the check guess button

                                                                         btnCheckGuess->Enabled = false;

                                                                         //Enable all of the radio buttons

                                                                         rbnControl10->Enabled = true;

                                                                         rbnControl100->Enabled = true;

                                                                         rbnControl1000->Enabled = true;

                                                                         //Enable the two reset buttons

                                                                         btnDefaults->Enabled = true;

                                                                         btnReset->Enabled = true;

                                                                         stsLabel->Text = "Get Ready!";

                                                                         

                                                             }

                                                             //Is player guess less than number?

                                                             if( dblNoOfGuesses < intRandomNumber )

                                                             {

                                                                         //Clear text

                                                                         txtInput->Text = "";

                                                                         //Increase guess count

                                                                         intNoOfGuesses += 1;

                                                                         //Check if verbose messaging is on

                                                                         if( chkVerbose->Checked == true )

                                                                         {

                                                                                     //Create the message string

                                                                                     txtOutput->Text = \

                                                                                     String::Concat( "The number that "

                                                                                     "you entered was too low. "

                                                                                     "Please enter a higher number "

                                                                                     "and try again. "

                                                                                     "Number of guesses made = ",

                                                                                     intNoOfGuesses.ToString() );

                                                                         }

                                                                         else

                                                                                     txtOutput->Text = \

                                                                                     "Too low.";

                                                             }

                                                             //Is player guess greater than number

                                                             if( dblNoOfGuesses > intRandomNumber )

                                                             {

                                                                         //Clear text

                                                                         txtInput->Text = "";

                                                                         //Increase guess count

                                                                         intNoOfGuesses += 1;

                                                                         //Check if verbose messaging is on

                                                                         if( chkVerbose->Checked == true )

                                                                         {

                                                                                     //Create the message string

                                                                                     txtOutput->Text = \

                                                                                     String::Concat( "The number that "

                                                                                     "you entered was too high. "

                                                                                     "Please enter a lower number "

                                                                                     "and try again. "

                                                                                     "Number of guesses made = ",

                                                                                     intNoOfGuesses.ToString() );

                                                                         }

                                                                         else

                                                                                     txtOutput->Text = \

                                                                                     "Too high.";

                                                             

                                                             }

                                                 }

                                                 else

                                                 {

                                                            if( chkVerbose->Checked == true )

                                                            {

                                                                        //Create the message string

                                                                        txtOutput->Text = \

                                                                        String::Concat( "Sorry, you "

                                                                        "entered a non-numeric guess. "

                                                                        "Please enter a number and "

                                                                        "try again. " );

                                                            }

                                                            else

                                                                        txtOutput->Text = \

                                                                        "Numeric input required.";

                                                                       

                                                 }

                                     }

                                     else

                                     {

                                                if( chkVerbose->Checked == true )

                                                {

                                                            //Create the message string

                                                            txtOutput->Text = \

                                                            String::Concat( "Sorry, but ",

                                                            "a number is required to. ",

                                                            "play. Please enter a ",

                                                            "number and try again." );

                                                }

                                                else

                                                            txtOutput->Text = \

                                                            "No input provided.";

                                     }

                                     txtInput->Focus();

                         }

private: System::Void btnNewGame_Click(System::Object^  sender, System::EventArgs^  e) {

                                     //Set conditions for new game

 

                                     //Generate a new random number

                                     this->GenerateRandomNumber();

                                     txtOutput->Text = "";

                                     txtInput->Text = "";

                                     //Set starting game buttons

                                     btnNewGame->Enabled = false;

                                     btnCheckGuess->Enabled = true;

                                     //Enable text input

                                     txtInput->Enabled = true;

                                     //Turn off radio buttons

                                     rbnControl10->Enabled = false;

                                     rbnControl100->Enabled = false;

                                     rbnControl1000->Enabled = false;

                                     //Disable the two reset buttons

                                     btnDefaults->Enabled = true;

                                     btnReset->Enabled = true;

                                     //Set status strip text

                                     stsLabel->Text = "Enter your guess.";

                                     txtInput->Focus();

                         }

private: System::Void chkVerbose_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {

                         }

};

}