Here is another classic puzzle called the Three Bucket Problem. In this scenario, there are an 8-liter bucket filled with water and empty 3-liter and 5-liter buckets. You solve the puzzle by using the three buckets to divide the 8 liters of water into two equal parts of 4 liters.
Expected Result:
Drag a bucket above the bucket you wish to pour into. Continue until you have divided the 8 liters into two buckets with 4 liters in each.For Reference:
http://learningandtheadolescentmind.org/resources_02_bucket.html
//Bucket Problem
//Three Buckets A,B,C in 8 litres,5 litres,3 litres respectively
//Fill the A and B with 4 litres of milk with out wasting any milk and C empty
using System;
using System.Windows.Forms;
namespace Bucket_Problem
{
public partial class Form1 : Form
{
//Number of attempt by User
int count = 0;
///
/// Constant value maximum value for Buckets
///
public const int bucketA=8,bucketB=5,bucketC=3;
///
/// Initial value for buckets
///
public int bucketAvalue=0, bucketBvalue=0, bucketCvalue=0;
public int tmpA = 0, tmpB = 0, tmpC = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Fill the Combo Box at Load Event
FillComboboxatLoad();
//Initial Value for Bucket
txtAbucket.Text = "8";
txtBbucket.Text = "0";
txtCbucket.Text = "0";
}
///
/// Fill the Combo Box at Load Event
///
public void FillComboboxatLoad()
{
cmbFrom.Items.Add("Select...");
cmbFrom.Items.Add("Bucket A");
cmbFrom.Items.Add("Bucket B");
cmbFrom.Items.Add("Bucket C");
cmbFrom.SelectedIndex = 0;
cmbTo.Items.Add("Select...");
cmbTo.Items.Add("Bucket A");
cmbTo.Items.Add("Bucket B");
cmbTo.Items.Add("Bucket C");
cmbTo.SelectedIndex = 0;
}
///
/// Used For sending the Milk from One Bucket to Another Bucket
///
private void btnGo_Click(object sender, EventArgs e)
{
if ((cmbFrom.SelectedItem.ToString().Trim() == "Select...")||(cmbTo.SelectedItem.ToString().Trim()) == "Select...")
{
MessageBox.Show("You must Select the Bucket in Combo Box.", "Name Entry Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
//Increment The Count Value
count = count + 1;
bucketAvalue=Convert.ToInt16(txtAbucket.Text);
bucketBvalue =Convert.ToInt16(txtBbucket.Text);
bucketCvalue = Convert.ToInt16(txtCbucket.Text);
try
{
//From A->B Data Flow from A TO B
//This Condition in used to Check whether A and B Selected on From and To
if (cmbFrom.SelectedItem.ToString() == "Bucket A")
{
if (cmbTo.SelectedItem.ToString() == "Bucket B")
{
if (bucketAvalue + bucketBvalue <= bucketB)
{
bucketBvalue = bucketAvalue + bucketBvalue;
bucketAvalue = 0;
}
else if (bucketAvalue + bucketBvalue > bucketB)
{
tmpB = (bucketB - bucketBvalue);
bucketAvalue = bucketAvalue - tmpB;
if (bucketAvalue < 0)
bucketAvalue = bucketAvalue * (-1);
bucketBvalue = bucketBvalue + tmpB;
}
txtAbucket.Text = bucketAvalue.ToString();
txtBbucket.Text = bucketBvalue.ToString();
}
//From A->C Data Flow from A TO C
//This Condition in used to Check whether A and C Selected on From and To
if (cmbTo.SelectedItem.ToString() == "Bucket C")
{
if (bucketAvalue + bucketCvalue <= bucketC)
{
bucketCvalue = bucketAvalue + bucketCvalue;
bucketAvalue = 0;
}
else if (bucketAvalue + bucketCvalue > bucketC)
{
tmpC = (bucketC - bucketCvalue);
bucketAvalue = bucketAvalue - tmpC;
if (bucketAvalue < 0)
bucketAvalue = bucketAvalue * (-1);
bucketCvalue = bucketCvalue + tmpC;
}
txtAbucket.Text = bucketAvalue.ToString();
txtCbucket.Text = bucketCvalue.ToString();
}
}
//From B->A Data Flow from B TO A
//This Condition in used to Check whether B and A Selected on From and To
if (cmbFrom.SelectedItem.ToString() == "Bucket B")
{
if (cmbTo.SelectedItem.ToString() == "Bucket A")
{
if (bucketBvalue + bucketAvalue <= bucketA)
{
bucketAvalue = bucketBvalue + bucketAvalue;
bucketBvalue = 0;
}
else if (bucketAvalue + bucketBvalue > bucketA)
{
tmpA = (bucketA - bucketAvalue);
bucketBvalue = bucketBvalue - tmpA;
if (bucketBvalue < 0)
bucketBvalue = bucketBvalue * (-1);
bucketAvalue = bucketAvalue + tmpA;
}
txtAbucket.Text = bucketAvalue.ToString();
txtBbucket.Text = bucketBvalue.ToString();
}
//From B->C Data Flow from B TO C
//This Condition in used to Check whether B and C Selected on From and To
if (cmbTo.SelectedItem.ToString() == "Bucket C")
{
if (bucketBvalue + bucketCvalue <= bucketC)
{
bucketCvalue = bucketBvalue + bucketCvalue;
bucketBvalue = 0;
}
else if (bucketBvalue + bucketCvalue > bucketC)
{
tmpC = (bucketC - bucketCvalue);
bucketBvalue = bucketBvalue - tmpC;
if (bucketBvalue < 0)
bucketBvalue = bucketBvalue * (-1);
bucketCvalue = bucketCvalue + tmpC;
}
txtBbucket.Text = bucketBvalue.ToString();
txtCbucket.Text = bucketCvalue.ToString();
}
}
//From C->A Data Flow from C TO A
//This Condition in used to Check whether C and A Selected on From and To
if (cmbFrom.SelectedItem.ToString() == "Bucket C")
{
if (cmbTo.SelectedItem.ToString() == "Bucket A")
{
if (bucketCvalue + bucketAvalue <= bucketA)
{
bucketAvalue = bucketCvalue + bucketAvalue;
bucketCvalue = 0;
}
else if (bucketAvalue + bucketCvalue > bucketA)
{
tmpA = (bucketA - bucketAvalue);
bucketCvalue = bucketCvalue - tmpA;
if (bucketCvalue < 0)
bucketCvalue = bucketBvalue * (-1);
bucketAvalue = bucketAvalue + tmpA;
}
txtAbucket.Text = bucketAvalue.ToString();
txtCbucket.Text = bucketCvalue.ToString();
}
//FROM C->B Data Flow from C TO B
// This Condition in used to Check whether C and C Selected on From and To
if (cmbTo.SelectedItem.ToString() == "Bucket B")
{
if (bucketBvalue + bucketCvalue <= bucketB)
{
bucketBvalue = bucketBvalue + bucketCvalue;
bucketCvalue = 0;
}
else if (bucketCvalue + bucketBvalue > bucketB)
{
tmpB = (bucketB - bucketBvalue);
bucketCvalue = bucketCvalue - tmpB;
if (bucketCvalue < 0)
bucketCvalue = bucketCvalue * (-1);
bucketBvalue = bucketBvalue + tmpB;
}
txtBbucket.Text = bucketBvalue.ToString();
txtCbucket.Text = bucketCvalue.ToString();
}
}
lbCount.Text = count.ToString();
if ((txtAbucket.Text == "4") && (txtBbucket.Text == "4"))
{
lbCount.Text = "You Won Your count is" + count;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
private void cmbFrom_SelectedIndexChanged(object sender, EventArgs e)
{
//Fill the Combobox when it is Bucket A
if (cmbFrom.SelectedItem.ToString() == "Bucket A")
{
cmbTo.Items.Clear();
cmbTo.Items.Add("Select...");
cmbTo.Items.Add("Bucket B");
cmbTo.Items.Add("Bucket C");
cmbTo.SelectedIndex = 0;
}
//Fill the Combobox when it is Bucket B
if (cmbFrom.SelectedItem.ToString() == "Bucket B")
{
cmbTo.Items.Clear();
cmbTo.Items.Add("Select...");
cmbTo.Items.Add("Bucket A");
cmbTo.Items.Add("Bucket C");
cmbTo.SelectedIndex = 0;
}
//Fill the Combobox when it is Bucket C
if (cmbFrom.SelectedItem.ToString() == "Bucket C")
{
cmbTo.Items.Clear();
cmbTo.Items.Add("Select...");
cmbTo.Items.Add("Bucket A");
cmbTo.Items.Add("Bucket B");
cmbTo.SelectedIndex = 0;
}
}
}
}
No comments:
Post a Comment
Comments Welcome