using System.Collections.Generic;
private const int NumDiscs = 3;
private const int ColumnWidth = 20;
public string Name {get; set;}
public Stack<int> Discs {get; set;}
public static void Main()
Console.WriteLine("===================");
Console.WriteLine($"\n{NumDiscs} Discs\n");
var discIndex = NumDiscs;
Discs = new Stack<int>(Enumerable.Range(1, NumDiscs).Reverse())
WriteTowers(towerA, towerB, towerC);
MoveDisc(discIndex, towerA, towerC, towerB);
Console.WriteLine($"\nTotal Moves: {_numMoves}\n");
public static void MoveDisc(int discIndex, Tower fromTower, Tower toTower, Tower otherTower)
MoveDisc(discIndex - 1, fromTower, otherTower, toTower);
toTower.Discs.Push(fromTower.Discs.Pop());
Console.WriteLine("\nMove disk {0} from tower {1} to tower {2}\n", discIndex, fromTower.Name, toTower.Name);
WriteTowers(fromTower, toTower, otherTower);
MoveDisc(discIndex -1, otherTower, toTower, fromTower);
public static void WriteTowers(Tower tower1, Tower tower2, Tower tower3)
List<Tower> towers = new List<Tower>{tower1, tower2, tower3};
towers = towers.OrderBy(t => t.Name).ToList();
var sb = new StringBuilder();
foreach (var tower in towers)
string discDrawing = String.Empty;
var modifier = NumDiscs - tower.Discs.Count;
var discIndex = tower.Discs.ElementAtOrDefault(level - modifier);
discDrawing = GetDiskDrawing(discIndex, discIndex.ToString());
sb.Append(CenterAlignTextInColumn(discDrawing));
foreach (var tower in towers)
sb.Append(CenterAlignTextInColumn(tower.Name));
Console.WriteLine(sb.ToString());
private static string GetDiskDrawing(int size, string label)
return "/" + label + "\\";
return "/" + GetDiskDrawing(size - 1, label) + "\\";
private static string CenterAlignTextInColumn(string text)
int leftPadding = (ColumnWidth - text.Length) / 2;
int rightPadding = ColumnWidth - text.Length - leftPadding;
return new string(' ', leftPadding) + text + new string(' ', rightPadding);