private static void DoOperations(int[] operations, Bottle bottle1, Bottle bottle2) { bottle1.Empty(); bottle2.Empty(); foreach (var operation in operations) { if (operation == 0) { bottle1.FillToTopFromTap(); } else if (operation == 1) { bottle2.FillToTopFromTap(); } else if (operation == 2) { bottle2.Fill(bottle1.Empty()); } else if (operation == 3) { bottle1.Fill(bottle2.Empty()); } else if (operation == 4) { bottle2.FillToTop(bottle1); } else if (operation == 5) { bottle1.FillToTop(bottle2); } else if (operation == 6) { bottle1.Empty(); } else if (operation == 7) { bottle2.Empty(); } } }
static void Main(string[] args) { Bottle bottle1, bottle2; Bottle.SetButtleCapasity(out bottle1, out bottle2); //var bottle1 = new Bottle(5, 0); //var bottle2 = new Bottle(7, 0); //var wantedVolume = 5; int numberOfOperations = 1; while (true) { var isSolved = TryWithGivenNumberOfOperations(numberOfOperations, bottle1, bottle2, Bottle.WantedVolume); if (isSolved) { break; } numberOfOperations++; } }
private static bool TryWithGivenNumberOfOperations(int numberOfOperations, Bottle bottle1, Bottle bottle2, int wantedVolume) { Console.WriteLine(); Console.WriteLine($"Prøver med {numberOfOperations} oprasjon(er)."); var operations = new int[numberOfOperations]; while (true) { DoOperations(operations, bottle1, bottle2); var isSolved = IsSolved(bottle1, bottle2, wantedVolume, operations); if (isSolved) { return(true); } var success = UpdateOperations(operations); if (!success) { break; } } return(false); }
private static void DoOperations(int[] operations, Bottle bottle1, Bottle bottle2) { for (var i = 1; i < operations.Length; i++) { //køyr på! var operation = operations[i]; if (operation == 0) { bottle1.FillUpFromTap(); //Fylle flaske 1 } else if (operation == 1) { bottle2.FillUpFromTap(); //Fylle flaske 2 } else if (operation == 2) //tømme fra flaske 1 i flaske 2 { if (bottle1.IsEmpty()) { break; } var success = bottle2.Fill(bottle1.Empty()); if (!success) { break; } } else if (operation == 3) //tømme fra flaske 2 i flaske 1 { if (bottle2.IsEmpty()) { break; } var success = bottle2.Fill(bottle2.Empty()); if (!success) { break; } } else if (operation == 4) //fylle opp flaske 2 med flaske 1 { var success = bottle2.FillUp(bottle1); if (!success) { break; } } else if (operation == 5) //fylle opp flaske 1 med flaske 2 { var success = bottle1.FillUp(bottle2); if (!success) { break; } } else if (operation == 6) //tømme flaske 1 { bottle1.Empty(); } else if (operation == 7) //tømme flaske 2 { bottle2.Empty(); } } }