Advertisement
Razali

Tower of Hanoi Solver

Aug 3rd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace Tower_of_Hanoi
  9. {
  10.     class Program
  11.     {
  12.         public static Stack<Int32> towerA = null, towerB = null, towerC = null;
  13.         public static int intTotalMoves = 0;
  14.         public static int speed;
  15.         static void Main(string[] args)
  16.         {
  17.            
  18.             int numDiscs;
  19.            
  20.             Console.Write("Enter the number of disc: ");
  21.             numDiscs = int.Parse(Console.ReadLine());
  22.  
  23.             Console.Write("Enter the speed of solving from 1(slowest) to 10(fastest): ");
  24.             speed = int.Parse(Console.ReadLine());
  25.             speed = (10 - speed) * 100;
  26.  
  27.             Init(numDiscs, ref towerA, ref towerB, ref towerC);
  28.             print(towerA, towerB, towerC);
  29.  
  30.             /* Start Game */
  31.             //Move nDisks from towerA to TowerC using towerB as a placeholder
  32.             move(towerA.Count, towerA, towerC, towerB);
  33.  
  34.             Console.WriteLine("\n\nTotal moves made " + intTotalMoves);
  35.             Console.ReadLine();
  36.         }
  37.  
  38.         static void move(int nDiscs, Stack<Int32> from, Stack<Int32> to, Stack<Int32> temp)
  39.         {
  40.             //Base Case
  41.             if(nDiscs == 1)
  42.             {
  43.                 //Directly move it to the destination
  44.                 to.Push(from.Pop());
  45.                 intTotalMoves++;
  46.                 print(towerA, towerB, towerC);
  47.                 Thread.Sleep(speed);
  48.                 return;
  49.             }
  50.  
  51.             move(nDiscs - 1, from, temp, to);
  52.             move(1, from, to, temp);
  53.             move(nDiscs - 1, temp, to, from);
  54.         }
  55.  
  56.         static void print(Stack<Int32> towerA, Stack<Int32> towerB, Stack<Int32> towerC)
  57.         {
  58.             /* Reverse the stacks for ease of printing */
  59.             towerA = new Stack<Int32>(towerA);
  60.             towerB = new Stack<Int32>(towerB);
  61.             towerC = new Stack<Int32>(towerC);
  62.  
  63.  
  64.             Console.Write("Tower A: ");
  65.             while(towerA.Count != 0)
  66.                 Console.Write(towerA.Pop().ToString());
  67.             Console.WriteLine();
  68.  
  69.             Console.Write("Tower B: ");
  70.             while (towerB.Count != 0)
  71.                 Console.Write(towerB.Pop().ToString());
  72.             Console.WriteLine();
  73.  
  74.             Console.Write("Tower C: ");
  75.             while (towerC.Count != 0)
  76.                 Console.Write(towerC.Pop().ToString());
  77.             Console.WriteLine("\n");
  78.         }
  79.         static void Init(int numDisks, ref Stack<Int32> towerA, ref Stack<Int32> towerB, ref Stack<Int32> towerC)
  80.         {
  81.             towerA = new Stack<int>();
  82.             towerC = new Stack<int>();
  83.             towerB = new Stack<int>();
  84.  
  85.             for (int i = numDisks; i >= 1; i--)
  86.                 towerA.Push(i);
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement