public void MoveDisk(Tower from, Tower to) { Trace.WriteLine(string.Format("Move disk from {0} to {1}", from, to)); var disk = from.Disks.Pop(); to.Disks.Push(disk); if (this.ValidateTower(from) == false || this.ValidateTower(to) == false) { throw new Exception("Validate failed"); } }
public void MoveDiskRecursive(Tower source, Tower target, Tower buffer, int n) { if (n == 1) { this.MoveDisk(source, target); } else { this.MoveDiskRecursive(source, buffer, target, n - 1); this.MoveDiskRecursive(source, target, buffer, 1); this.MoveDiskRecursive(buffer, target, source, n-1); } }
private bool ValidateTower(Tower tower) { var previousDisk = 0; foreach (var disk in tower.Disks) { if (previousDisk > 0 && previousDisk > disk) { return false; } previousDisk = disk; } return true; }