At the beginning you'll be asked to select an door.
After your selected a door the host will open a door which always contains a goat.
Then you will be asked if you want to stick to your first choice or do want to swap.
The game shows you the wins by staying and swapping.
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 Monty_Hall_Game {
public partial class frmMontyHallGame : Form {
public frmMontyHallGame() { InitializeComponent(); }
Random r = new Random();
int prize, selectedDoor, hostOpenDoor, remainingDoor, winByStay = 0, winBySwap = 0;
bool game = false;
void montyHallGame(int getDoor) {
if (game == false) {
prize = r.Next(1, 4); selectedDoor = getDoor; hostOpenDoor = prize;
while (hostOpenDoor == prize || hostOpenDoor == getDoor) { hostOpenDoor = r.Next(1, 4); }
((PictureBox)this.Controls["pbDoor" + hostOpenDoor.ToString()]).Image = Properties.Resources.Goat;
((PictureBox)this.Controls["pbDoor" + hostOpenDoor.ToString()]).Enabled = false;
((PictureBox)this.Controls["pbArrow" + hostOpenDoor.ToString()]).Visible = false;
lblMessage.Left = 45;
lblMessage.Text = "Do you want to stay or swap?";
remainingDoor = hostOpenDoor;
while (remainingDoor == getDoor || remainingDoor == hostOpenDoor) { remainingDoor = r.Next(1, 4); }
((PictureBox)this.Controls["pbArrow" + remainingDoor.ToString()]).Visible = false;
game = true;
}
else {
lblMessage.Left = 130;
remainingDoor = hostOpenDoor;
while (remainingDoor == getDoor || remainingDoor == hostOpenDoor) { remainingDoor = r.Next(1, 4); }
if (prize == getDoor) {
((PictureBox)this.Controls["pbDoor" + getDoor.ToString()]).Image = Properties.Resources.Car;
((PictureBox)this.Controls["pbDoor" + remainingDoor.ToString()]).Image = Properties.Resources.Goat;
if (getDoor == selectedDoor) {
winByStay++;
lblWinStay.Text = winByStay.ToString();
}
else {
winBySwap++;
lblWinSwap.Text = winBySwap.ToString();
}
lblMessage.Text = "You've won!";
reset(getDoor, remainingDoor);
}
else {
((PictureBox)this.Controls["pbDoor" + getDoor.ToString()]).Image = Properties.Resources.Goat;
((PictureBox)this.Controls["pbDoor" + remainingDoor.ToString()]).Image = Properties.Resources.Car;
lblMessage.Text = "You've lost!";
reset(getDoor, remainingDoor);
}
}
}
void reset(int getDoor, int remainingDoor) {
((PictureBox)this.Controls["pbDoor" + getDoor.ToString()]).Enabled = false;
((PictureBox)this.Controls["pbDoor" + remainingDoor.ToString()]).Enabled = false;
((PictureBox)this.Controls["pbArrow" + getDoor.ToString()]).Visible = true;
((PictureBox)this.Controls["pbArrow" + remainingDoor.ToString()]).Visible = false;
}
private void pbDoor1_Click(object sender, EventArgs e) { montyHallGame(1); }
private void pbDoor2_Click(object sender, EventArgs e) { montyHallGame(2); }
private void pbDoor3_Click(object sender, EventArgs e) { montyHallGame(3); }
private void button1_Click(object sender, EventArgs e) {
game = false;
lblMessage.Left = 85;
lblMessage.Text = "Make your selection !";
for (int i = 1; i <= 3; i++) {
((PictureBox)this.Controls["pbDoor" + i.ToString()]).Enabled = true;
((PictureBox)this.Controls["pbDoor" + i.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject("Door" + i.ToString());
((PictureBox)this.Controls["pbArrow" + i.ToString()]).Visible = true;
}
}
}
}
C# Winforms