Advertisement
f0rkB0mb

Contest - Towers of Hanoi - Simple

Jan 9th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Diagnostics;
  4.  
  5. namespace towSimple
  6. {
  7.     class HanoiTowers
  8.     {
  9.         public HanoiTowers()
  10.         {
  11.             NumberofDiscs = 0;
  12.             NumberOfMoves = 0;
  13.         }
  14.  
  15.         public HanoiTowers(int setDiscNumber)
  16.         {
  17.             NumberofDiscs = setDiscNumber;
  18.             NumberOfMoves = 0;
  19.         }
  20.  
  21.         public int NumberofDiscs
  22.         {
  23.             get;
  24.             set;
  25.         }
  26.        
  27.         public int NumberOfMoves
  28.         {
  29.             get;
  30.             private set;
  31.         }
  32.  
  33.         public void MoveDiscs(int discs, int from, int to, int temp)
  34.         {
  35.            
  36.             if (discs > 0)
  37.             {
  38.                 MoveDiscs(discs - 1, from, temp, to);
  39.                 NumberOfMoves += 1;
  40.                 Console.WriteLine(PrintMove(discs, from, to));
  41.                 MoveDiscs(discs - 1, temp, to, from);
  42.             }
  43.         }
  44.  
  45.         private string PrintMove(int discs, int from, int to)
  46.         {
  47.             return string.Format("Moving Disc {0} from Tower #{1} to Tower #{2}", discs, from, to);
  48.         }
  49.  
  50.     }
  51.  
  52.     class Program
  53.     {
  54.         static void Main(string[] args)
  55.         {
  56.             Console.Title = "FB-ProgrammingLanguageGroup ToH-Sample 1";
  57.             string aw = "";
  58.             do
  59.             {
  60.                 HanoiTowers towers = new HanoiTowers();
  61.  
  62.                 Console.Write("Number of Discs: ");
  63.                 int nbrOfDiscs = int.Parse(Console.ReadLine());
  64.                 towers.NumberofDiscs = nbrOfDiscs;
  65.                 Stopwatch st = new Stopwatch();
  66.  
  67.                 st.Start();
  68.                 towers.MoveDiscs(towers.NumberofDiscs, 1, 3, 2);
  69.                 st.Stop();
  70.  
  71.                 TimeSpan span = st.Elapsed;
  72.  
  73.                 Console.ForegroundColor = ConsoleColor.Yellow;
  74.                 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);
  75.                 Console.ForegroundColor = ConsoleColor.Gray;
  76.  
  77.                 Console.Write("\nQuit (y/n)?: ");
  78.                 aw = Console.ReadLine();
  79.  
  80.  
  81.             } while (aw.ToLower() != "y");
  82.             return;
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement