In C# Create a project named LetsMakeADeal. in the game, three prizes of varying
ID: 3816626 • Letter: I
Question
In C# Create a project named LetsMakeADeal. in the game, three prizes of varying value are assigned randomly to be hidden behind three "doors" that you can implement as Buttons. For example, the prizes might be a new car, a big-screen TV, and a live goat. The player chooses a Button, and then one of the two other prizes is revealed; the one revealed is never the most desirable prize. The user then has the option of changing the original selection to the remaing unseen choice. For example, consider these two game scenarios:
Suppose that the most valuable prize is randomly assigned tot he first button, If the user chooses the first button, reveal either of the other two prizes, and ask the user if he wants to change his selection.
Suppose that the most valuable prize is assigned to the first button, but the user chooese the secound button. Reveal the thrid prize so that the most valuable prize's location is still hidden, and then ask the user whether he wants to change his selection.
After the user has chosen to retain his original selection or make a change, reveal what he has won.
Explanation / Answer
Ans:
Program::
// 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.Windows.Forms;
using namespace
{
public partial class Form1 : Form
{
bool choice = true;
string[] prizes = { "A new CAR!!", "big-screen TV" ,"live Goat!" };
int randNum;
Random ranNumberGenerator = new Random();
public Form1()
{
InitializeComponent();
randNum = ranNumberGenerator.Next(0, 3);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
/// <the random number is assigned to this button>
if (choice)
{
/// <randomly assign prizes>
button1.Tag = randNum;
if (randNum == 0)
{
button2.Tag = 1;
button3.Tag = 2;
}
else if (randNum == 1)
{
button2.Tag = 0;
button3.Tag = 2;
}
else
{
button2.Tag = 0;
button3.Tag = 1;
}
if (Convert.ToInt32(button2.Tag) < Convert.ToInt32(button3.Tag))
textBox1.Text = "Door 2 has " + prizes[Convert.ToInt32(button2.Tag)] + ". Now You can change the doors!";
else
textBox1.Text = "Door 3 has " + prizes[Convert.ToInt32(button3.Tag)] + ". Now You can change the doors!";
choice = false;
}
else
{
textBox1.Text = "Congratulations, you have won the" + prizes[Convert.ToInt32(button1.Tag)];
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (choice)
{
/// <randomly assign prizes>
button2.Tag = randNum;
if (randNum == 0)
{
button1.Tag = 1;
button3.Tag = 2;
}
else if (randNum == 1)
{
button1.Tag = 0;
button3.Tag = 2;
}
else
{
button1.Tag = 0;
button3.Tag = 1;
}
if (Convert.ToInt32(button1.Tag) < Convert.ToInt32(button3.Tag))
textBox1.Text = "Door 1 has " + prizes[Convert.ToInt32(button1.Tag)] + ". You can change doors now!";
else
textBox1.Text = "Door 3 has " + prizes[Convert.ToInt32(button3.Tag)] + ". You can change doors now!";
choice = false;
}
else
{
textBox1.Text = "Congratulations, you have won the" + prizes[Convert.ToInt32(button2.Tag)];
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (choice)
{
/// <randomly assigns the prizes>
button3.Tag = randNum;
if (randNum == 0)
{
button1.Tag = 1;
button2.Tag = 2;
}
else if (randNum == 1)
{
button1.Tag = 0;
button2.Tag = 2;
}
else
{
button1.Tag = 0;
button2.Tag = 1;
}
if (Convert.ToInt32(button1.Tag) < Convert.ToInt32(button2.Tag))
textBox1.Text = "Door11 has the " + prizes[Convert.ToInt32(button1.Tag)]+ ".Now you can change doors!";
else
textBox1.Text = "Door-2 has the " + prizes[Convert.ToInt32(button2.Tag)] + ".Now you can change doors now!";
choice = false;
}
else
{
textBox1.Text = "Congratulations, you have won the" + prizes[Convert.ToInt32(button3.Tag)];
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
////Form1.Designer.cs////
using namespace
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
/// <button1>
this.button1.Location = new System.Drawing.Point(12, 39);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 176);
this.button1.TabIndex = 0;
this.button1.Text = "Door 1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
/// <button2>
this.button2.Location = new System.Drawing.Point(129, 39);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(94, 176);
this.button2.TabIndex = 1;
this.button2.Text = "Door 2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
/// <button3>
this.button3.Location = new System.Drawing.Point(245, 40);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(98, 180);
this.button3.TabIndex = 3;
this.button3.Text = "Door 3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
/// <label1>
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(96, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(158, 14);
this.label1.TabIndex = 2;
this.label1.Text = "Welcome to the Let's_Make_A_Deal! ";
/// <label2>
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(56, 230);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(240, 12);
this.label2.TabIndex = 3;
this.label2.Text = "Click on the Door to start)the_game (Choose_wisely!)";
/// <textBox1>
this.textBox1.BackColor = System.Drawing.SystemColors.Control;
this.textBox1.Location = new System.Drawing.Point(13, 262);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(323, 19);
this.textBox1.TabIndex = 4;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
/// <Form1>
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(350, 291);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Let's_make_a_deal!";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
}
}