Skip to content

C# console application game that uses Recursion to perform a set of movements on disks over different piles.

Notifications You must be signed in to change notification settings

luayyounus/TowerOfHanoi_Recursion

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tower of Hanoi - Math Puzzle

The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods and a number of disks of different sizes, which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

Overview

This is a C# console application. The purpose of this program is to show possible puzzle moves with Recursion.

Requirements to run the Application

Getting Started

  1. Clone the repository to your local machine.
  2. Cd into the application directory where the AppName.sln exist.
  3. Open the application using Open/Start AppName.sln.
  4. Once Visual Studio is opened, you can Run the application by clicking on the Play button .
  5. A welcome message with instructions will be displayed to teach you how to get the results.

Diagram Explaining the Puzzle

Tower Of Hanoi

The whole concept is to move a pile of disks from one rod to another using a helper rode.

A few rules should be applied to the puzzle:-

  • Only one disk is allowed to move at a time.
  • Bigger disks can never be on the top of smaller ones.
  • Minimum number of disks to move is 1.

To calculate the minimum number of moves required for the stack to be transferred, the following formula is used:-

  • Total Number of Moves = (2 ^ Number Of Disks) - 1 ... Note that ^ is the power operator.

Considering that we have three rods/pegs, let's give every one of them a number.

E.g.: 1-2-3 in ascending order where:

  • '1' is the starting pile that has the disks.
  • '2' is the helper rod that will be calculated every recursive call.
  • '3' is the destination rode where the final move will happen.

The steps taken to calculate movements on the disks are:

  1. Move (n - 1) disk from pile 1 to 3 using the helper pile 2 as an intermediate.
  2. Move nth disk from pile 1 to 3
  3. Move (n - 1) disk from 2 to 3 using the helper pile 1 as an intermediate.

Note: n is the number of disks needs to be transferred.

After performing the total amount of recursive calls calculated earlier, the destination pile will have all the disks ordered exactly as they were on the first rod at the beginning of the puzzle.

Architecture

  • C# Console Core application.

About

C# console application game that uses Recursion to perform a set of movements on disks over different piles.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%