Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Diagnostics;
- namespace towSimple
- {
- class HanoiTowers
- {
- public HanoiTowers()
- {
- NumberofDiscs = 0;
- NumberOfMoves = 0;
- }
- public HanoiTowers(int setDiscNumber)
- {
- NumberofDiscs = setDiscNumber;
- NumberOfMoves = 0;
- }
- public int NumberofDiscs
- {
- get;
- set;
- }
- public int NumberOfMoves
- {
- get;
- private set;
- }
- public void MoveDiscs(int discs, int from, int to, int temp)
- {
- if (discs > 0)
- {
- MoveDiscs(discs - 1, from, temp, to);
- NumberOfMoves += 1;
- Console.WriteLine(PrintMove(discs, from, to));
- MoveDiscs(discs - 1, temp, to, from);
- }
- }
- private string PrintMove(int discs, int from, int to)
- {
- return string.Format("Moving Disc {0} from Tower #{1} to Tower #{2}", discs, from, to);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.Title = "FB-ProgrammingLanguageGroup ToH-Sample 1";
- string aw = "";
- do
- {
- HanoiTowers towers = new HanoiTowers();
- Console.Write("Number of Discs: ");
- int nbrOfDiscs = int.Parse(Console.ReadLine());
- towers.NumberofDiscs = nbrOfDiscs;
- Stopwatch st = new Stopwatch();
- st.Start();
- towers.MoveDiscs(towers.NumberofDiscs, 1, 3, 2);
- st.Stop();
- TimeSpan span = st.Elapsed;
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("\n{0} Moves are needed to solve this problem with {1} Discs in {2:00}:{3:00}:{4:00}:{5:000}", towers.NumberOfMoves, towers.NumberofDiscs, span.Hours, span.Minutes, span.Seconds, span.Milliseconds);
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("\nQuit (y/n)?: ");
- aw = Console.ReadLine();
- } while (aw.ToLower() != "y");
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement