11. Random Number Guessing Game Create an application that generates a in the ra
ID: 3849098 • Letter: 1
Question
11. Random Number Guessing Game Create an application that generates a in the range random number of 1 through 100 and asks the user to guess what user's guess is higher than the random number, number is. If the try again If the user's the program should display high, guess is lower than the random number, the program should display "To low, try If the user guesses the number, the application should congratulate the user and then generate a new random number so the game can start over. optional Enhancement: Enhance the game so it keeps count of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses.Explanation / Answer
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;
/* Random Number Guessing Game
Requirement: Create an application that generates a random number in the range of 1 through 100 and asks the user to guess what the number is.
If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number,
the program should display "Too low, try again." If the user guesses the number, the application should congratulate the user and then generate a
new random number so the game can start over. The game should keep count of the number of guesses that the user makes.
When the user correctly guesses the random number, the program should display the number of guesses along with the congratulations.
*/
namespace HanHW4_1
{
public partial class Form1 : Form
{
// Declare fields to be used in the class
Random rand;
int number;
int playCount;
public Form1()
{
InitializeComponent();
rand = new Random();
}
private void Form1_Load(object sender, EventArgs e)
{
// Call generateNumber method
generateNumber();
}
private void generateNumber()
{
// Generate random number, assign integer 1 to playCount variable
number = rand.Next(1 , 101);
randomNumberTextBox.Text = number.ToString();
playCount = 1;
playCountTextBox.Text = playCount.ToString();
resultLabel.Text = string.Empty;
guessTextBox.Text = string.Empty;
// Change randomNumberTextBox property to hide
randomNumberTextBox.Visible = false;
}
private void generatePlayCount()
{
// Increase the count of playCount variable
playCount++;
playCountTextBox.Text = playCount.ToString();
}
private void exitButton_Click(object sender, EventArgs e)
{
// Close the form
this.Close();
}
private void startNewButton_Click(object sender, EventArgs e)
{
// Call generateNumber method
generateNumber();
}
private void guessTextBox_KeyDown(object sender, KeyEventArgs e)
{
try
{
// Check enter key press
if (e.KeyCode == Keys.Enter)
{
// Declare variable for user input number
int guessedNumber = int.Parse(guessTextBox.Text);
// if guess number is higher than randomly generated number, inform the user with message box control regarding the result.
if (guessedNumber > number)
{
resultLabel.Text = guessTextBox.Text + " is too high, please try again.";
generatePlayCount();
}
// if guess number is lower than randomly generated number, inform the user with message box control regarding the result.
else if (guessedNumber < number)
{
resultLabel.Text = guessTextBox.Text + " is too low, please try again.";
generatePlayCount();
}
else
{
// Dialogresult to store user choice
randomNumberTextBox.Visible = true;
resultLabel.Text = string.Empty;
DialogResult dialogResult = MessageBox.Show("Congratulations, " + guessedNumber + " was the lucky number and the play count was " + playCountTextBox.Text + ". Are you ready for a new challenge?", "Well done!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
// Call generateNumber method
generateNumber();
}
else if (dialogResult == DialogResult.No)
{
// Farewell user and close the form
MessageBox.Show("See you next time!");
this.Close();
}
}
// Clear input text box
guessTextBox.Text = string.Empty;
}
}
catch (Exception ex)
{
// Display error message
MessageBox.Show(ex.Message);
guessTextBox.Text = string.Empty;
}
}
}
}
Form1.Designer.cs
namespace HanHW4_1
{
partial class Form1
{
/// <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.playCountLabel = new System.Windows.Forms.Label();
this.playCountTextBox = new System.Windows.Forms.TextBox();
this.guessTextBox = new System.Windows.Forms.TextBox();
this.guessLabel = new System.Windows.Forms.Label();
this.exitButton = new System.Windows.Forms.Button();
this.startNewButton = new System.Windows.Forms.Button();
this.randomNumberTextBox = new System.Windows.Forms.TextBox();
this.randomNumberLabel = new System.Windows.Forms.Label();
this.resultLabel = new System.Windows.Forms.Label();
this.guessNumberLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// playCountLabel
//
this.playCountLabel.AutoSize = true;
this.playCountLabel.Location = new System.Drawing.Point(12, 67);
this.playCountLabel.Name = "playCountLabel";
this.playCountLabel.Size = new System.Drawing.Size(72, 17);
this.playCountLabel.TabIndex = 0;
this.playCountLabel.Text = "Play Count:";
//
// playCountTextBox
//
this.playCountTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.playCountTextBox.Location = new System.Drawing.Point(86, 67);
this.playCountTextBox.Name = "playCountTextBox";
this.playCountTextBox.ReadOnly = true;
this.playCountTextBox.Size = new System.Drawing.Size(100, 18);
this.playCountTextBox.TabIndex = 1;
this.playCountTextBox.TabStop = false;
//
// guessTextBox
//
this.guessTextBox.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.guessTextBox.Location = new System.Drawing.Point(240, 115);
this.guessTextBox.Name = "guessTextBox";
this.guessTextBox.Size = new System.Drawing.Size(61, 35);
this.guessTextBox.TabIndex = 1;
this.guessTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.guessTextBox_KeyDown);
//
// guessLabel
//
this.guessLabel.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.guessLabel.Location = new System.Drawing.Point(12, 10);
this.guessLabel.Name = "guessLabel";
this.guessLabel.Size = new System.Drawing.Size(389, 45);
this.guessLabel.TabIndex = 3;
this.guessLabel.Text = "Random number has been generated between 1 and 100. Please enter your guess numbe" +
"r in the text box and press Enter key.";
//
// exitButton
//
this.exitButton.Location = new System.Drawing.Point(313, 244);
this.exitButton.Name = "exitButton";
this.exitButton.Size = new System.Drawing.Size(75, 26);
this.exitButton.TabIndex = 3;
this.exitButton.Text = "E&xit";
this.exitButton.UseVisualStyleBackColor = true;
this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
//
// startNewButton
//
this.startNewButton.Location = new System.Drawing.Point(23, 244);
this.startNewButton.Name = "startNewButton";
this.startNewButton.Size = new System.Drawing.Size(75, 26);
this.startNewButton.TabIndex = 2;
this.startNewButton.Text = "Start New";
this.startNewButton.UseVisualStyleBackColor = true;
this.startNewButton.Click += new System.EventHandler(this.startNewButton_Click);
//
// randomNumberTextBox
//
this.randomNumberTextBox.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.randomNumberTextBox.Location = new System.Drawing.Point(100, 115);
this.randomNumberTextBox.Name = "randomNumberTextBox";
this.randomNumberTextBox.ReadOnly = true;
this.randomNumberTextBox.Size = new System.Drawing.Size(61, 35);
this.randomNumberTextBox.TabIndex = 7;
this.randomNumberTextBox.TabStop = false;
this.randomNumberTextBox.Visible = false;
//
// randomNumberLabel
//
this.randomNumberLabel.AutoSize = true;
this.randomNumberLabel.Location = new System.Drawing.Point(63, 94);
this.randomNumberLabel.Name = "randomNumberLabel";
this.randomNumberLabel.Size = new System.Drawing.Size(112, 17);
this.randomNumberLabel.TabIndex = 9;
this.randomNumberLabel.Text = "Random Number:";
//
// resultLabel
//
this.resultLabel.AutoSize = true;
this.resultLabel.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.resultLabel.Location = new System.Drawing.Point(107, 164);
this.resultLabel.Name = "resultLabel";
this.resultLabel.Size = new System.Drawing.Size(0, 17);
this.resultLabel.TabIndex = 12;
//
// guessNumberLabel
//
this.guessNumberLabel.AutoSize = true;
this.guessNumberLabel.Location = new System.Drawing.Point(219, 94);
this.guessNumberLabel.Name = "guessNumberLabel";
this.guessNumberLabel.Size = new System.Drawing.Size(98, 17);
this.guessNumberLabel.TabIndex = 13;
this.guessNumberLabel.Text = "Guess Number:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(413, 294);
this.Controls.Add(this.guessNumberLabel);
this.Controls.Add(this.resultLabel);
this.Controls.Add(this.randomNumberLabel);
this.Controls.Add(this.randomNumberTextBox);
this.Controls.Add(this.startNewButton);
this.Controls.Add(this.exitButton);
this.Controls.Add(this.guessLabel);
this.Controls.Add(this.guessTextBox);
this.Controls.Add(this.playCountTextBox);
this.Controls.Add(this.playCountLabel);
this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Number Guessing Game";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label playCountLabel;
private System.Windows.Forms.TextBox playCountTextBox;
private System.Windows.Forms.TextBox guessTextBox;
private System.Windows.Forms.Label guessLabel;
private System.Windows.Forms.Button exitButton;
private System.Windows.Forms.Button startNewButton;
private System.Windows.Forms.TextBox randomNumberTextBox;
private System.Windows.Forms.Label randomNumberLabel;
private System.Windows.Forms.Label resultLabel;
private System.Windows.Forms.Label guessNumberLabel;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HanHW4_1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.