Assignment10yourlastnamecs In C Write a Windows Form program

[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers.

Create a Form, name it GuessANumber and create a randomly generated array containing 100 integer numbers. See examples for random number generation at the following sites: http://www.dotnetperls.com/random http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx

The random number generation for the array will be done in a method both when the array is first populated and when the array needs to be repopulated (requirement 10).

The form will have a textbox in which the user will enter an integer number to match against the next number in the array.

The form will have three buttons labeled Guess, Next Guess (disabled at startup), and Exit GuessANumber.

The form will have a label that contains the text \"Place your mouse here for a hint\" that will generate a hint about the hidden number when the mouse is passed over the label which will be displayed in a second label as follows:

If the current random number in the array is greater than 3 and less than 96 set the second label text to \"It\'s not <random number + 3>\"

Else set the second label text to \"It\'s not <random number - 1>\"

Starting with position 0 in each array ask the user to guess the number currently selected from the array, enter it in the textbox and click the Guess button. The number for each guess will be selected from the array in the index order (0 - 99).

When the Guess button is clicked:

The Guess button will be disabled.

The Next Guess button will be enabled.

A label will display a \"You Win\" message if the number entered in the textbox matches the number from the array or \"Sorry - You Lose; the number is: <random number>\" if the number does not match the number from the array.

If the numbers match add one to an accumulating correct guess counter and display the number of correct guesses made.

If the numbers do not match, add one to an accumulating incorrect guess counter and display the number of incorrect guesses made.

Disable the hint label.

Set the array index to the next number in the array.

When the Next Guess button is clicked:

The Next Guess button will be disabled.

The Guess button will be enabled.

The win/lose message label will be cleared of text.

The hint label will be enabled.

When the user clicks the Next Guess button, the user can then make another guess and the program will use the next array number from the array.

If the user makes more than 100 guesses, the program should use the randomization method to repopulate the random numbers in the array and reset the array subscript to 0 so the guessing can start over, but continue the running totals.

When the user clicks the Exit GuessANumber button the application will close.

Internal Documentation.

Solution

Form1.Designer.cs

namespace Assignment10_Stroop
{
    partial class GuessANumber
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name=\"disposing\">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #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>
        private void InitializeComponent()
        {
            this.guessBtn = new System.Windows.Forms.Button();
            this.nextGuessBtn = new System.Windows.Forms.Button();
            this.exitBtn = new System.Windows.Forms.Button();
            this.hintPromptLbl = new System.Windows.Forms.Label();
            this.hintLbl = new System.Windows.Forms.Label();
            this.incorrectLbl = new System.Windows.Forms.Label();
            this.correctLbl = new System.Windows.Forms.Label();
            this.playerGuessTxt = new System.Windows.Forms.TextBox();
            this.statusLbl = new System.Windows.Forms.Label();
            this.correctNumberLbl = new System.Windows.Forms.Label();
            this.correctCountLbl = new System.Windows.Forms.Label();
            this.incorrectCountLbl = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // guessBtn
            //
            this.guessBtn.Location = new System.Drawing.Point(28, 80);
            this.guessBtn.Name = \"guessBtn\";
            this.guessBtn.Size = new System.Drawing.Size(75, 23);
            this.guessBtn.TabIndex = 0;
            this.guessBtn.Text = \"Guess\";
            this.guessBtn.UseVisualStyleBackColor = true;
            this.guessBtn.Click += new System.EventHandler(this.guessBtn_Click);
            //
            // nextGuessBtn
            //
            this.nextGuessBtn.Enabled = false;
            this.nextGuessBtn.Location = new System.Drawing.Point(28, 124);
            this.nextGuessBtn.Name = \"nextGuessBtn\";
            this.nextGuessBtn.Size = new System.Drawing.Size(75, 23);
            this.nextGuessBtn.TabIndex = 1;
            this.nextGuessBtn.Text = \"Next Guess\";
            this.nextGuessBtn.UseVisualStyleBackColor = true;
            this.nextGuessBtn.Click += new System.EventHandler(this.nextGuessBtn_Click);
            //
            // exitBtn
            //
            this.exitBtn.Location = new System.Drawing.Point(28, 221);
            this.exitBtn.Name = \"exitBtn\";
            this.exitBtn.Size = new System.Drawing.Size(183, 23);
            this.exitBtn.TabIndex = 2;
            this.exitBtn.Text = \"Exit Guess A Number\";
            this.exitBtn.UseVisualStyleBackColor = true;
            this.exitBtn.Click += new System.EventHandler(this.exitBtn_Click);
            //
            // hintPromptLbl
            //
            this.hintPromptLbl.AutoSize = true;
            this.hintPromptLbl.Location = new System.Drawing.Point(25, 172);
            this.hintPromptLbl.Name = \"hintPromptLbl\";
            this.hintPromptLbl.Size = new System.Drawing.Size(186, 13);
            this.hintPromptLbl.TabIndex = 3;
            this.hintPromptLbl.Text = \"Place your mouse over here for a hint!\";
            this.hintPromptLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.hintPromptLbl.MouseHover += new System.EventHandler(this.hintPromptLbl_MouseHover);
            //
            // hintLbl
            //
            this.hintLbl.Location = new System.Drawing.Point(25, 195);
            this.hintLbl.Name = \"hintLbl\";
            this.hintLbl.Size = new System.Drawing.Size(186, 13);
            this.hintLbl.TabIndex = 4;
            this.hintLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // incorrectLbl
            //
            this.incorrectLbl.AutoSize = true;
            this.incorrectLbl.Location = new System.Drawing.Point(133, 135);
            this.incorrectLbl.Name = \"incorrectLbl\";
            this.incorrectLbl.Size = new System.Drawing.Size(52, 13);
            this.incorrectLbl.TabIndex = 5;
            this.incorrectLbl.Text = \"Incorrect:\";
            //
            // correctLbl
            //
            this.correctLbl.AutoSize = true;
            this.correctLbl.Location = new System.Drawing.Point(133, 116);
            this.correctLbl.Name = \"correctLbl\";
            this.correctLbl.Size = new System.Drawing.Size(44, 13);
            this.correctLbl.TabIndex = 6;
            this.correctLbl.Text = \"Correct:\";
            //
            // playerGuessTxt
            //
            this.playerGuessTxt.Location = new System.Drawing.Point(136, 82);
            this.playerGuessTxt.Name = \"playerGuessTxt\";
            this.playerGuessTxt.Size = new System.Drawing.Size(75, 20);
            this.playerGuessTxt.TabIndex = 7;
            this.playerGuessTxt.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // statusLbl
            //
            this.statusLbl.Font = new System.Drawing.Font(\"Microsoft Sans Serif\", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.statusLbl.Location = new System.Drawing.Point(28, 18);
            this.statusLbl.Name = \"statusLbl\";
            this.statusLbl.Size = new System.Drawing.Size(183, 23);
            this.statusLbl.TabIndex = 8;
            this.statusLbl.Text = \"Guess A Number!\";
            this.statusLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // correctNumberLbl
            //
            this.correctNumberLbl.Location = new System.Drawing.Point(28, 45);
            this.correctNumberLbl.Name = \"correctNumberLbl\";
            this.correctNumberLbl.Size = new System.Drawing.Size(183, 23);
            this.correctNumberLbl.TabIndex = 9;
            this.correctNumberLbl.Text = \"Guess a number between 1 and 100\";
            this.correctNumberLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // correctCountLbl
            //
            this.correctCountLbl.AutoSize = true;
            this.correctCountLbl.Location = new System.Drawing.Point(198, 116);
            this.correctCountLbl.Name = \"correctCountLbl\";
            this.correctCountLbl.Size = new System.Drawing.Size(13, 13);
            this.correctCountLbl.TabIndex = 10;
            this.correctCountLbl.Text = \"0\";
            this.correctCountLbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // incorrectCountLbl
            //
            this.incorrectCountLbl.AutoSize = true;
            this.incorrectCountLbl.Location = new System.Drawing.Point(198, 135);
            this.incorrectCountLbl.Name = \"incorrectCountLbl\";
            this.incorrectCountLbl.Size = new System.Drawing.Size(13, 13);
            this.incorrectCountLbl.TabIndex = 11;
            this.incorrectCountLbl.Text = \"0\";
            this.incorrectCountLbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // GuessANumber
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(247, 261);
            this.Controls.Add(this.incorrectCountLbl);
            this.Controls.Add(this.correctCountLbl);
            this.Controls.Add(this.correctNumberLbl);
            this.Controls.Add(this.statusLbl);
            this.Controls.Add(this.playerGuessTxt);
            this.Controls.Add(this.correctLbl);
            this.Controls.Add(this.incorrectLbl);
            this.Controls.Add(this.hintLbl);
            this.Controls.Add(this.hintPromptLbl);
            this.Controls.Add(this.exitBtn);
            this.Controls.Add(this.nextGuessBtn);
            this.Controls.Add(this.guessBtn);
            this.Name = \"GuessANumber\";
            this.Text = \"Guess A Number\";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button guessBtn;
        private System.Windows.Forms.Button nextGuessBtn;
        private System.Windows.Forms.Button exitBtn;
        private System.Windows.Forms.Label hintPromptLbl;
        private System.Windows.Forms.Label hintLbl;
        private System.Windows.Forms.Label incorrectLbl;
        private System.Windows.Forms.Label correctLbl;
        private System.Windows.Forms.TextBox playerGuessTxt;
        private System.Windows.Forms.Label statusLbl;
        private System.Windows.Forms.Label correctNumberLbl;
        private System.Windows.Forms.Label correctCountLbl;
        private System.Windows.Forms.Label incorrectCountLbl;
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment10_Stroop
{
    public partial class GuessANumber : Form
    {
        // class fields
        public int[] Numbers { get; set; }
        public int ArrayPosition { get; set; }
        public int CorrectGuessCount { get; set; }
        public int IncorrectGuessCount { get; set; }

        // derived field for easier reading
        public int CorrectNumber
        {
            get { return Numbers[ArrayPosition]; }
        }

        public GuessANumber()
        {
            Numbers = new int[100]; // instantiate array
            FillRandomNumberArray(); // populate array with random numbers
            ArrayPosition = 0; // set array index to 0
            CorrectGuessCount = 0; // set correct guess count to 0
            IncorrectGuessCount = 0; // set incorrect guess count to 0
            InitializeComponent(); // initialize form
        }

        // method fills numbers array with random numbers
        private void FillRandomNumberArray()
        {
            Random random = new Random();

            for(int i = 0; i < Numbers.Length; i++)
            {
                Numbers[i] = random.Next(0, 101);
            }
        }

        // Guess Button event handler
        private void guessBtn_Click(object sender, EventArgs e)
        {
            int guess; // int variable for user guess

            // try to parse input, assigns value of zero if not sucessful
            int.TryParse(playerGuessTxt.Text, out guess);

            // if player guess is out of range, or if
            // input was unable to be parsed (0),
            // alert user and return from method
            if (guess < 1 || guess > 100)
            {
                correctNumberLbl.Text = LabelTexts.InvalidGuess;
                return;
            }

            guessBtn.Enabled = false; // diable guess button
            nextGuessBtn.Enabled = true; // enable next guess button

            // if guess is correct
            if (guess == CorrectNumber)
            {
                statusLbl.Text = LabelTexts.Win; // set label to alert of win
                CorrectGuessCount ++; // update running count of wins
                correctCountLbl.Text = Convert.ToString(CorrectGuessCount); // update label to reflect wins
            }
            // if guess is incorrect
            else
            {
                statusLbl.Text = LabelTexts.Lose; // set label to alert of loss
                IncorrectGuessCount++; // update running count of losses
                incorrectCountLbl.Text = Convert.ToString(IncorrectGuessCount); // update label to reflect losses
            }

            // display correct number in label
            correctNumberLbl.Text = String.Format(\"{0} {1}\", LabelTexts.CorrectNumber, this.CorrectNumber);

            // disable hint label
            hintLbl.Enabled = false;

        }

        // Next Guess button event handler
        private void nextGuessBtn_Click(object sender, EventArgs e)
        {
            // if the array possition is at the end of the array,
            // repopulate array with random numbers, otherwise
            // increment array index to next position
            if (ArrayPosition == Numbers.Length - 1)
            {
                FillRandomNumberArray();
                ArrayPosition = 0;
            }
            else
            {
                ArrayPosition++; // increment array index
            }

            guessBtn.Enabled = true; // diable guess button
            nextGuessBtn.Enabled = false; // enable next guess button
            statusLbl.Text = LabelTexts.StatusDefault; // reset status label
            hintLbl.Text = \"\"; // reset hint label text
            playerGuessTxt.Text = \"\";
            correctNumberLbl.Text = LabelTexts.CorrectNumberDefault;
            hintLbl.Enabled = true; // enable hint label

            Console.WriteLine(\"Correct Number:\" + CorrectNumber);
        }

        // event handler for hint label mouse hover
        private void hintPromptLbl_MouseHover(object sender, EventArgs e)
        {
            int hintNumber;

            // set hint number based on range of correct number
            if (CorrectNumber > 3 && CorrectNumber < 96)
            {
                hintNumber = CorrectNumber + 3;
            }
            else
            {
                hintNumber = CorrectNumber - 1;
            }

            // set hint label text to display hint
            hintLbl.Text = String.Format(\"{0} {1}.\", LabelTexts.Hint, hintNumber);
        }

        // exit button event handler closes application
        private void exitBtn_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }

     struct LabelTexts
    {
        public const string StatusDefault = \"Guess A Number!\";
        public const string CorrectNumberDefault = \"Guess a number between 1 and 100\";
        public const string Win = \"Yes, You Win!\";
        public const string Lose = \"Sorry, You Lose!\";
        public const string InvalidGuess = \"Invalid Entry. Please Try Again.\";
        public const string CorrectNumber = \"The correct number was\";
        public const string Hint = \"The number is not\";
    }
}

Assignment.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment10_Stroop
{
    static class Assignment10_Stroop
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GuessANumber());
        }
    }
}

[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an
[Assignment10_yourlastname.cs] In C#: Write a Windows Form program to guess against a list of randomly generated numbers. Create a Form, name it GuessANumber an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site